An open source design system that's fully customizable and agent ready
TypeScript
ci: the public text check reads the pull request's own commits (#10724)
Cindy Zhange9da998501cd
diff --git a/.github/scripts/check-public-text.mjs b/.github/scripts/check-public-text.mjsindex cde4257364..6f65d7e0f9 100644--- a/.github/scripts/check-public-text.mjs+++ b/.github/scripts/check-public-text.mjs@@ -141,12 +141,11 @@ function readArgs(argv) {return args;}+const SHA = /^[0-9a-f]{7,64}$/i;+/** Reads the range with git. Throws when it cannot be read whole. */export function readRange(base, head) {- if (- !/^[0-9a-f]{7,64}$/i.test(base ?? '') ||- !/^[0-9a-f]{7,64}$/i.test(head ?? '')- )+ if (!SHA.test(base ?? '') || !SHA.test(head ?? ''))throw new Error('--base and --head must be commit shas');const log = git(['log', '--format=%H%x00%B%x1e', `${base}..${head}`]);const commits = log@@ -158,6 +157,34 @@ export function readRange(base, head) {return {sha, message};});if (commits.length === 0) throw new Error('the range has no commits');+ return {commits, diff: readDiffBetween(base, head)};+}++/**+ * Reads exactly the pull request's commits from the commit list GitHub+ * reports (one `{"sha", "message"}` JSON object a line), and its added lines+ * from the diff between the base branch tip and the merge of the head into it.+ * A shallow clone cannot misplace the merge base here, so the base branch's+ * own commits are never read as the pull request's. Throws when the list is+ * empty or unreadable.+ */+export function readPullRequestRange(commitsText, diffBase, diffHead) {+ if (!SHA.test(diffBase ?? '') || !SHA.test(diffHead ?? ''))+ throw new Error('--diff-base and --diff-head must be commit shas');+ const commits = commitsText+ .split('\n')+ .filter(line => line.trim())+ .map(line => {+ const {sha, message} = JSON.parse(line);+ if (!SHA.test(sha ?? '') || typeof message !== 'string')+ throw new Error('a listed commit has no sha or message');+ return {sha, message};+ });+ if (commits.length === 0) throw new Error('the commit list is empty');+ return {commits, diff: readDiffBetween(diffBase, diffHead)};+}++function readDiffBetween(base, head) {// `--text` and `--no-textconv`: a path marked binary in `.gitattributes`// shows its added lines too. Renames are found, so a moved file shows only// the lines its move changed.@@ -177,7 +204,7 @@ export function readRange(base, head) {]);if (readDiff(diff).unreadable)throw new Error('a changed file has a path the check cannot read');- return {commits, diff};+ return diff;}/**@@ -199,7 +226,14 @@ function main() {let pr = {};try {digests = new Set(JSON.parse(fs.readFileSync(args.hashes, 'utf8')));- range = readRange(args.base, args.head);+ range =+ args['commits-json'] != null+ ? readPullRequestRange(+ fs.readFileSync(args['commits-json'], 'utf8'),+ args['diff-base'],+ args['diff-head'],+ )+ : readRange(args.base, args.head);if (args['pr-json'] != null)pr = readPullRequest(fs.readFileSync(args['pr-json'], 'utf8'));} catch (error) {diff --git a/.github/scripts/check-public-text.test.mjs b/.github/scripts/check-public-text.test.mjsindex 29f98c9f6b..f3a6d2bb53 100644--- a/.github/scripts/check-public-text.test.mjs+++ b/.github/scripts/check-public-text.test.mjs@@ -290,6 +290,112 @@ describe('the script on a real repository', () => {expect(run(base, head).status).toBe(0);});+ it("reads only the pull request's commits when the merge base read is too old", () => {+ const {commit, run, git, dir, hashes} = repo();+ // main: base, then two commits; one names an internal change id.+ const base = commit('a.md', 'a\n', 'base');+ git('checkout', '-q', '-b', 'topic');+ const own = commit('b.md', 'b\n', 'add b');+ git('checkout', '-q', 'main');+ commit('c.md', 'c\n', `main work (${changeId})`);+ const tip = commit('d.md', 'd\n', 'more main work');+ // The branch merges main, then GitHub merges the head into main's tip.+ git('checkout', '-q', 'topic');+ git('merge', '-q', '--no-edit', 'main');+ const head = git('rev-parse', 'HEAD');+ git('checkout', '-q', '--detach', tip);+ git('merge', '-q', '--no-edit', head);+ const merge = git('rev-parse', 'HEAD');+ // A shallow clone can resolve the merge base to the old branch point: the+ // range then holds main's own commits and their text.+ expect(run(base, head).status).toBe(1);+ // GitHub's list holds only the pull request's commits.+ const list = path.join(dir, 'commits.jsonl');+ fs.writeFileSync(+ list,+ [own, head]+ .map(sha =>+ JSON.stringify({sha, message: git('log', '-1', '--format=%B', sha)}),+ )+ .join('\n'),+ );+ const listed = spawnSync(+ process.execPath,+ [+ SCRIPT,+ '--base',+ base,+ '--head',+ head,+ '--commits-json',+ list,+ '--diff-base',+ tip,+ '--diff-head',+ merge,+ '--hashes',+ hashes,+ ],+ {cwd: dir, encoding: 'utf8'},+ );+ expect(listed.stdout).toContain('Checked 2 commit message(s)');+ expect(listed.status).toBe(0);+ });++ it("still finds the pull request's own text through the list", () => {+ const {commit, git, dir, hashes} = repo();+ const tip = commit('a.md', 'a\n', 'base');+ git('checkout', '-q', '-b', 'topic');+ const own = commit('b.md', `uses ${FAKE_NAME}\n`, `add b (${changeId})`);+ git('checkout', '-q', '--detach', tip);+ git('merge', '-q', '--no-edit', '--no-ff', own);+ const merge = git('rev-parse', 'HEAD');+ const list = path.join(dir, 'commits.jsonl');+ fs.writeFileSync(+ list,+ `${JSON.stringify({sha: own, message: `add b (${changeId})`})}\n`,+ );+ const result = spawnSync(+ process.execPath,+ [+ SCRIPT,+ '--commits-json',+ list,+ '--diff-base',+ tip,+ '--diff-head',+ merge,+ '--hashes',+ hashes,+ ],+ {cwd: dir, encoding: 'utf8'},+ );+ expect(result.status).toBe(1);+ expect(result.stdout).toContain(+ 'message has an internal change or task id',+ );+ expect(result.stdout).toContain('file=b.md,line=1,');+ // An empty list is unreadable, never a pass.+ fs.writeFileSync(list, '\n');+ expect(+ spawnSync(+ process.execPath,+ [+ SCRIPT,+ '--commits-json',+ list,+ '--diff-base',+ tip,+ '--diff-head',+ merge,+ '--hashes',+ hashes,+ ],+ {cwd: dir, encoding: 'utf8'},+ ).status,+ ).toBe(2);+ });+it('fails closed when the range is empty, unreadable or not a sha', () => {const {commit, run, hashes} = repo();const base = commit('a.md', 'a\n', 'base');@@ -365,11 +471,27 @@ describe('Lint runs the check (spec:AST-030 FR27)', () => {expect(step.run).not.toMatch(/--unshallow origin >/);});- it('checks the pull request head from its merge base, and fails without one', () => {+ it("reads the pull request's own commits from GitHub's full list, with the merge's diff", () => {+ expect(step.run).toContain(+ 'gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/commits?per_page=100"',+ );+ // Only a list that holds every commit the pull request reports counts.+ expect(step.run).toContain('[ "$(grep -c . "$COMMITS")" -eq "$EXPECTED" ]');+ expect(step.run).toContain(+ 'ARGS+=(--commits-json "$COMMITS" --diff-base "$BASE_TIP" --diff-head "$MERGE_SHA")',+ );+ expect(step.run).toContain('MERGE_SHA=$(git rev-parse HEAD)');+ });++ it('falls back to the merge base without the list, and fails closed without either', () => {expect(step.run).toContain('HEAD_SHA=$(git rev-parse HEAD^2)');expect(step.run).toContain('git merge-base "$BASE_TIP" "$HEAD_SHA"');- expect(step.run).toContain('--base "$MERGE_BASE" --head "$HEAD_SHA"');- expect(step.run).toMatch(/merge base was not found[^\n]*\n\s*exit 1/);+ expect(step.run).toContain(+ 'ARGS=(--base "${MERGE_BASE:-$BASE_TIP}" --head "$HEAD_SHA")',+ );+ expect(step.run).toMatch(+ /if \[ -z "\$MERGE_BASE" \] && \[ "\$LISTED" != true \]; then\n\s*echo "::error[^\n]*merge base could be read[^\n]*"\n\s*exit 1/,+ );});it('reads the current title and description through the API, and never prints them', () => {diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.ymlindex 69e53b8901..36ba54d7b8 100644--- a/.github/workflows/lint.yml+++ b/.github/workflows/lint.yml@@ -68,8 +68,8 @@ jobs:>> "$GITHUB_OUTPUT"fi- # Public text carries no internal identifier, host or name: each commit- # message and added line from the merge base to the head, and the title+ # Public text carries no internal identifier, host or name: each of the+ # pull request's own commit messages and added lines, and the title# and description as they read when the check runs, so a re-run reads an# edit (spec:AST-030 FR27). The base branch's copy of the check runs, so# a pull request cannot change its own check.@@ -98,23 +98,43 @@ jobs:fiHEAD_SHA=$(git rev-parse HEAD^2)BASE_TIP=$(git rev-parse HEAD^1)+ MERGE_SHA=$(git rev-parse HEAD)+ # The pull request's own commits come from GitHub's list of them, and+ # its added lines from the base tip to this merge, so a shallow clone+ # cannot read the base branch's commits as the pull request's. The+ # list is used only when it holds every commit the pull request+ # reports (GitHub lists at most 250).+ COMMITS="$CHECK_DIR/commits.jsonl"+ EXPECTED=$(node -e 'const p = require(process.argv[1]); process.stdout.write(String(p.commits ?? p.pull_request?.commits ?? ""))' "$CHECK_DIR/pull-request.json" 2>/dev/null || true)+ LISTED=false+ if [ -n "$EXPECTED" ] \+ && gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/commits?per_page=100" \+ --jq '.[] | {sha: .sha, message: .commit.message}' > "$COMMITS" 2>/dev/null \+ && [ "$(grep -c . "$COMMITS")" -eq "$EXPECTED" ]; then+ LISTED=true+ fiMERGE_BASE=$(git merge-base "$BASE_TIP" "$HEAD_SHA" 2>/dev/null || true)- # A branch far behind its base: deepen only the base branch and the- # pull request head, then, rarely, take the whole history.- if [ -z "$MERGE_BASE" ]; then+ # Without the list: a branch far behind its base deepens only the+ # base branch and the pull request head, then, rarely, takes the+ # whole history.+ if [ -z "$MERGE_BASE" ] && [ "$LISTED" != true ]; thengit fetch --no-tags --deepen=500 origin "$BASE_REF" "refs/pull/$PR_NUMBER/head" > /dev/null 2>&1 || trueMERGE_BASE=$(git merge-base "$BASE_TIP" "$HEAD_SHA" 2>/dev/null || true)fi- if [ -z "$MERGE_BASE" ]; then+ if [ -z "$MERGE_BASE" ] && [ "$LISTED" != true ]; thengit fetch --no-tags --unshallow origin "$BASE_REF" "refs/pull/$PR_NUMBER/head" > /dev/null 2>&1 || trueMERGE_BASE=$(git merge-base "$BASE_TIP" "$HEAD_SHA" 2>/dev/null || true)fi- if [ -z "$MERGE_BASE" ]; then- echo "::error title=Public text check::The merge base was not found, so the commits cannot be checked."+ if [ -z "$MERGE_BASE" ] && [ "$LISTED" != true ]; then+ echo "::error title=Public text check::Neither the pull request's commit list nor its merge base could be read, so the commits cannot be checked."exit 1fi+ ARGS=(--base "${MERGE_BASE:-$BASE_TIP}" --head "$HEAD_SHA")+ if [ "$LISTED" = true ]; then+ ARGS+=(--commits-json "$COMMITS" --diff-base "$BASE_TIP" --diff-head "$MERGE_SHA")+ finode "$CHECK_DIR/.github/scripts/check-public-text.mjs" \- --base "$MERGE_BASE" --head "$HEAD_SHA" \+ "${ARGS[@]}" \--hashes "$CHECK_DIR/public-text-hashes.json" \--pr-json "$CHECK_DIR/pull-request.json"diff --git a/docs/specs/AST-030/spec.md b/docs/specs/AST-030/spec.mdindex ab976637a5..ab688baaf6 100644--- a/docs/specs/AST-030/spec.md+++ b/docs/specs/AST-030/spec.md@@ -587,9 +587,7 @@ they are separately named.contributor's pull request still needs `ci:run` (FR17), and `main` and therelease-check cover Lab as before (DEC-21).- **FR27 — Lint fails a pull request whose public text carries an internal- reference.** Public text is each commit message and added line from the merge- base to the pull request head, and the pull request's title and description- as they read when the check runs. Generated text that no person writes, the+ reference.** Public text is each of the pull request's own commit messages and each line it adds, and the pull request's title and description as they read when the check runs. Generated text that no person writes, thelockfile and the digest list, is not read.The required Lint job runs the base branch's copy of the check, so a pullrequest cannot change the check that judges it. The check matches generic@@ -683,6 +681,7 @@ The nightly perf lane is the one scheduled measurement outside them (FR18).| FR24 | `ci-heavy-lanes.test.mjs` cadence cases | release-check 170 and 176 minutes old; the unit lane hourly beside it; weekday 06:37 Pacific inside the window in standard and daylight time; the pre-cut check already started; the tip already checked; a weekend morning; before 06:30 and after 07:30 | a release-check starts inside the 175-minute window, the pre-cut check is skipped or starts twice, or it runs on a weekend || FR26 | `ci-lab-only-routing.test.mjs`, `change-scope.test.mjs` and `test-scope-action.test.mjs` Lab-only cases | a Lab-only change, with and without a Changeset; a new Lab component with its routes, discovery entry and story; Lab beside core, charts, a core Storybook story, a route or discovery entry for another component, an entry-file diff GitHub leaves out, the docsite, the Sandbox, a workflow, the lockfile, root configuration, internal tooling or a spec record; a rename into or out of Lab; an incomplete list; a base classifier without the lane; each required context, its owners, the UI legs' Lab tests, the UI and Node guard tests, the repository guards, the Storybook build and Lab typecheck, the heavy suites with and without a label, lint and an agent push | a mixed change takes the Lab-only lane; a new Lab component's own entry files make it mixed, or another component's do not; a changed existing story, a story for a component the change does not carry, or a renamed entry file keeps the lane; the CLI official-packages check, the route-lock check when the lock changed, or the Storybook build does not run for one; a required context skips or fails, or passes without its notice; a Lab test, a guard test, a repository guard, the Lab typecheck or the Storybook build does not run; an unlabelled heavy suite, the docsite or the full typecheck runs, or a label's suite cannot audit Lab; lint is skipped || FR27 | `check-public-text.test.mjs` over the check and the Lint workflow step | each identifier shape; a hashed name and host made up in the test; ordinary text; a finding in a commit message, an added line and the description; a match before the merge base; an empty range, a bad sha, a missing commit and a missing digest file | a reference passes; the output holds the matched text; a match on the base branch fails a later pull request; an unreadable range passes; the step runs the pull request's own copy |+| FR27 | `check-public-text.test.mjs` commit-list cases | a pull request whose merge base resolves too far back in a shallow clone; its own commit with an internal reference | the check flags the base branch's commits, or passes the pull request's own reference || FR28 | `playwright-legs.test.mjs` creator, prefix and head-repository cases ("plans every WebKit spec only for the release tooling's interaction-check/ pull request") | release interaction-check PRs: the release tooling's pull request; the same prefix from another author or from a fork; the tooling account on another branch | A prefix-only, other-author or fork pull request gets the full WebKit scope, or the tooling's interaction-check pull request does not |## Decision log