-
Notifications
You must be signed in to change notification settings - Fork 43
188 lines (161 loc) · 6.6 KB
/
a11y-contrast.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
name: A11y contrast check
on:
pull_request:
branches-ignore:
- 'changeset-release/**'
workflow_dispatch:
jobs:
changes:
uses: ./.github/workflows/hasChanged.yml
build:
needs: changes
if: needs.changes.outputs.tokens == 'true' || github.event_name == 'workflow_dispatch'
name: Check design token color contrast
runs-on: ubuntu-latest
continue-on-error: true
outputs:
faildChecks: ${{ steps.check-results.outputs.faildChecks }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- name: Install dependencies
run: npm ci --no-audit --no-fund --ignore-scripts
- name: Build tokens
run: npm run build:tokens
- name: Run required checks
run: |
npm run contrast:check
- name: Prepare check results
id: check-results
continue-on-error: true
uses: actions/github-script@v7
with:
script: |
const results = require('./color-contrast-check.json');
const faildChecks = results.reduce((acc, {failingContrast}) => acc + failingContrast, 0);
// prepare outputs for all failed themes
const failedResults = results.filter(themeResults => themeResults.failingContrast > 0).map(({theme, failingContrast, markdownTable}) => ({
title: `# ❌ \`${theme}\`: ${failingContrast} checks failed`,
body: `${markdownTable}`
}))
// prepare summary body
const summaryMarkdown = '## Design Token Contrast Check\n\n' +
results.map(({theme, failingContrast, failedMarkdownTable}) => {
if(failingContrast === 0) {
return "### \\`"+theme+"\\`: " + `✅ all checks passed\n\n`
}
// if there are failing checks, return a summary with a details section
return "### \\`"+theme+"\\`: " + `❌ ${failingContrast} checks failed\n\n` +
'<details>' +
`<summary>Show results table for theme: ${theme}</summary>\n` +
" \n"+
` ${failedMarkdownTable}` +
'\n</details>'
}).join('\n\n')
// set output
core.setOutput('summaryMarkdown', summaryMarkdown)
core.setOutput('failedResults', failedResults)
core.setOutput('faildChecks', faildChecks)
// fail action if any contrast check fails
if (faildChecks > 0) {
core.setFailed(`\u001b[91;1m🛑 ${faildChecks} contrast checks failed. Please fix the failing checks and try again.\n\nCheck action summary for more details.`);
}
// success
else {
core.info('\u001b[32;1m✅ All contrast checks passed!')
}
- name: Report check results as summary
uses: actions/github-script@v7
with:
script: |
const resultsMarkdown = `${{ steps.check-results.outputs.summaryMarkdown }}`
// output results to summary
core.summary.addRaw(resultsMarkdown, true)
core.summary.write({overwrite: true})
- name: Report check results as comment
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
env:
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_RUN_ID: ${{ github.run_id }}
with:
script: |
const results = ${{ steps.check-results.outputs.failedResults }}
const identifier = `<!-- contrast check -->`
const WORKFLOW_SUMMARY_URL = `https://github.com/${{env.GITHUB_REPOSITORY}}/actions/runs/${{env.GITHUB_RUN_ID}}`
// get comments
const {data: comments} = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
});
// get token issue
const tokenCheckComment = comments.filter(comment => comment.body.includes(identifier));
// delete existing comments
if(tokenCheckComment.length > 0) {
for (const comment of tokenCheckComment) {
await github.rest.issues.deleteComment({
comment_id: comment.id,
owner: context.repo.owner,
repo: context.repo.repo,
})
}
}
for (const {title, body} of results) {
// get token issue
const outputBody = `${title}\n\n${body}\n\n<a href="${WORKFLOW_SUMMARY_URL}">→ Details</a>${identifier}`
// if token issue does not exist, create it
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: outputBody
})
}
Fail_action_on_contrast_failing:
needs: build
name: Fail action on contrast failing
if: needs.build.outputs.faildChecks > 0
runs-on: ubuntu-latest
steps:
- name: Contrast checks failed
run: |
echo "::error::${{ needs.build.outputs.faildChecks }} contrast checks failed. Please fix the failing checks and try again."
exit 1
remove_comment:
needs: changes
if: needs.changes.outputs.tokens == 'false'
runs-on: ubuntu-latest
steps:
- name: Remove comment and summary
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const identifier = `<!-- contrast check -->`
// get comments
const {data: comments} = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
});
// get token issue
const tokenCheckComment = comments.filter(comment => comment.body.includes(identifier));
// delete existing comments
if(tokenCheckComment.length > 0) {
for (const comment of tokenCheckComment) {
await github.rest.issues.deleteComment({
comment_id: comment.id,
owner: context.repo.owner,
repo: context.repo.repo,
})
}
}
// remove summary
core.summary.clear()
core.summary.write({overwrite: true})