From b22ca1a58e10402c0692ea0081265031e726b5a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 17 Nov 2026 16:55:33 +0000 Subject: [PATCH] fix(release): count every pusher of a release, need a second maintainer, date approvals (#8076) * test(release): a release's approver must not have pushed it, and a second maintainer must have * fix(release): count every pusher of a release, need a second maintainer, date approvals * docs(release): who may approve a stable release, and the second maintainer it needs * test(release): a push the activity log records with no account stops the release * fix(release): a push recorded with no account stops the release --- .github/workflows/publish.yml | 34 +++++++- docs/contributing/release-channels.mdx | 2 +- scripts/publish-workflow.test.mjs | 108 ++++++++++++++++++++++--- 3 files changed, 127 insertions(+), 17 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 80c663f0a9..49cb48b459 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -95,7 +95,7 @@ jobs: | select(.merged_at != null and .base.ref == "main" and .merge_commit_sha == $sha) | select(.head.repo.full_name == $repo) | select(.head.ref | test("^release/v[0-9]+[.][0-9]+[.][0-9]+$")) - | [.head.ref, .number, .head.sha, .user.login, $sha] | @tsv' "$RUNNER_TEMP/pulls.json")" + | [.head.ref, .number, .head.sha, .user.login, .user.type, $sha] | @tsv' "$RUNNER_TEMP/pulls.json")" # A release commit GitHub has not linked to its PR yet must fail, where a rerun recovers it, not skip. if [ -z "$found" ] && [[ "$subject" =~ ^chore:\ release\ v[0-9] ]]; then echo "::error::$commit reads as a release commit but merges no release PR from this repo; rerun once GitHub lists it" @@ -112,19 +112,42 @@ jobs: echo "::error::$SHA is the merge of more than one release PR: $(cut -f1 <<<"$heads" | tr '\n' ' ')" exit 1 fi - IFS=$'\t' read -r ref number head author release <<<"$heads" + IFS=$'\t' read -r ref number head author author_type release <<<"$heads" # Branch protection can be bypassed, so a maintainer who did not open, write or push the release must approve # its final commit, and no maintainer's or app's change request may stand. Anyone can review a public repo: # a person's review counts only with write access. Each reviewer's latest verdict counts. + gh api "repos/$REPO/pulls/$number" > "$RUNNER_TEMP/pull.json" gh api --paginate "repos/$REPO/pulls/$number/reviews" > "$RUNNER_TEMP/reviews.json" gh api --paginate "repos/$REPO/pulls/$number/commits" > "$RUNNER_TEMP/commits.json" gh api --paginate "repos/$REPO/issues/$number/events" > "$RUNNER_TEMP/events.json" + # Commit names can be anyone's, so every account that pushed the branch is a writer too. + gh api --paginate "repos/$REPO/activity?ref=refs/heads/$ref&per_page=100" > "$RUNNER_TEMP/activity.json" + listed="$(jq -s 'add | length' "$RUNNER_TEMP/commits.json")" + [ "$listed" = "$(jq .commits "$RUNNER_TEMP/pull.json")" ] || + { echo "::error::#$number has more commits than GitHub lists ($listed), so who wrote it is unknown"; exit 1; } + jq -s -r 'add | .[] | select(.activity_type != "branch_deletion") | [.actor.login, .actor.type] | @tsv' \ + "$RUNNER_TEMP/activity.json" > "$RUNNER_TEMP/pushers.tsv" + [ -s "$RUNNER_TEMP/pushers.tsv" ] || + { echo "::error::no push to $ref is recorded, so who pushed it is unknown"; exit 1; } + ! cut -f1 "$RUNNER_TEMP/pushers.tsv" | grep -qx '' || + { echo "::error::a push to $ref is recorded with no account, so who pushed it is unknown"; exit 1; } { echo "$author" + cut -f1 "$RUNNER_TEMP/pushers.tsv" jq -s -r 'add | .[] | .author.login?, .committer.login? | values' "$RUNNER_TEMP/commits.json" jq -s -r 'add | .[] | select(.event == "head_ref_force_pushed") | .actor.login? | values' \ "$RUNNER_TEMP/events.json" } > "$RUNNER_TEMP/writers.txt" + # A second person with write access opened or pushed it; none of them can be the approver. + second="" + for login in $({ [ "$author_type" != User ] || echo "$author"; awk -F'\t' '$2 == "User" { print $1 }' \ + "$RUNNER_TEMP/pushers.tsv"; } | sort -u); do + permission="$(gh api "repos/$REPO/collaborators/$login/permission" --jq .permission)" || + { echo "::error::could not read $login's permission on $REPO, so the release does not publish"; exit 1; } + case "$permission" in admin | write) second="$login" && break ;; esac + done + [ -n "$second" ] || + { echo "::error::no person with write access opened or pushed $ref besides its approver"; exit 1; } : > "$RUNNER_TEMP/maintainers.txt" for login in $(jq -s -r 'add | map(select(.user.type == "User" and (.state == "APPROVED" or .state == "CHANGES_REQUESTED"))) @@ -134,13 +157,16 @@ jobs: { echo "::error::could not read $login's permission on $REPO, so the release does not publish"; exit 1; } case "$permission" in admin | write) echo "$login" >> "$RUNNER_TEMP/maintainers.txt" ;; esac done - reviewed="$(jq -s -r --arg head "$head" \ + # An approval counts only after the PR's last base change, so it reviewed the diff into main. + based="$(jq -s -r 'add | map(select(.event == "base_ref_changed") | .created_at) | max // ""' \ + "$RUNNER_TEMP/events.json")" + reviewed="$(jq -s -r --arg head "$head" --arg based "$based" \ --rawfile maintainers "$RUNNER_TEMP/maintainers.txt" --rawfile writers "$RUNNER_TEMP/writers.txt" 'add | ($maintainers | split("\n")) as $maintainers | ($writers | split("\n")) as $writers | map(select(.state == "APPROVED" or .state == "CHANGES_REQUESTED")) | map(select(.user.type != "User" or (.user.login | IN($maintainers[])))) | group_by(.user.login) | map(max_by(.submitted_at)) - | any(.state == "APPROVED" and .commit_id == $head and .user.type == "User" + | any(.state == "APPROVED" and .commit_id == $head and .user.type == "User" and .submitted_at > $based and (.user.login | IN($writers[]) | not)) and all(.state != "CHANGES_REQUESTED")' \ "$RUNNER_TEMP/reviews.json")" diff --git a/docs/contributing/release-channels.mdx b/docs/contributing/release-channels.mdx index ab555edfc5..d73b52fed0 100644 --- a/docs/contributing/release-channels.mdx +++ b/docs/contributing/release-channels.mdx @@ -65,7 +65,7 @@ The publish workflow validates release channel boundaries before publishing: - Stable versions must publish with `latest`. - Prerelease versions must publish with the prerelease dist-tag, such as `alpha`. -- Stable releases must come from a merged `release/vX.Y.Z` PR whose final commit a maintainer with write access approved. That maintainer must not have opened the PR, or written, committed or force-pushed its commits. Reviews from accounts without write access count neither as approvals nor as change requests. +- Stable releases must come from a merged `release/vX.Y.Z` PR whose final commit a maintainer with write access approved, after the PR's last base change. That maintainer must not have opened the PR, pushed to its branch (any push the branch's activity log records), or appear as an author or committer of its commits. A second person with write access must have opened the PR or pushed to its branch. Reviews from accounts without write access count neither as approvals nor as change requests. - Prerelease tags must point at a commit already on `main`, `next` or `alpha`. - Only repository admins may push prerelease (`v*-*`) tags. That is a prerelease's only review: a prerelease branch takes pushes without a PR, and no approval follows the tag. - Every publish runs in the `npm-publish` environment, which npm trusted publishing names; it waits for no approval. diff --git a/scripts/publish-workflow.test.mjs b/scripts/publish-workflow.test.mjs index 38363dc802..a99fd9ad5e 100644 --- a/scripts/publish-workflow.test.mjs +++ b/scripts/publish-workflow.test.mjs @@ -74,7 +74,7 @@ test("a push to main publishes only the merge of exactly one approved release PR const head = "c".repeat(40); const pull = (overrides = {}) => ({ number: 8000, - user: { login: "releaser" }, + user: { login: "releaser", type: "User" }, merged_at: "2026-11-17T02:24:00Z", merge_commit_sha: sha, base: { ref: "main" }, @@ -88,8 +88,9 @@ test("a push to main publishes only the merge of exactly one approved release PR submitted_at: `2026-11-17T0${hour}:00:00Z`, }); const approved = [review("reviewer", "APPROVED", 1)]; - // The fake answers PR 8000's reviews, commits and events, the push from FAKE_COMPARE, each commit's PR lookup from - // FAKE_PULLS_, and each reviewer's permission from FAKE_PERMISSIONS; the lookup of FAKE_PERMISSION_FAILS fails. + // The fake answers PR 8000, its reviews, commits and events, the branch's activity, the push from FAKE_COMPARE, + // each commit's PR lookup from FAKE_PULLS_, and each login's permission from FAKE_PERMISSIONS (login=permission + // lines); the lookup of FAKE_PERMISSION_FAILS fails. writeFileSync( join(bin, "gh"), `#!/bin/sh @@ -97,6 +98,8 @@ case "$*" in *pulls/8000/reviews*) printf '%s' "$FAKE_REVIEWS" ;; *pulls/8000/commits*) printf '%s' "$FAKE_COMMITS" ;; *issues/8000/events*) printf '%s' "$FAKE_EVENTS" ;; + *pulls/8000) printf '%s' "$FAKE_PULL" ;; + *activity\\?ref=refs/heads/release/v0.9.61\\&*) printf '%s' "$FAKE_ACTIVITY" ;; *compare/*) printf '%s' "$FAKE_COMPARE" ;; *commits/*/pulls*) c="$(printf '%s' "$*" | sed 's#.*commits/\\([0-9a-f]*\\)/pulls.*#\\1#')" @@ -114,7 +117,11 @@ esac author: { login: author }, committer: { login: committer }, }); - const permissions = "releaser=admin\nreviewer=write\nother=admin\noutsider=read"; + const permissions = "releaser=admin\nreviewer=write\nother=admin\noutsider=read\nhelper=read"; + const pushBy = (login, type = "User", activity_type = "push") => ({ + activity_type, + actor: { login, type }, + }); chmodSync(join(bin, "gh"), 0o755); const main = { REF_TYPE: "branch", REF_NAME: "main" }; /** A push of `commits` (oldest first, each `{ sha, message, pulls }`) whose last commit is the run's sha. */ @@ -127,9 +134,20 @@ esac commits.map(({ sha: c, pulls }) => [`FAKE_PULLS_${c}`, JSON.stringify(pulls)]), ), }); - const run = (pulls, ref = main, reviews = approved, history = {}) => { + /** A release PR's history: one commit and one push by its opener, unless `history` says otherwise. */ + const historyOf = (pulls, ref, history) => ({ + commits: [commit("releaser", "web-flow")], + push: [{ sha, message: ref.HEAD_MESSAGE ?? "fix: a change", pulls }], + before: "e".repeat(40), + activity: [pushBy("releaser", "User", "branch_creation"), pushBy("releaser")], + events: [], + permissions, + permissionFails: "", + ...history, + }); + const run = (pulls, ref = main, reviews = approved, given = {}) => { + const history = historyOf(pulls, ref, given); const out = join(bin, "out"); - const commits = history.push ?? [{ sha, message: ref.HEAD_MESSAGE ?? "fix: a change", pulls }]; writeFileSync(out, ""); const result = runStep(step, { cwd: bin, @@ -138,13 +156,15 @@ esac SHA: sha, RUNNER_TEMP: bin, GITHUB_OUTPUT: out, - BEFORE: history.before ?? "e".repeat(40), - ...push(commits, history.total), + BEFORE: history.before, + ...push(history.push, history.total), FAKE_REVIEWS: JSON.stringify(reviews), - FAKE_COMMITS: JSON.stringify(history.commits ?? [commit("releaser", "web-flow")]), - FAKE_EVENTS: JSON.stringify(history.events ?? []), - FAKE_PERMISSIONS: history.permissions ?? permissions, - FAKE_PERMISSION_FAILS: history.permissionFails ?? "", + FAKE_COMMITS: JSON.stringify(history.commits), + FAKE_PULL: JSON.stringify({ commits: history.listed ?? history.commits.length }), + FAKE_ACTIVITY: JSON.stringify(history.activity), + FAKE_EVENTS: JSON.stringify(history.events), + FAKE_PERMISSIONS: history.permissions, + FAKE_PERMISSION_FAILS: history.permissionFails, ...ref, }, path: `${bin}:${process.env.PATH}`, @@ -256,9 +276,73 @@ esac "the approver force-pushed the branch", { events: [{ event: "head_ref_force_pushed", actor: { login: "reviewer" } }] }, ], + [ + "the approver pushed commits written under another name", + { activity: [pushBy("releaser", "User", "branch_creation"), pushBy("reviewer")] }, + ], + [ + "the approver pushed commits with no account behind them", + { + commits: [{ author: null, committer: null }], + activity: [pushBy("releaser", "User", "branch_creation"), pushBy("reviewer")], + }, + ], + [ + "the approver created the branch", + { activity: [pushBy("reviewer", "User", "branch_creation")] }, + ], + ["more commits than GitHub lists", { listed: 251 }], + ["no push to the branch was recorded", { activity: [] }], + [ + "a push with no recorded account", + { + activity: [ + pushBy("releaser", "User", "branch_creation"), + { activity_type: "push", actor: null }, + ], + }, + ], ]) { assert.equal(run([pull()], main, approved, history).status, 1, why); } + // A second person with write access opened or pushed the release, besides the approver. + const openedBy = (login, type = "User") => pull({ user: { login, type } }); + for (const [why, pulls, history] of [ + [ + "an opener without write access who pushed", + [openedBy("helper")], + { activity: [pushBy("helper")] }, + ], + [ + "an app opened and pushed it", + [openedBy("release-app[bot]", "Bot")], + { activity: [pushBy("release-app[bot]", "Bot")] }, + ], + [ + "an opener without write access and only an app's push", + [openedBy("helper")], + { activity: [pushBy("ci[bot]", "Bot")] }, + ], + ]) { + assert.equal(run(pulls, main, approved, history).status, 1, why); + } + assert.equal( + run([openedBy("helper")], main, approved, { activity: [pushBy("releaser")] }).status, + 0, + "a maintainer's push is the second person when the opener has no write access", + ); + // An approval counts only if the base was main when it was given and has not changed since. + const rebased = { events: [{ event: "base_ref_changed", created_at: "2026-11-17T03:00:00Z" }] }; + assert.equal( + run([pull()], main, approved, rebased).status, + 1, + "an approval given before the base changed to main", + ); + assert.equal( + run([pull()], main, [review("reviewer", "APPROVED", 4)], rebased).status, + 0, + "an approval given after the last base change", + ); const reapproved = [ review("reviewer", "CHANGES_REQUESTED", 1), review("reviewer", "APPROVED", 2),