Skip to content

Commit 91072df

Browse files
authored
Extracts issue body generation in prepration for adding screenshots (#134)
Extracts the logic to generate an issue body in preparation for adding screenshots. Adding screenshots will update the issue body generation, so I am pulling that out separately so it's easier to review the diff on the future PR. Also adds a new `tests` directory to the `file` directory with some basic unit tests for the new generated issue body function. Issues were created as expected when manually testing internally.
2 parents 7b1ad39 + 7c85a7e commit 91072df

File tree

4 files changed

+85
-25
lines changed

4 files changed

+85
-25
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Finding } from "./types.d.js";
2+
3+
export function generateIssueBody(finding: Finding, repoWithOwner: string): string {
4+
const solutionLong = finding.solutionLong
5+
?.split("\n")
6+
.map((line: string) =>
7+
!line.trim().startsWith("Fix any") &&
8+
!line.trim().startsWith("Fix all") &&
9+
line.trim() !== ""
10+
? `- ${line}`
11+
: line
12+
)
13+
.join("\n");
14+
const acceptanceCriteria = `## Acceptance Criteria
15+
- [ ] The specific axe violation reported in this issue is no longer reproducible.
16+
- [ ] The fix MUST meet WCAG 2.1 guidelines OR the accessibility standards specified by the repository or organization.
17+
- [ ] A test SHOULD be added to ensure this specific axe violation does not regress.
18+
- [ ] This PR MUST NOT introduce any new accessibility issues or regressions.
19+
`;
20+
const body = `## What
21+
An accessibility scan flagged the element \`${finding.html}\` on ${finding.url} because ${finding.problemShort}. Learn more about why this was flagged by visiting ${finding.problemUrl}.
22+
23+
To fix this, ${finding.solutionShort}.
24+
${solutionLong ? `\nSpecifically:\n\n${solutionLong}` : ''}
25+
26+
${acceptanceCriteria}
27+
`;
28+
29+
return body;
30+
}
31+

.github/actions/file/src/openIssue.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Octokit } from '@octokit/core';
22
import type { Finding } from './types.d.js';
3+
import { generateIssueBody } from "./generateIssueBody.js";
34
import * as url from 'node:url'
45
const URL = url.URL;
56

@@ -23,32 +24,10 @@ export async function openIssue(octokit: Octokit, repoWithOwner: string, finding
2324
const labels = [`${finding.scannerType} rule: ${finding.ruleId}`, `${finding.scannerType}-scanning-issue`];
2425
const title = truncateWithEllipsis(
2526
`Accessibility issue: ${finding.problemShort[0].toUpperCase() + finding.problemShort.slice(1)} on ${new URL(finding.url).pathname}`,
26-
GITHUB_ISSUE_TITLE_MAX_LENGTH
27+
GITHUB_ISSUE_TITLE_MAX_LENGTH,
2728
);
28-
const solutionLong = finding.solutionLong
29-
?.split("\n")
30-
.map((line) =>
31-
!line.trim().startsWith("Fix any") &&
32-
!line.trim().startsWith("Fix all") &&
33-
line.trim() !== ""
34-
? `- ${line}`
35-
: line
36-
)
37-
.join("\n");
38-
const acceptanceCriteria = `## Acceptance Criteria
39-
- [ ] The specific axe violation reported in this issue is no longer reproducible.
40-
- [ ] The fix MUST meet WCAG 2.1 guidelines OR the accessibility standards specified by the repository or organization.
41-
- [ ] A test SHOULD be added to ensure this specific axe violation does not regress.
42-
- [ ] This PR MUST NOT introduce any new accessibility issues or regressions.
43-
`;
44-
const body = `## What
45-
An accessibility scan flagged the element \`${finding.html}\` on ${finding.url} because ${finding.problemShort}. Learn more about why this was flagged by visiting ${finding.problemUrl}.
4629

47-
To fix this, ${finding.solutionShort}.
48-
${solutionLong ? `\nSpecifically:\n\n${solutionLong}` : ''}
49-
50-
${acceptanceCriteria}
51-
`;
30+
const body = generateIssueBody(finding, repoWithOwner);
5231

5332
return octokit.request(`POST /repos/${owner}/${repo}/issues`, {
5433
owner,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, it, expect } from "vitest";
2+
import { generateIssueBody } from "../src/generateIssueBody.ts";
3+
4+
const baseFinding = {
5+
scannerType: "axe",
6+
ruleId: "color-contrast",
7+
url: "https://example.com/page",
8+
html: "<span>Low contrast</span>",
9+
problemShort: "elements must meet minimum color contrast ratio thresholds",
10+
problemUrl: "https://dequeuniversity.com/rules/axe/4.10/color-contrast?application=playwright",
11+
solutionShort: "ensure the contrast between foreground and background colors meets WCAG thresholds",
12+
};
13+
14+
describe("generateIssueBody", () => {
15+
it("includes acceptance criteria and omits the Specifically section when solutionLong is missing", () => {
16+
const body = generateIssueBody(baseFinding, "github/accessibility-scanner");
17+
18+
expect(body).toContain("## What");
19+
expect(body).toContain("## Acceptance Criteria");
20+
expect(body).toContain("The specific axe violation reported in this issue is no longer reproducible.");
21+
expect(body).not.toContain("Specifically:");
22+
});
23+
24+
it("formats solutionLong lines into bullets while preserving Fix any/Fix all lines", () => {
25+
const body = generateIssueBody(
26+
{
27+
...baseFinding,
28+
solutionLong: [
29+
"Use a darker foreground color.",
30+
"Fix any of the following:",
31+
"Increase font weight.",
32+
"Fix all of the following:",
33+
"Add a non-color visual indicator.",
34+
"",
35+
].join("\n"),
36+
},
37+
"github/accessibility-scanner",
38+
);
39+
40+
expect(body).toContain("Specifically:");
41+
expect(body).toContain("- Use a darker foreground color.");
42+
expect(body).toContain("Fix any of the following:");
43+
expect(body).toContain("- Increase font weight.");
44+
expect(body).toContain("Fix all of the following:");
45+
expect(body).toContain("- Add a non-color visual indicator.");
46+
47+
expect(body).not.toContain("- Fix any of the following:");
48+
expect(body).not.toContain("- Fix all of the following:");
49+
});
50+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.0-development",
44
"description": "Finds potential accessibility gaps, files GitHub issues to track them, and attempts to fix them with Copilot",
55
"scripts": {
6-
"test": "vitest run tests/*.test.ts"
6+
"test": "vitest run tests/*.test.ts .github/actions/**/tests/*.test.ts"
77
},
88
"repository": {
99
"type": "git",

0 commit comments

Comments
 (0)