Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
REF_TYPE: ${{ github.ref_type }}
REF_NAME: ${{ github.ref_name }}
HEAD_MESSAGE: ${{ github.event.head_commit.message }}
BEFORE: ${{ github.event.before }}
run: |
if [ "$REF_TYPE" = "tag" ]; then
{
Expand All @@ -77,26 +78,41 @@ jobs:
} >> "$GITHUB_OUTPUT"
exit 0
fi
gh api "repos/$REPO/commits/$SHA/pulls" > "$RUNNER_TEMP/pulls.json"
heads="$(jq -r --arg sha "$SHA" --arg repo "$REPO" '.[]
| 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] | @tsv' "$RUNNER_TEMP/pulls.json")"
if [ -z "$heads" ]; then
# The merge queue can land several PRs in one push, and this runs once, for the last: every commit the push
# brought is read, and a release publishes its own merge commit, never a later one.
if [[ "$BEFORE" =~ ^0*$ ]]; then
jq -n --arg sha "$SHA" --arg message "$HEAD_MESSAGE" \
'{total_commits: 1, commits: [{sha: $sha, commit: {message: $message}}]}' > "$RUNNER_TEMP/push.json"
else
gh api "repos/$REPO/compare/$BEFORE...$SHA" > "$RUNNER_TEMP/push.json"
fi
[ "$(jq '.commits | length' "$RUNNER_TEMP/push.json")" = "$(jq .total_commits "$RUNNER_TEMP/push.json")" ] ||
{ echo "::error::the push to $SHA has more commits than GitHub lists, so a release in it could be missed"; exit 1; }
heads=""
while IFS=$'\t' read -r -u 3 commit subject; do
gh api "repos/$REPO/commits/$commit/pulls" > "$RUNNER_TEMP/pulls.json"
found="$(jq -r --arg sha "$commit" --arg repo "$REPO" '.[]
| 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")"
# A release commit GitHub has not linked to its PR yet must fail, where a rerun recovers it, not skip.
if [[ "${HEAD_MESSAGE%%$'\n'*}" =~ ^chore:\ release\ v[0-9] ]]; then
echo "::error::$SHA reads as a release commit but merges no release PR from this repo; rerun once GitHub lists it"
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"
exit 1
fi
echo "$SHA merges no release PR; nothing to publish"
heads+="${found:+$found$'\n'}"
done 3< <(jq -r '.commits[] | [.sha, (.commit.message | split("\n")[0])] | @tsv' "$RUNNER_TEMP/push.json")
heads="${heads%$'\n'}"
if [ -z "$heads" ]; then
echo "the push to $SHA merges no release PR; nothing to publish"
exit 0
fi
if [ "$(wc -l <<<"$heads")" -ne 1 ]; then
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 <<<"$heads"
IFS=$'\t' read -r ref number head author 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.
Expand Down Expand Up @@ -134,7 +150,7 @@ jobs:
exit 1
fi
{
echo "sha=$SHA"
echo "sha=$release"
echo "version=${ref#release/v}"
echo "source=release-pr"
echo "ref=$ref"
Expand Down
51 changes: 48 additions & 3 deletions scripts/publish-workflow.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,20 @@ 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 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.
// The fake answers PR 8000's reviews, commits and events, the push from FAKE_COMPARE, each commit's PR lookup from
// FAKE_PULLS_<sha>, and each reviewer's permission from FAKE_PERMISSIONS; the lookup of FAKE_PERMISSION_FAILS fails.
writeFileSync(
join(bin, "gh"),
`#!/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" ;;
*compare/*) printf '%s' "$FAKE_COMPARE" ;;
*commits/*/pulls*)
c="$(printf '%s' "$*" | sed 's#.*commits/\\([0-9a-f]*\\)/pulls.*#\\1#')"
eval "v=\\\${FAKE_PULLS_$c-}"
printf '%s' "\${v:-[]}" ;;
*collaborators/*/permission*)
login="$(printf '%s' "$*" | sed 's#.*collaborators/\\([^/]*\\)/permission.*#\\1#')"
[ "$login" != "$FAKE_PERMISSION_FAILS" ] || exit 1
Expand All @@ -112,8 +117,19 @@ esac
const permissions = "releaser=admin\nreviewer=write\nother=admin\noutsider=read";
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. */
const push = (commits, total = commits.length) => ({
FAKE_COMPARE: JSON.stringify({
total_commits: total,
commits: commits.map(({ sha: c, message }) => ({ sha: c, commit: { message } })),
}),
...Object.fromEntries(
commits.map(({ sha: c, pulls }) => [`FAKE_PULLS_${c}`, JSON.stringify(pulls)]),
),
});
const run = (pulls, ref = main, reviews = approved, history = {}) => {
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,
Expand All @@ -122,7 +138,8 @@ esac
SHA: sha,
RUNNER_TEMP: bin,
GITHUB_OUTPUT: out,
FAKE_PULLS: JSON.stringify(pulls),
BEFORE: history.before ?? "e".repeat(40),
...push(commits, history.total),
FAKE_REVIEWS: JSON.stringify(reviews),
FAKE_COMMITS: JSON.stringify(history.commits ?? [commit("releaser", "web-flow")]),
FAKE_EVENTS: JSON.stringify(history.events ?? []),
Expand Down Expand Up @@ -251,6 +268,34 @@ esac
0,
"a later approval answers its own change request",
);
// The merge queue can land the release with other PRs in one push; the run is for the push's last commit.
const release = "f".repeat(40);
const batched = (releasePulls, total) => ({
push: [
{ sha: release, message: "chore: release v0.9.61 (#8000)", pulls: releasePulls },
{ sha, message: "fix: a server change (#8001)", pulls: [] },
],
total,
});
assert.deepEqual(run([], main, approved, batched([pull({ merge_commit_sha: release })])), {
status: 0,
outputs: { sha: release, version: "0.9.61", source: "release-pr", ref: "release/v0.9.61" },
});
assert.equal(
run([], main, approved, batched([])).status,
1,
"a release commit below the tip that GitHub has not linked yet fails, never skips",
);
assert.equal(
run([], main, approved, batched([pull({ merge_commit_sha: release })], 300)).status,
1,
"a push with more commits than GitHub lists",
);
assert.deepEqual(
run([pull()], main, approved, { before: "0".repeat(40) }).outputs.sha,
sha,
"a push that created the branch reads only its last commit",
);
const second = pull({
head: { ref: "release/v0.9.62", repo: { full_name: "heygen-com/hyperframes" } },
});
Expand Down
Loading