forked from act-rules/act-rules.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GitHub script to set rules to approved (act-rules#1963)
* Add GH script to set rules to approved * Add docs
- Loading branch information
1 parent
e35e9e7
commit 5362b59
Showing
8 changed files
with
189 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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,82 @@ | ||
#!/usr/bin/env zx | ||
import 'zx/globals'; | ||
import assert from 'assert'; | ||
import moment from 'moment'; | ||
|
||
import { | ||
config, | ||
cloneWcagActRules, | ||
createOrCheckoutBranch, | ||
commitAndPush | ||
} from './commons.mjs'; | ||
|
||
const w3cDataFormat = 'D MMMM YYYY'; | ||
const isoDateFormat = 'YYYY-MM-DD'; | ||
|
||
assert(typeof argv.ruleId === 'string', 'Expected --ruleId to be set'); | ||
assert(argv.ruleId.length === 6, 'Expected --ruleId to be 6 characters long'); | ||
assert(typeof argv.branch === 'string', 'Expected --branch to be set'); | ||
|
||
if (!argv['skip-clone']) { | ||
await cloneWcagActRules(config); | ||
} | ||
|
||
await createOrCheckoutBranch(config, argv.branch); | ||
await generateApprovedRulePages(config, argv.ruleId); | ||
await updateRuleVersionsYaml(config, argv.ruleId); | ||
await approveTestCaseJson(config, argv.ruleId); | ||
await commitAndPush(config, `Set ${argv.ruleId} to approved`); | ||
|
||
async function generateApprovedRulePages({ tmpDir, rulesDir, glossaryDir }, ruleId) { | ||
await $`node ./node_modules/act-tools/dist/cli/rule-transform.js \ | ||
--rulesDir "${rulesDir}" \ | ||
--glossaryDir "${glossaryDir}" \ | ||
--outDir "${tmpDir}" \ | ||
--ruleIds "${ruleId}" | ||
`; | ||
} | ||
|
||
async function updateRuleVersionsYaml({ tmpDir }, ruleId) { | ||
const ruleVersionPath = `${tmpDir}_data/wcag-act-rules/rule-versions.yml`; | ||
let ruleVersionsStr = fs.readFileSync(ruleVersionPath, 'utf8'); | ||
const ruleVersions = YAML.parse(ruleVersionsStr); | ||
assert( | ||
ruleVersions[ruleId] === undefined, | ||
`RuleID ${ruleId} should not exists in rule-versions.yml. Was this rule approved before?` | ||
); | ||
|
||
const proposedText = fs.readFileSync(`${tmpDir}content/rules/${ruleId}/proposed.md`, 'utf8'); | ||
const proposedData = proposedText.match(/last_modified:\s+(.*)/)?.[1] | ||
assert(proposedData, `Unable to find last_modified data in ${ruleId}/proposed.md`); | ||
|
||
ruleVersions[ruleId] = [{ | ||
file: 'proposed.md', | ||
url: `${ruleId}/proposed/`, | ||
w3cDate: proposedData, | ||
isoDate: moment(proposedData, w3cDataFormat).format(isoDateFormat) | ||
}, { | ||
file: 'index.md', | ||
url: `${ruleId}/`, | ||
w3cDate: moment().format(w3cDataFormat), | ||
isoDate: moment().format(isoDateFormat) | ||
}] | ||
|
||
ruleVersionsStr = YAML.stringify(ruleVersions); | ||
fs.writeFileSync(ruleVersionPath, ruleVersionsStr, 'utf8'); | ||
console.log(`Added ${ruleId} to rule-versions.yml`); | ||
} | ||
|
||
async function approveTestCaseJson({ tmpDir }, ruleId) { | ||
let testCaseCount = 0; | ||
const testCaseJsonPath = `${tmpDir}content-assets/wcag-act-rules/testcases.json`; | ||
const testCaseJson = JSON.parse(fs.readFileSync(testCaseJsonPath, 'utf8')); | ||
testCaseJson.testcases.forEach((testCase, index) => { | ||
if (testCase.ruleId === ruleId) { | ||
// Override rather than update so that `approved` isn't at the bottom | ||
testCaseJson.testcases[index] = { ruleId, approved: true, ...testCase } | ||
testCaseCount++ | ||
} | ||
}); | ||
console.log(`Set ${testCaseCount} test cases of rule ${ruleId} to be approved in testcases.json`); | ||
fs.writeFileSync(testCaseJsonPath, JSON.stringify(testCaseJson, null, 2), 'utf8'); | ||
} |
This file contains 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,52 @@ | ||
import 'zx/globals'; | ||
import assert from 'assert'; | ||
|
||
export const config = { | ||
tmpDir: `./wcag-act-rules-tmp/`, | ||
rulesDir: `./_rules/`, | ||
glossaryDir: `./pages/glossary/`, | ||
testAssetsDir: `./test-assets/`, | ||
} | ||
|
||
export async function cloneWcagActRules({ tmpDir }) { | ||
await $`rm -rf ${tmpDir}`; | ||
await $`git clone \ | ||
--no-single-branch \ | ||
--branch main \ | ||
https://github.com/w3c/wcag-act-rules.git ${tmpDir} \ | ||
--depth 1 | ||
`; | ||
} | ||
|
||
export async function createOrCheckoutBranch({ tmpDir }, branchName) { | ||
assert(branchName, 'branchName must be defined'); | ||
cd(tmpDir); | ||
try { | ||
await $`git checkout ${branchName}`; | ||
} catch { | ||
await $`git checkout -b ${branchName}`; | ||
} finally { | ||
cd(`../`); | ||
} | ||
} | ||
|
||
export async function commitAndPush({ tmpDir }, commitMessage) { | ||
cd(tmpDir); | ||
try { | ||
const diff = (await $`git diff --name-status`).stdout; | ||
if (diff.trim().length === 0) { | ||
console.log('No changes detected, skipping git commit') | ||
return; | ||
} | ||
await $`git add .`; | ||
await $`git commit -m ${commitMessage}`; | ||
try { | ||
await $`git push`; | ||
} catch { | ||
const branchName = (await $`git branch --show-current`); | ||
await $`git push --set-upstream origin ${branchName}`; | ||
} | ||
} finally { | ||
cd(`../`); | ||
} | ||
} |
This file contains 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 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,42 @@ | ||
# Sets a rule to "approved" in the wcag-act-rules repository in a given branch. | ||
name: Set a rule to approved | ||
on: | ||
workflow_dispatch: | ||
inputs: | ||
ruleId: | ||
description: 'Rule ID' | ||
required: true | ||
branch: | ||
description: 'Target branch name (new or existing)' | ||
required: true | ||
|
||
jobs: | ||
dispatch: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check user permission | ||
uses: 74th/[email protected] | ||
with: | ||
listfile: .github/workflows/chair-accounts.md | ||
|
||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: '16' | ||
cache: npm | ||
- uses: actions/cache@v3 | ||
with: | ||
path: ~/.npm | ||
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | ||
restore-keys: | | ||
${{ runner.os }}-node- | ||
- run: git config --global url."https://github.com/".insteadOf ssh://[email protected]/ | ||
- name: Install dependencies | ||
run: npm ci | ||
- name: Configure git | ||
run: | | ||
git config --global url."https://${{ secrets.WAI_GIT_NAME }}:${{ secrets.WAI_GIT_ACCESS_TOKEN }}@github.com".insteadOf "https://github.com" | ||
git config --global user.name "${{ secrets.WAI_GIT_NAME }}" | ||
git config --global user.email "${{ secrets.WAI_GIT_EMAIL }}" | ||
- name: Set rule to approved | ||
run: npx zx .github/scripts/wai-build.mjs --ruleId ${{ github.event.inputs.ruleId }}" --branch ${{ github.event.inputs.branch }}" |
This file contains 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,4 @@ | ||
WilcoFiers | ||
daniel-montalvo | ||
tbostic32 | ||
kengdoj |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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