fix(publish): a stable release needs a maintainer's approval, not any account's (#8062)

* test(publish): a stable release counts only maintainers' reviews, never the approver's own push

* fix(publish): a stable release needs a maintainer's approval, not any account's

* fix(publish): only approvers and change requesters are looked up, and a failed lookup always refuses

* style(publish): the release gate's comments and lines fit the 120-character limit
This commit is contained in:
Miguel Ángel
2026-11-17 09:33:45 +00:00
committed by GitHub
parent f97e289058
commit 4807799c4e
3 changed files with 88 additions and 10 deletions
+29 -6
View File
@@ -97,17 +97,40 @@ jobs:
exit 1
fi
IFS=$'\t' read -r ref number head author <<<"$heads"
# Branch protection can be bypassed, so the release itself requires a person other than its author to have
# approved its final commit, with no change request from anyone still standing. Each latest verdict counts.
# 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 --paginate "repos/$REPO/pulls/$number/reviews" > "$RUNNER_TEMP/reviews.json"
reviewed="$(jq -s -r --arg head "$head" --arg author "$author" 'add
| map(select(.user.login != $author and (.state == "APPROVED" or .state == "CHANGES_REQUESTED")))
gh api --paginate "repos/$REPO/pulls/$number/commits" > "$RUNNER_TEMP/commits.json"
gh api --paginate "repos/$REPO/issues/$number/events" > "$RUNNER_TEMP/events.json"
{
echo "$author"
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"
: > "$RUNNER_TEMP/maintainers.txt"
for login in $(jq -s -r 'add
| map(select(.user.type == "User" and (.state == "APPROVED" or .state == "CHANGES_REQUESTED")))
| map(.user.login) | unique[]' \
"$RUNNER_TEMP/reviews.json"); 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) echo "$login" >> "$RUNNER_TEMP/maintainers.txt" ;; esac
done
reviewed="$(jq -s -r --arg head "$head" \
--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 (.user.login | IN($writers[]) | not))
and all(.state != "CHANGES_REQUESTED")' \
"$RUNNER_TEMP/reviews.json")"
if [ "$reviewed" != "true" ]; then
echo "::error::$ref (#$number) has no approval of its final commit by someone else, or a change request stands"
echo "::error::$ref (#$number): no maintainer who did not write or push it approved its final commit," \
"or a change request stands"
exit 1
fi
{
+1 -1
View File
@@ -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, reviewed `release/vX.Y.Z` PR.
- 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.
- 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.
+58 -3
View File
@@ -88,14 +88,31 @@ 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 from FAKE_REVIEWS and the commit's PR lookup from FAKE_PULLS.
// The fake answers PR 8000's reviews, commits and events, the commit's PR lookup from FAKE_PULLS, and each
// reviewer's permission from FAKE_PERMISSIONS (login=permission lines); the lookup of FAKE_PERMISSION_FAILS fails.
writeFileSync(
join(bin, "gh"),
'#!/bin/sh\ncase "$*" in *pulls/8000/reviews*) printf \'%s\' "$FAKE_REVIEWS" ;; *) printf \'%s\' "$FAKE_PULLS" ;; esac\n',
`#!/bin/sh
case "$*" in
*pulls/8000/reviews*) printf '%s' "$FAKE_REVIEWS" ;;
*pulls/8000/commits*) printf '%s' "$FAKE_COMMITS" ;;
*issues/8000/events*) printf '%s' "$FAKE_EVENTS" ;;
*collaborators/*/permission*)
login="$(printf '%s' "$*" | sed 's#.*collaborators/\\([^/]*\\)/permission.*#\\1#')"
[ "$login" != "$FAKE_PERMISSION_FAILS" ] || exit 1
printf '%s\\n' "$FAKE_PERMISSIONS" | sed -n "s/^$login=//p" ;;
*) printf '%s' "$FAKE_PULLS" ;;
esac
`,
);
const commit = (author, committer = author) => ({
author: { login: author },
committer: { login: committer },
});
const permissions = "releaser=admin\nreviewer=write\nother=admin\noutsider=read";
chmodSync(join(bin, "gh"), 0o755);
const main = { REF_TYPE: "branch", REF_NAME: "main" };
const run = (pulls, ref = main, reviews = approved) => {
const run = (pulls, ref = main, reviews = approved, history = {}) => {
const out = join(bin, "out");
writeFileSync(out, "");
const result = runStep(step, {
@@ -107,6 +124,10 @@ test("a push to main publishes only the merge of exactly one approved release PR
GITHUB_OUTPUT: out,
FAKE_PULLS: JSON.stringify(pulls),
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 ?? "",
...ref,
},
path: `${bin}:${process.env.PATH}`,
@@ -187,6 +208,40 @@ test("a push to main publishes only the merge of exactly one approved release PR
]) {
assert.equal(run([pull()], main, reviews).status, 1, why);
}
// Anyone can review a public repo: only a reviewer with write access counts, either way.
assert.equal(
run([pull()], main, [review("outsider", "APPROVED", 1)]).status,
1,
"an approval from an account without write access",
);
assert.equal(
run([pull()], main, [...approved, review("outsider", "CHANGES_REQUESTED", 2)]).status,
0,
"a change request from an account without write access blocks nothing",
);
const outsiderObjects = [...approved, review("outsider", "CHANGES_REQUESTED", 2)];
assert.equal(
run([pull()], main, outsiderObjects, { permissionFails: "outsider" }).status,
1,
"a permission that cannot be read refuses the release, even for a review that would not count",
);
assert.equal(
run([pull()], main, [...approved, review("outsider", "COMMENTED", 2)], {
permissionFails: "outsider",
}).status,
0,
"a reviewer who only commented is not looked up",
);
for (const [why, history] of [
["the approver wrote the approved commit", { commits: [commit("reviewer", "web-flow")] }],
["the approver committed it", { commits: [commit("releaser", "reviewer")] }],
[
"the approver force-pushed the branch",
{ events: [{ event: "head_ref_force_pushed", actor: { login: "reviewer" } }] },
],
]) {
assert.equal(run([pull()], main, approved, history).status, 1, why);
}
const reapproved = [
review("reviewer", "CHANGES_REQUESTED", 1),
review("reviewer", "APPROVED", 2),