-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add accessibility Github Actions workflow #7669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f05ca2e
add accessibility workflow
hbuchel aa57488
Update name
hbuchel ca6d515
fix function name
hbuchel 9920b7b
add light mode/dark mode testing, clean up logging
hbuchel ad13347
add some pages to show violations
hbuchel b6c5e19
add spacing
hbuchel 6d60435
revert page changes
hbuchel 9236db1
make sure actions packages are up to date
hbuchel 5139d3f
Merge branch 'main' into AddAxeWorkflow
hbuchel 88f6524
add some initial readme guidance for accessibility violations
hbuchel e00f5ff
covert chain promises to async/await, some formatting/comments updated
hbuchel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: Accessibility Scan | ||
on: | ||
pull_request: | ||
branches: [main] | ||
types: [opened, synchronize] | ||
env: | ||
BUILD_DIR: 'client/www/next-build' | ||
jobs: | ||
accessibility: | ||
name: Runs accessibility scan on changed pages | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout branch | ||
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 | ||
- name: Setup Node.js 20 | ||
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2 | ||
with: | ||
node-version: 20.x | ||
- name: Install dependencies | ||
run: yarn | ||
- name: Build | ||
run: yarn build | ||
env: | ||
NODE_OPTIONS: --max_old_space_size=4096 | ||
- name: Get changed/new pages to run accessibility tests on | ||
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
id: pages-to-a11y-test | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const { getChangedPages } = require('./.github/workflows/scripts/check_for_changed_pages.js'); | ||
const buildDir = process.env.BUILD_DIR; | ||
return getChangedPages({github, context, buildDir}); | ||
- name: Run site | ||
run: | | ||
python -m http.server 3000 -d ${{ env.BUILD_DIR }} & | ||
sleep 5 | ||
- name: Run accessibility tests on changed/new MDX pages | ||
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
id: axeResults | ||
with: | ||
result-encoding: string | ||
script: | | ||
const { runAccessibilityScan } = require('./.github/workflows/scripts/run_accessibility_scan.js'); | ||
const pages = ${{ steps.pages-to-a11y-test.outputs.result }} | ||
return await runAccessibilityScan(pages) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module.exports = { | ||
getChangedPages: ({ github, context, buildDir }) => { | ||
const fs = require('fs'); | ||
const cheerio = require('cheerio'); | ||
|
||
const urlList = []; | ||
|
||
const { | ||
issue: { number: issue_number }, | ||
repo: { owner, repo } | ||
} = context; | ||
|
||
// Use the Github API to query for the list of files from the PR | ||
return github | ||
.paginate( | ||
'GET /repos/{owner}/{repo}/pulls/{pull_number}/files', | ||
{ owner, repo, pull_number: issue_number }, | ||
(response) => response.data.filter((file) => (file.status === 'modified' || file.status === 'added')) | ||
) | ||
.then((files) => { | ||
const possiblePages = []; | ||
const platforms = [ | ||
'android', | ||
'angular', | ||
'flutter', | ||
'javascript', | ||
'nextjs', | ||
'react', | ||
'react-native', | ||
'swift', | ||
'vue', | ||
] | ||
hbuchel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
files.forEach(({filename}) => { | ||
const isPage = filename.startsWith('src/pages') && (filename.endsWith('index.mdx') || filename.endsWith('index.tsx')); | ||
if(isPage) { | ||
|
||
const path = filename.replace('src/pages', '').replace('/index.mdx', '').replace('/index.tsx', ''); | ||
if(path.includes('[platform]')) { | ||
platforms.forEach((platform) => { | ||
possiblePages.push(path.replace('[platform]', platform)); | ||
}) | ||
} else { | ||
possiblePages.push(path); | ||
} | ||
} | ||
}); | ||
|
||
const siteMap = fs.readFileSync(`${buildDir}/sitemap.xml`); | ||
|
||
const siteMapParse = cheerio.load(siteMap, { | ||
xml: true | ||
}); | ||
|
||
siteMapParse('url').each(function () { | ||
urlList.push(siteMapParse(this).find('loc').text()); | ||
}); | ||
|
||
const pages = possiblePages.filter((page) => urlList.includes(`https://docs.amplify.aws${page}/`)); | ||
|
||
return pages; | ||
}); | ||
}, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
module.exports = { | ||
runAccessibilityScan: (pages) => { | ||
const core = require('@actions/core'); | ||
const { AxePuppeteer } = require('@axe-core/puppeteer'); | ||
const puppeteer = require('puppeteer'); | ||
|
||
const violations = []; | ||
|
||
// When flipping from dark mode to light mode, we need to add a small timeout | ||
// to account for css transitions otherwise there can be false contrast issues found. | ||
// Usage: await sleep(300); | ||
const sleep = ms => new Promise(res => setTimeout(res, ms)); | ||
|
||
const logViolation = (violation) => { | ||
violation.nodes.forEach(node => { | ||
console.log(node.failureSummary); | ||
console.log(node.html); | ||
node.target.forEach( target => { | ||
console.log('CSS target: ', target) | ||
}) | ||
console.log('\n'); | ||
}) | ||
|
||
} | ||
|
||
async function runAxeAnalyze(pages) { | ||
for (const page of pages) { | ||
const browser = await puppeteer.launch(); | ||
const pageToVisit = await browser.newPage(); | ||
await pageToVisit.goto(`http://localhost:3000${page}/`, {waitUntil: 'domcontentloaded'}); | ||
await pageToVisit.click('button[title="Light mode"]'); | ||
await pageToVisit.waitForSelector('[data-amplify-color-mode="light"]'); | ||
await sleep(300); | ||
|
||
|
||
try { | ||
console.log(`\nTesting light mode: http://localhost:3000${page}/`) | ||
const results = await new AxePuppeteer(pageToVisit).analyze(); | ||
if(results.violations.length > 0) { | ||
results.violations.forEach(violation => { | ||
logViolation(violation); | ||
violations.push(violation); | ||
}) | ||
} else { | ||
console.log('No violations found. \n'); | ||
} | ||
|
||
} catch (error) { | ||
core.setFailed(`There was an error testing the page: ${error}`); | ||
} | ||
|
||
await pageToVisit.click('button[title="Dark mode"]'); | ||
await pageToVisit.waitForSelector('[data-amplify-color-mode="dark"]'); | ||
await sleep(300); | ||
|
||
try { | ||
console.log(`\nTesting dark mode: http://localhost:3000${page}/`) | ||
const results = await new AxePuppeteer(pageToVisit).analyze(); | ||
if(results.violations.length > 0) { | ||
results.violations.forEach(violation => { | ||
logViolation(violation); | ||
violations.push(violation); | ||
}) | ||
} else { | ||
console.log('No violations found. \n'); | ||
} | ||
|
||
} catch (error) { | ||
core.setFailed(`There was an error testing the page: ${error}`); | ||
} | ||
|
||
await browser.close(); | ||
} | ||
if(violations.length > 0) { | ||
core.setFailed(`Please fix the above accessibility violations.`); | ||
} | ||
} | ||
|
||
runAxeAnalyze(pages); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not blocking: Could we use
async/await
here? I know one of our utilities function started usingawait
, but I know we still have other scripts using the promises chaining, so this comment isn't blockingexample of async and await from one of our scripts:
docs/.github/workflows/scripts/utilities.js
Lines 14 to 22 in 19117c7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this in latest commit to use async/await instead (and some formatting/comment updates)