An open source design system that's fully customizable and agent ready
TypeScript
ci(a11y): scope PR evidence steps by the merge-base diff (#7470)
Cindy Zhang7d9d2970add7
diff --git a/.github/scripts/pr-scope-touches.sh b/.github/scripts/pr-scope-touches.shnew file mode 100755index 0000000000..2e20bab4e2--- /dev/null+++ b/.github/scripts/pr-scope-touches.sh@@ -0,0 +1,48 @@+#!/usr/bin/env bash+# Copyright (c) Meta Platforms, Inc. and affiliates.+#+# Does this pull request change a path matching PATTERN?+#+# pr-scope-touches.sh BASE_SHA PATTERN+#+# Exit 0: the PR changes a matching path. Exit 1: it does not. Exit 2: the+# merge base could not be found (or the arguments are unusable), so the caller+# cannot tell and should run its evidence, as it did before.+#+# Callers load this script from BASE_SHA, not from the PR head: a PR step's+# body comes from the merge commit, but pr-a11y checks out the PR head, which+# on an older branch does not have this file.+#+# The PR's changes are the diff from its merge base with BASE_SHA to HEAD,+# not from BASE_SHA itself. A branch cut before the base moved would otherwise+# report every file the base added since as a file the PR deleted, and a step+# would run evidence for specs that do not exist at the PR head.+set -uo pipefail++if [ -z "${1:-}" ] || [ -z "${2:-}" ]; then+ echo 'usage: pr-scope-touches.sh BASE_SHA PATTERN' >&2+ exit 2+fi+base="$1"+pattern="$2"+depth="${PR_SCOPE_FETCH_DEPTH:-200}" # first fetch, then each deepening step+head="$(git rev-parse HEAD)" || exit 2++git fetch --quiet --no-tags --depth="$depth" origin "$base" "$head" 2>/dev/null || exit 2+# Deepen until the merge base is reachable, however far the branch is behind.+until merge_base="$(git merge-base "$base" "$head" 2>/dev/null)"; do+ # Complete history and still no merge base: genuinely unknown.+ [ "$(git rev-parse --is-shallow-repository)" = true ] || exit 2+ git fetch --quiet --no-tags --deepen="$depth" origin "$base" "$head" 2>/dev/null || exit 2+done++# --no-renames lists both sides of a move, so moving a file out of a pattern+# still counts as touching it.+changed="$(git diff --no-renames --name-only "$merge_base" "$head")" || exit 2+# Not piped into `grep -q`: under pipefail its early exit can fail the pipe.+grep -qE "$pattern" <<<"$changed"+case $? in+ 0) exit 0 ;;+ 1) exit 1 ;;+ *) exit 2 ;; # an invalid pattern must not read as out of scope+esacdiff --git a/.github/scripts/pr-scope-touches.test.mjs b/.github/scripts/pr-scope-touches.test.mjsnew file mode 100644index 0000000000..17b7861db4--- /dev/null+++ b/.github/scripts/pr-scope-touches.test.mjs@@ -0,0 +1,192 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {execFileSync, spawnSync} from 'node:child_process';+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import {fileURLToPath} from 'node:url';+import {afterEach, beforeEach, describe, expect, it} from 'vitest';++const SCRIPT = path.join(+ path.dirname(fileURLToPath(import.meta.url)),+ 'pr-scope-touches.sh',+);+const LINK =+ '^(internal/a11y-spec/src/patterns/link\\.|packages/core/src/Link/)';++let root;+let origin;+let clone;++const git = (cwd, ...args) =>+ execFileSync('git', args, {+ cwd,+ encoding: 'utf8',+ env: {+ ...process.env,+ GIT_AUTHOR_NAME: 't',+ GIT_AUTHOR_EMAIL: '[email protected]',+ GIT_COMMITTER_NAME: 't',+ GIT_COMMITTER_EMAIL: '[email protected]',+ },+ }).trim();++function commit(cwd, file, message) {+ fs.mkdirSync(path.dirname(path.join(cwd, file)), {recursive: true});+ fs.writeFileSync(path.join(cwd, file), `${message}\n`);+ git(cwd, 'add', file);+ git(cwd, 'commit', '-q', '-m', message);+ return git(cwd, 'rev-parse', 'HEAD');+}++/** A depth-1 checkout of `sha`, as actions/checkout leaves it. */+function checkout(sha) {+ clone = path.join(root, `clone-${sha.slice(0, 7)}`);+ fs.mkdirSync(clone);+ git(clone, 'init', '-q');+ git(clone, 'remote', 'add', 'origin', `file://${origin}`);+ git(clone, 'fetch', '-q', '--depth=1', 'origin', sha);+ git(clone, 'checkout', '-q', '--detach', sha);+}++function touches(base, pattern = LINK) {+ return spawnSync('bash', [SCRIPT, base, pattern], {+ cwd: clone,+ encoding: 'utf8',+ }).status;+}++beforeEach(() => {+ root = fs.mkdtempSync(path.join(os.tmpdir(), 'pr-scope-'));+ origin = path.join(root, 'origin');+ fs.mkdirSync(origin);+ git(origin, 'init', '-q', '-b', 'main');+ git(origin, 'config', 'uploadpack.allowAnySHA1InWant', 'true');+ commit(origin, 'README.md', 'root');+});++afterEach(() => {+ fs.rmSync(root, {recursive: true, force: true});+});++describe('pr-scope-touches', () => {+ it('ignores files the base added after a stale branch was cut', () => {+ git(origin, 'checkout', '-q', '-b', 'feature');+ const head = commit(origin, 'packages/core/src/Markdown/x.ts', 'unrelated');+ git(origin, 'checkout', '-q', 'main');+ const base = commit(+ origin,+ 'packages/core/src/Link/__tests__/Link.a11y.chromium.spec.ts',+ 'link contract',+ );+ checkout(head);+ // A two-dot diff from the base tip lists the Link spec as deleted.+ expect(git(clone, 'fetch', '-q', '--depth=1', 'origin', base)).toBe('');+ expect(git(clone, 'diff', '--name-only', base, 'HEAD')).toContain(+ 'packages/core/src/Link/',+ );+ expect(touches(base)).toBe(1);+ });++ it('reports a branch that changes a matching path', () => {+ git(origin, 'checkout', '-q', '-b', 'feature');+ const head = commit(+ origin,+ 'packages/core/src/Link/Link.tsx',+ 'link change',+ );+ git(origin, 'checkout', '-q', 'main');+ const base = commit(origin, 'docs/other.md', 'base moved');+ checkout(head);+ expect(touches(base)).toBe(0);+ });++ it('reports a branch that deletes a matching path', () => {+ const shared = commit(origin, 'packages/core/src/Link/Link.tsx', 'link');+ git(origin, 'checkout', '-q', '-b', 'feature');+ git(origin, 'rm', '-q', 'packages/core/src/Link/Link.tsx');+ git(origin, 'commit', '-q', '-m', 'delete link');+ const head = git(origin, 'rev-parse', 'HEAD');+ checkout(head);+ expect(touches(shared)).toBe(0);+ });++ it('deepens until it finds a merge base far behind the base tip', () => {+ git(origin, 'checkout', '-q', '-b', 'feature');+ const head = commit(origin, 'a.md', 'a');+ git(origin, 'checkout', '-q', 'main');+ let base = '';+ for (let i = 0; i < 7; i++) base = commit(origin, `b${i}.md`, `b${i}`);+ base = commit(+ origin,+ 'packages/core/src/Link/__tests__/Link.a11y.chromium.spec.ts',+ 'link contract',+ );+ checkout(head);+ const status = spawnSync('bash', [SCRIPT, base, LINK], {+ cwd: clone,+ encoding: 'utf8',+ env: {...process.env, PR_SCOPE_FETCH_DEPTH: '2'},+ }).status;+ expect(status).toBe(1);+ });++ it('is unknown for unrelated histories', () => {+ const base = commit(origin, 'a.md', 'a');+ git(origin, 'checkout', '-q', '--orphan', 'other');+ git(origin, 'rm', '-rqf', '.');+ const head = commit(origin, 'packages/x.md', 'unrelated root');+ checkout(head);+ expect(touches(base)).toBe(2);+ });++ it('counts moving a matching file out of the pattern', () => {+ commit(origin, 'apps/storybook/stories/LinkA11y.stories.tsx', 'story');+ const shared = git(origin, 'rev-parse', 'HEAD');+ git(origin, 'checkout', '-q', '-b', 'feature');+ git(+ origin,+ 'mv',+ 'apps/storybook/stories/LinkA11y.stories.tsx',+ 'apps/storybook/stories/Moved.stories.tsx',+ );+ git(origin, 'commit', '-q', '-m', 'move');+ checkout(git(origin, 'rev-parse', 'HEAD'));+ expect(+ touches(+ shared,+ '^(packages/core/src/Link/|apps/storybook/stories/LinkA11y)',+ ),+ ).toBe(0);+ });++ it('is unknown, not out of scope, for unusable arguments', () => {+ const head = commit(origin, 'a.md', 'a');+ checkout(head);+ expect(touches(head, '(')).toBe(2);+ expect(+ spawnSync('bash', [SCRIPT, head], {cwd: clone, encoding: 'utf8'}).status,+ ).toBe(2);+ });++ it('is loaded from the base by every workflow step that uses it', () => {+ const workflow = fs.readFileSync(+ path.join(path.dirname(SCRIPT), '../workflows/ci.yml'),+ 'utf8',+ );+ const uses = workflow.match(/pr-scope-touches\.sh/g) ?? [];+ const fromBase =+ workflow.match(+ /git show "\$BASE_SHA:\.github\/scripts\/pr-scope-touches\.sh" > "\$helper"/g,+ ) ?? [];+ expect(uses.length).toBe(4);+ expect(fromBase.length).toBe(uses.length);+ expect(workflow).not.toMatch(/bash \.github\/scripts\/pr-scope-touches/);+ });++ it('is unknown when the base cannot be fetched', () => {+ const head = commit(origin, 'a.md', 'a');+ checkout(head);+ expect(touches('f'.repeat(40))).toBe(2);+ });+});diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 14934b4bd5..99bca65033 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -1410,14 +1410,19 @@ jobs:if node -e "process.exit(require('./analysis.json').changedPackages?.includes('@astryxdesign/richtext') ? 0 : 1)"; thenshould_run=truefi- # The sandbox's stories belong to no package, so compare file names- # with the base. If the base cannot be fetched, run the evidence.- if git fetch --quiet --depth=1 origin "$BASE_SHA"; then- if git diff --name-only "$BASE_SHA" HEAD \- | grep -qE '^(apps/storybook/stories/RichTextEditor\.(markdownParity|stories)|packages/richtext/src/__tests__/(MarkdownParitySandbox|RichTextView|RichTextEditorToolbar))'; then- should_run=true- fi- else+ # The sandbox's stories belong to no package, so compare the file+ # names the PR changes since its merge base.+ # The helper comes from the base, since an older PR head lacks it.+ # Exit 1 means out of scope; 0 in scope; 2 unknown, so run.+ scope=2+ helper="$(mktemp)"+ if git fetch --quiet --no-tags --depth=1 origin "$BASE_SHA" \+ && git show "$BASE_SHA:.github/scripts/pr-scope-touches.sh" > "$helper"; then+ scope=0+ bash "$helper" "$BASE_SHA" \+ '^(apps/storybook/stories/RichTextEditor\.(markdownParity|stories)|packages/richtext/src/__tests__/(MarkdownParitySandbox|RichTextView|RichTextEditorToolbar))' || scope=$?+ fi+ if [ "$scope" != 1 ]; thenshould_run=truefifi@@ -1490,14 +1495,19 @@ jobs:case ",${COMPONENTS}," in*,core/ToggleButton,*) should_run=true ;;esac- # The contract belongs to no package, so compare file names with the- # base. If the base cannot be fetched, run the evidence.- if git fetch --quiet --depth=1 origin "$BASE_SHA"; then- if git diff --name-only "$BASE_SHA" HEAD \- | grep -qE '^internal/a11y-spec/src/patterns/toggle-button'; then- should_run=true- fi- else+ # The contract belongs to no package, so compare the file names the+ # PR changes since its merge base.+ # The helper comes from the base, since an older PR head lacks it.+ # Exit 1 means out of scope; 0 in scope; 2 unknown, so run.+ scope=2+ helper="$(mktemp)"+ if git fetch --quiet --no-tags --depth=1 origin "$BASE_SHA" \+ && git show "$BASE_SHA:.github/scripts/pr-scope-touches.sh" > "$helper"; then+ scope=0+ bash "$helper" "$BASE_SHA" \+ '^internal/a11y-spec/src/patterns/toggle-button' || scope=$?+ fi+ if [ "$scope" != 1 ]; thenshould_run=truefiecho "should_run=${should_run}" >> "$GITHUB_OUTPUT"@@ -1535,13 +1545,18 @@ jobs:run: |should_run=false# Layout and the contract are matched by file name so every Layout- # region component is covered. If the base cannot be fetched, run.- if git fetch --quiet --depth=1 origin "$BASE_SHA"; then- if git diff --name-only "$BASE_SHA" HEAD \- | grep -qE '^(internal/a11y-spec/src/patterns/landmark|packages/core/src/Layout/|apps/storybook/stories/LayoutLandmarkA11y)'; then- should_run=true- fi- else+ # region component is covered.+ # The helper comes from the base, since an older PR head lacks it.+ # Exit 1 means out of scope; 0 in scope; 2 unknown, so run.+ scope=2+ helper="$(mktemp)"+ if git fetch --quiet --no-tags --depth=1 origin "$BASE_SHA" \+ && git show "$BASE_SHA:.github/scripts/pr-scope-touches.sh" > "$helper"; then+ scope=0+ bash "$helper" "$BASE_SHA" \+ '^(internal/a11y-spec/src/patterns/landmark|packages/core/src/Layout/|apps/storybook/stories/LayoutLandmarkA11y)' || scope=$?+ fi+ if [ "$scope" != 1 ]; thenshould_run=truefiecho "should_run=${should_run}" >> "$GITHUB_OUTPUT"@@ -1579,14 +1594,18 @@ jobs:run: |should_run=false# Link and the contract are matched by file name so every Link source- # file, including LinkProvider, is covered. If the base cannot be- # fetched, run.- if git fetch --quiet --depth=1 origin "$BASE_SHA"; then- if git diff --name-only "$BASE_SHA" HEAD \- | grep -qE '^(internal/a11y-spec/src/patterns/link\.|packages/core/src/Link/|apps/storybook/stories/LinkA11y)'; then- should_run=true- fi- else+ # file, including LinkProvider, is covered.+ # The helper comes from the base, since an older PR head lacks it.+ # Exit 1 means out of scope; 0 in scope; 2 unknown, so run.+ scope=2+ helper="$(mktemp)"+ if git fetch --quiet --no-tags --depth=1 origin "$BASE_SHA" \+ && git show "$BASE_SHA:.github/scripts/pr-scope-touches.sh" > "$helper"; then+ scope=0+ bash "$helper" "$BASE_SHA" \+ '^(internal/a11y-spec/src/patterns/link\.|packages/core/src/Link/|apps/storybook/stories/LinkA11y)' || scope=$?+ fi+ if [ "$scope" != 1 ]; thenshould_run=truefiecho "should_run=${should_run}" >> "$GITHUB_OUTPUT"