diff --git a/.github/scripts/commons.mjs b/.github/scripts/commons.mjs index d4cefe095f2..d4f679b622c 100644 --- a/.github/scripts/commons.mjs +++ b/.github/scripts/commons.mjs @@ -8,6 +8,8 @@ export const config = { testAssetsDir: `./test-assets/`, } +export const branchName = process.env.BRANCH_NAME ?? 'main'; + export async function cloneWcagActRules({ tmpDir }) { await $`rm -rf ${tmpDir}`; await $`git clone \ @@ -18,8 +20,7 @@ export async function cloneWcagActRules({ tmpDir }) { `; } -export async function createOrCheckoutBranch({ tmpDir }, branchName) { - assert(branchName, 'branchName must be defined'); +export async function createOrCheckoutBranch({ tmpDir }) { cd(tmpDir); try { await $`git checkout ${branchName}`; diff --git a/.github/scripts/pr-preview.mjs b/.github/scripts/pr-preview.mjs new file mode 100644 index 00000000000..d6d70cb69f0 --- /dev/null +++ b/.github/scripts/pr-preview.mjs @@ -0,0 +1,29 @@ +#!/usr/bin/env zx +import 'zx/globals'; +import { config, cloneWcagActRules, createOrCheckoutBranch, commitAndPush, branchName } from './commons.mjs' + +await cloneWcagActRules(config); +await createOrCheckoutBranch(config, branchName); +await generateProposedRulePages(config); +await generateTestCases(config); +const commitMessage = (await $`git log -1 --pretty=%B`).stdout; +await commitAndPush(config, commitMessage); + +async function generateProposedRulePages({ tmpDir, rulesDir, glossaryDir, testAssetsDir }) { + await $`node ./node_modules/act-tools/dist/cli/rule-transform.js \ + --rulesDir "${rulesDir}" \ + --glossaryDir "${glossaryDir}" \ + --testAssetsDir "${testAssetsDir}" \ + --outDir "${tmpDir}" \ + --proposed + `; +} + +async function generateTestCases({ tmpDir, rulesDir, testAssetsDir }) { + await $`node ./node_modules/act-tools/dist/cli/build-examples.js \ + --rulesDir "${rulesDir}" \ + --testAssetsDir "${testAssetsDir}" \ + --outDir "${tmpDir}" \ + --proposed + `; +} diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml new file mode 100644 index 00000000000..3000acf840e --- /dev/null +++ b/.github/workflows/pr-preview.yml @@ -0,0 +1,39 @@ +name: PR Preview +on: + pull_request: {} + workflow_dispatch: + +jobs: + dispatch: + runs-on: ubuntu-latest + permissions: + pull-requests: write + env: + branch_name: ${{ github.event.pull_request.head.ref }} + gh_repo: act-rules/act-rules.github.io + GH_TOKEN: ${{ github.token }} + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + 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://git@github.com/ + - 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: Build and deploy WAI update + run: npm run pr:preview + - name: Comment on PR with preview link + run: | + gh pr comment ${{ github.event.pull_request.number }} --body "Preview ready at https://deploy-preview-${{ env.branch_name }}--wai-wcag-act-rules.netlify.app" diff --git a/package.json b/package.json index fe780b9d4cd..e9431728f27 100644 --- a/package.json +++ b/package.json @@ -177,7 +177,8 @@ "formatRulesDir": "prettier --write './_rules/**/*.md'", "format": "prettier --write *.{json,md,js,html,css,yml} './{__tests__,_rules,.github,pages,test-assets,test-utils,utils}/**/*.{json,md,js,html,css,yml}'", "test": "jest --coverage", - "build:wai": "zx .github/scripts/wai-build.mjs" + "build:wai": "zx .github/scripts/wai-build.mjs", + "pr:preview": "zx .github/scripts/pr-preview.mjs" }, "homepage": "https://github.com/act-rules/act-rules.github.io", "repository": {