Skip to content

Commit 54166ff

Browse files
authored
ci: run the accessibility contracts at the head that built their Storybook (#9971)
* ci: run the accessibility contracts at the head that built their Storybook * ci: treat a step in the merge checkout before the head checkout as guarded
1 parent 4f5257f commit 54166ff

5 files changed

Lines changed: 135 additions & 21 deletions

File tree

‎.github/scripts/a11y-pr-contracts.mjs‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,9 @@ export function changedSinceMergeBase(base) {
610610
}
611611

612612
/**
613-
* A spec's routing lines (and the audit config) come from the commit the
614-
* workflow steps come from when it has the file (an older PR head predates
615-
* current routing); otherwise from the checkout.
613+
* A spec's routing lines, the audit config and the recorded durations come
614+
* from the commit the workflow steps come from when it has the file (an older
615+
* PR head predates current routing); otherwise from the checkout.
616616
*/
617617
function readPolicySource(root, file, policySha) {
618618
if (policySha) {
@@ -639,10 +639,18 @@ function readAuditConfig(root, file, policySha) {
639639
return JSON.parse(source);
640640
}
641641

642-
function readDurations(root) {
643-
const file = path.join(root, '.github/a11y-contract-durations.json');
644-
if (!fs.existsSync(file)) return {};
645-
return fileSeconds(JSON.parse(fs.readFileSync(file, 'utf8')));
642+
function readDurations(root, policySha) {
643+
let source;
644+
try {
645+
source = readPolicySource(
646+
root,
647+
'.github/a11y-contract-durations.json',
648+
policySha,
649+
);
650+
} catch {
651+
return {};
652+
}
653+
return fileSeconds(JSON.parse(source));
646654
}
647655

648656
/** The Playwright worker count, as playwright.config.ts reads it. */
@@ -765,7 +773,7 @@ function main() {
765773
components: ownedBy,
766774
changedPaths: readLines(changedFilesFile) ?? changedPaths,
767775
}),
768-
seconds: readDurations(root),
776+
seconds: readDurations(root, policySha),
769777
workers: contractWorkers(),
770778
})
771779
: {run: routed, notRun: []};

‎.github/scripts/a11y-pr-contracts.test.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ describe('the default check of the touched components', () => {
475475
expect(runner).toContain(
476476
'changedPaths: readLines(changedFilesFile) ?? changedPaths',
477477
);
478-
expect(runner).toContain('seconds: readDurations(root)');
478+
expect(runner).toContain('seconds: readDurations(root, policySha)');
479479
expect(runner).toContain('workers: contractWorkers()');
480480
});
481481
});

‎.github/scripts/ci-head-checkout-scripts.test.mjs‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
* `head_file_present`, the step loads it from the workflow's own commit
1919
* (`git show "$WORKFLOW_SHA:<script>"`, or from the job's `.test-scope`
2020
* checkout, whose sparse directories hold it, directly or through a copy an
21-
* earlier step made in `$RUNNER_TEMP`), or the step checks for it itself
22-
* (`[ ! -f ... ]`). A job that does not measure the head takes the default
21+
* earlier step made in `$RUNNER_TEMP`), the step runs in the job's merge
22+
* checkout before its head checkout replaces the workspace, or the step checks
23+
* for it itself (`[ ! -f ... ]`). A job that does not measure the head takes the default
2324
* merge checkout instead, and this test does not apply to it.
2425
*
2526
* Scope: `.js`, `.mjs` and `.cjs` scripts named in a step's `run:`.
@@ -158,6 +159,24 @@ export function beforeHeadCheckout(job, step) {
158159
return head === -1 || steps.indexOf(step) < head;
159160
}
160161

162+
/**
163+
* Whether the step runs in the job's merge checkout (the workflow's own
164+
* commit): a workspace checkout with no `ref` comes before the step, and the
165+
* head checkout comes after it.
166+
*/
167+
export function inMergeCheckout(job, step) {
168+
const steps = job.steps ?? [];
169+
const merge = steps.findIndex(
170+
each =>
171+
String(each.uses ?? '').startsWith('actions/checkout@') &&
172+
each.with?.ref == null &&
173+
!each.with?.path,
174+
);
175+
return (
176+
merge !== -1 && merge < steps.indexOf(step) && beforeHeadCheckout(job, step)
177+
);
178+
}
179+
161180
export function unguardedScripts(job) {
162181
const scope = (job.steps ?? []).find(step =>
163182
String(step.uses ?? '').includes('test-scope'),
@@ -180,6 +199,7 @@ export function unguardedScripts(job) {
180199
beforeHeadCheckout(job, step)
181200
)
182201
continue;
202+
if (inMergeCheckout(job, step)) continue;
183203
// Run from a copy an earlier step took from that scope checkout.
184204
if (copiedFromScope(job, step, script)) continue;
185205
// The step skips a head without this script on its own.
@@ -246,6 +266,24 @@ describe('pull-request jobs that check out the head', () => {
246266
}
247267
});
248268

269+
it('exempts a step in the merge checkout only before the head checkout', () => {
270+
const merge = {name: 'Merge', uses: 'actions/checkout@v7'};
271+
const head = {
272+
name: 'Head',
273+
uses: 'actions/checkout@v7',
274+
with: {ref: '${{ github.event.pull_request.head.sha }}'},
275+
};
276+
const before = {name: 'Before', run: 'node .github/scripts/new.mjs'};
277+
const after = {name: 'After', run: 'node .github/scripts/new.mjs'};
278+
expect(unguardedScripts({steps: [merge, before, head, after]})).toEqual([
279+
'After: .github/scripts/new.mjs',
280+
]);
281+
// With no merge checkout, the step reads no tree of the workflow.
282+
expect(unguardedScripts({steps: [before, head]})).toEqual([
283+
'Before: .github/scripts/new.mjs',
284+
]);
285+
});
286+
249287
it("exempts only the script a step's own missing-file check names", () => {
250288
const run = [
251289
'D7_TEST=.github/scripts/d7.test.mjs',

‎.github/scripts/touched-components.test.mjs‎

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -749,14 +749,63 @@ describe('pr-component-a11y-contracts routing', () => {
749749
expect(find.run).toContain(
750750
'import {isA11ySpecFile} from "./.github/scripts/lib/a11y-spec-files.mjs";',
751751
);
752-
const checkout = contractStep('Checkout');
753-
expect(checkout.with?.ref).toBeUndefined();
752+
// The merge commit, as the touched check: an older head lacks these scripts.
753+
expect(
754+
contractStep('Check out the selection scripts').with?.ref,
755+
).toBeUndefined();
756+
});
757+
758+
it("runs the head's own specs against the Storybook that head built", () => {
759+
const storybookCheckout = ci.jobs['build-storybook'].steps.find(
760+
candidate =>
761+
candidate.uses?.startsWith('actions/checkout@') && candidate.with?.ref,
762+
);
763+
const headCheckout = contractStep(
764+
'Check out the head that built the Storybook',
765+
);
766+
expect(storybookCheckout.with.ref).toBe(
767+
'${{ github.event.pull_request.head.sha || github.sha }}',
768+
);
769+
expect(headCheckout.with.ref).toBe(storybookCheckout.with.ref);
770+
expect(headCheckout.if).toBe(ran);
771+
const names = contracts.steps.map(({name}) => name);
772+
const at = name => names.indexOf(name);
773+
expect(at('Check out the head that built the Storybook')).toBeGreaterThan(
774+
Math.max(
775+
at('Decide the touched components'),
776+
at('Find the changed browser accessibility specs'),
777+
),
778+
);
779+
for (const name of [
780+
'Setup Node and pnpm',
781+
'Download Storybook artifact',
782+
'Install Playwright',
783+
"Run the touched components' accessibility contracts",
784+
]) {
785+
expect(at(name), name).toBeGreaterThan(
786+
at('Check out the head that built the Storybook'),
787+
);
788+
}
789+
// Only the selection and head checkouts: no later checkout moves the tree.
790+
expect(
791+
contracts.steps
792+
.filter(candidate => candidate.uses?.startsWith('actions/checkout@'))
793+
.map(({name}) => name),
794+
).toEqual([
795+
'Check out the scope action',
796+
'Check out the selection scripts',
797+
'Check out the head that built the Storybook',
798+
]);
754799
});
755800

756801
it('runs the changed specs and the touched owners, on four workers, inside the job budget', () => {
757802
expect(runStep.if).toBe(ran);
758-
expect(runStep.run).toBe(
759-
'node .github/scripts/a11y-pr-contracts.mjs --changed-only "$SPECS_PATH" --changed-files "$FILES_PATH" --owned-by "$OWNERS"',
803+
// The runner comes from the merge commit; the head may predate it.
804+
expect(runStep.run).toContain(
805+
'git show "$WORKFLOW_SHA:.github/scripts/a11y-pr-contracts.mjs" > "$runner"',
806+
);
807+
expect(runStep.run).toContain(
808+
'node "$runner" --changed-only "$SPECS_PATH" --changed-files "$FILES_PATH" --owned-by "$OWNERS"',
760809
);
761810
// Every changed path, not only the changed specs: a change in a
762811
// component folder owns that folder's specs.

‎.github/workflows/ci.yml‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3851,8 +3851,10 @@ jobs:
38513851
if: steps.scope.outputs.applies != 'true'
38523852
run: echo "No component or browser accessibility spec changed; no contract to run."
38533853

3854-
# The tested merge commit, as the touched-components job checks out.
3855-
- name: Checkout
3854+
# The tested merge commit, as the touched-components job checks out: a
3855+
# head that predates these scripts does not have them. The specs run at
3856+
# the head (see "Check out the head that built the Storybook").
3857+
- name: Check out the selection scripts
38563858
if: steps.scope.outputs.applies == 'true'
38573859
uses: actions/checkout@v7
38583860
with:
@@ -3923,6 +3925,17 @@ jobs:
39233925
echo "No touched component and no changed spec; no contract to run."
39243926
fi
39253927
3928+
# The specs, their helpers and the lockfile come from the same commit as
3929+
# the Storybook build. A merge commit has specs that the head Storybook
3930+
# does not have, and those specs fail. Merge-level interactions are the
3931+
# job of the post-merge and release checks.
3932+
- name: Check out the head that built the Storybook
3933+
if: steps.touched.outputs.has_components == 'true' || steps.specs.outputs.has_specs == 'true'
3934+
uses: actions/checkout@v7
3935+
with:
3936+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
3937+
persist-credentials: false
3938+
39263939
- name: Setup Node and pnpm
39273940
if: steps.touched.outputs.has_components == 'true' || steps.specs.outputs.has_specs == 'true'
39283941
uses: ./.github/actions/setup
@@ -3940,9 +3953,11 @@ jobs:
39403953
if: steps.touched.outputs.has_components == 'true' || steps.specs.outputs.has_specs == 'true'
39413954
run: pnpm install --frozen-lockfile && npx playwright install chromium
39423955

3943-
# Routing lines come from this merge commit. A failing or unfinished
3944-
# spec fails the job, and so the test join. A file that does not fit the
3945-
# budget is not run and does not fail the job.
3956+
# The runner and the routing lines come from the merge commit, so an
3957+
# older head is routed by current policy. The specs run at the head; a
3958+
# spec that the head does not have is not listed and does not run. A
3959+
# failing or unfinished spec fails the job, and so the test join. A file
3960+
# that does not fit the budget is not run and does not fail the job.
39463961
- name: Run the touched components' accessibility contracts
39473962
id: contracts
39483963
if: steps.touched.outputs.has_components == 'true' || steps.specs.outputs.has_specs == 'true'
@@ -3956,7 +3971,11 @@ jobs:
39563971
FILES_PATH: ${{ steps.scope.outputs.changed_files_path }}
39573972
OWNERS: ${{ steps.touched.outputs.has_components == 'true' && steps.touched.outputs.a11y_owners || '' }}
39583973
A11Y_CONTRACT_WORKERS: 4
3959-
run: node .github/scripts/a11y-pr-contracts.mjs --changed-only "$SPECS_PATH" --changed-files "$FILES_PATH" --owned-by "$OWNERS"
3974+
run: |
3975+
runner="$RUNNER_TEMP/a11y-pr-contracts.mjs"
3976+
git cat-file -e "$WORKFLOW_SHA^{commit}" 2>/dev/null || git fetch --quiet --no-tags --depth=1 origin "$WORKFLOW_SHA"
3977+
git show "$WORKFLOW_SHA:.github/scripts/a11y-pr-contracts.mjs" > "$runner"
3978+
node "$runner" --changed-only "$SPECS_PATH" --changed-files "$FILES_PATH" --owned-by "$OWNERS"
39603979
39613980
- name: Upload contract evidence
39623981
if: always() && (steps.contracts.outcome == 'success' || steps.contracts.outcome == 'failure')

0 commit comments

Comments
 (0)