ci: release changelogs since the previous published release, and workflow hardening (#8341)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Mohammed Al Sahaf <msaa1990@gmail.com>
(cherry picked from commit 468154121d)
This commit is contained in:
Francis Lavoie 2026-11-03 18:07:13 -05:00
parent b01a31c635
commit 55996dfe05
No known key found for this signature in database
8 changed files with 636 additions and 380 deletions

60
.github/scripts/changelog-commits.sh vendored Executable file
View file

@ -0,0 +1,60 @@
#!/usr/bin/env bash
# Prints the commits that are new in <target> compared to the release <base>,
# oldest first, one SHA per line.
#
# Usage: changelog-commits.sh <base> <target>
#
# When <base> is an ancestor of <target>, that's simply base..target. When it
# isn't (e.g. base is a 2.11.x release cut from a maintenance branch and
# target is on master), base..target also contains the master commits whose
# fixes were already cherry-picked into the base release. Those are skipped
# when any of the following holds:
#
# 1. The commit has the same patch ID as a commit on the base side
# (a clean cherry-pick).
# 2. Cherry-picking it onto the base would change nothing, but
# cherry-picking it onto the merge base would. That is, the base
# already has the change, and it got there after the branches split.
# This catches clean picks whose lines were changed again later. The
# merge-base condition keeps a revert of something the base never had
# (which would also change nothing) in the list.
# 3. Its subject ends with a PR number, e.g. "(#1234)", and a commit on
# the base side has the identical subject. This catches backports that
# had to be adapted to apply. Subjects without a PR number are never
# matched this way: unrelated commits can share one (for example
# GitHub's "Merge commit from fork" for security advisories).
set -euo pipefail
base=$(git rev-parse --verify "${1:?usage: $0 <base> <target>}^{commit}")
target=$(git rev-parse --verify "${2:?usage: $0 <base> <target>}^{commit}")
if git merge-base --is-ancestor "$base" "$target"; then
git rev-list --reverse "$base..$target"
exit 0
fi
merge_base=$(git merge-base "$base" "$target")
base_tree=$(git rev-parse "$base^{tree}")
merge_base_tree=$(git rev-parse "$merge_base^{tree}")
backported_subjects=$(git log --format=%s "$merge_base..$base" | grep -E '\(#[0-9]+\)$' || true)
# picking_changes_nothing <commit> <onto> <onto-tree>
picking_changes_nothing() {
local result
if ! result=$(git merge-tree --write-tree --merge-base="$1^" "$2" "$1" 2>/dev/null); then
return 1 # conflicts, so it would change something
fi
[ "$(head -n 1 <<< "$result")" = "$3" ]
}
for commit in $(git rev-list --reverse --cherry-pick --right-only "$base...$target"); do
subject=$(git log -1 --format=%s "$commit")
if [[ $subject =~ \(#[0-9]+\)$ ]] && grep -qxF -- "$subject" <<< "$backported_subjects"; then
continue
fi
if picking_changes_nothing "$commit" "$base" "$base_tree" &&
! picking_changes_nothing "$commit" "$merge_base" "$merge_base_tree"; then
continue
fi
echo "$commit"
done

41
.github/scripts/previous-release.sh vendored Executable file
View file

@ -0,0 +1,41 @@
#!/usr/bin/env bash
# Prints the tag of the highest published (non-draft) GitHub release whose
# version is lower than the given one, or nothing if there is none.
#
# Usage: previous-release.sh <version>
#
# Release changelogs should cover everything since the last release users
# actually received. "The last tag" is not good enough:
#
# - When a release attempt is abandoned after tagging, the Go module proxy
# has usually cached the tag, so it can't be deleted and reused, and the
# next attempt gets a new patch number. Those abandoned tags never get a
# published release, so they are skipped here.
# - Releases can be cut from maintenance branches (e.g. 2.11), so the
# previous release isn't necessarily an ancestor of the new one. Picking
# by version means 2.12.0 compares against the latest 2.11.x, and a 2.11.x
# released after 2.12.0 compares against the previous 2.11.x.
#
# Requires GH_TOKEN and GITHUB_REPOSITORY in the environment.
set -euo pipefail
version=${1:?usage: $0 <version>}
# sort -V orders "2.12.0-beta.1" after "2.12.0", but with the prerelease
# separator replaced by "~" (which sorts before everything, even the end of
# the string) it orders like semver.
sort_key() {
printf '%s' "${1#v}" | sed 's/-/~/'
}
tags=$(gh api --paginate "repos/${GITHUB_REPOSITORY}/releases" --jq '.[] | select(.draft | not) | .tag_name')
{
# The new version itself, marked by an empty tag column.
printf '%s\t\n' "$(sort_key "$version")"
grep -E '^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$' <<< "$tags" | while IFS= read -r tag; do
if [ "$tag" != "$version" ]; then
printf '%s\t%s\n' "$(sort_key "$tag")" "$tag"
fi
done
} | sort -t $'\t' -k1,1V | awk -F '\t' '$2 == "" { print prev; exit } { prev = $2 }'

43
.github/scripts/release-notes.sh vendored Executable file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env bash
# Prints GitHub's generated release notes for <tag> (What's Changed, New
# Contributors, Full Changelog), compared against <previous-tag>, without
# the PRs that already shipped in <previous-tag>.
#
# Usage: release-notes.sh <tag> <target-commit> [previous-tag]
#
# GitHub's generator lists every PR in previous...tag. When the previous
# release was cut from a maintenance branch, that includes the master PRs
# whose fixes were cherry-picked into it, so those are removed here, using
# the same rules as changelog-commits.sh. Only lines ending in a PR link can
# be removed; anything else is left alone.
#
# Requires GH_TOKEN and GITHUB_REPOSITORY in the environment.
set -euo pipefail
tag=${1:?usage: $0 <tag> <target-commit> [previous-tag]}
target=${2:?usage: $0 <tag> <target-commit> [previous-tag]}
previous=${3:-}
args=(-f "tag_name=$tag" -f "target_commitish=$(git rev-parse --verify "$target^{commit}")")
if [ -n "$previous" ]; then
args+=(-f "previous_tag_name=$previous")
fi
notes=$(gh api -X POST "repos/${GITHUB_REPOSITORY}/releases/generate-notes" "${args[@]}" --jq .body)
if [ -z "$previous" ]; then
printf '%s\n' "$notes"
exit 0
fi
scripts=$(dirname "$0")
kept=$("$scripts/changelog-commits.sh" "$previous" "$target")
skipped_prs=$(git rev-list "$previous..$target" | { grep -vxF -f <(printf '%s\n' "$kept") || true; } |
xargs -r git log --no-walk --format=%s | sed -n 's/.*(#\([0-9][0-9]*\))$/\1/p' | sort -u)
if [ -z "$skipped_prs" ]; then
printf '%s\n' "$notes"
exit 0
fi
pattern="/pull/($(paste -sd '|' <<< "$skipped_prs"))\$"
printf '%s\n' "$notes" | tr -d '\r' | grep -vE -- "$pattern"

121
.github/scripts/release-proposal.js vendored Normal file
View file

@ -0,0 +1,121 @@
// Shared release proposal logic for the release workflows, loaded from
// actions/github-script with require(). Keeping it in one place makes the
// approval tracker and the release job agree on what an approved proposal is.
//
// A proposal is a draft PR labelled "release-proposal", titled exactly
// "Release Proposal: <version>", whose head commit message carries a
// "Target-Commit: <full sha>" trailer. The trailer is written by the
// Release Proposal workflow; unlike the PR body, it can't be changed
// without pushing a new commit, which makes earlier approvals stale.
'use strict';
const VERSION_RE = /^v\d+\.\d+\.\d+(-[0-9A-Za-z.]+)?$/;
const TARGET_COMMIT_RE = /^Target-Commit: ([0-9a-f]{40})$/m;
const REQUIRED_APPROVALS = 2;
function proposalTitle(version) {
return `Release Proposal: ${version}`;
}
function validateVersion(version) {
if (!VERSION_RE.test(version)) {
throw new Error(`invalid release version ${JSON.stringify(version)}`);
}
}
// parseMaintainers splits the MAINTAINER_LOGINS secret (comma or semicolon
// separated) into lowercased logins. GitHub logins are case-insensitive.
function parseMaintainers(raw) {
return (raw || '')
.split(/[,;]/)
.map(login => login.trim().toLowerCase())
.filter(login => login.length > 0);
}
// findOpenProposals returns every open release proposal for the version.
async function findOpenProposals({ github, context, version }) {
validateVersion(version);
const prs = await github.paginate(github.rest.pulls.list, {
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
per_page: 100,
});
return prs.filter(pr =>
pr.title === proposalTitle(version) &&
pr.labels.some(label => label.name === 'release-proposal'));
}
// targetCommit reads the Target-Commit trailer from the proposal's head commit.
async function targetCommit({ github, context, pr }) {
const { data: commit } = await github.rest.git.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: pr.head.sha,
});
const match = commit.message.match(TARGET_COMMIT_RE);
if (!match) {
throw new Error(`proposal #${pr.number} head commit ${pr.head.sha} has no Target-Commit trailer`);
}
return match[1];
}
// maintainerApprovals returns the maintainers whose latest review approves
// the proposal's current head commit. Plain comments don't change a
// reviewer's verdict, and approvals of an earlier head commit don't count.
async function maintainerApprovals({ github, context, pr, maintainers }) {
const reviews = await github.paginate(github.rest.pulls.listReviews, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
per_page: 100,
});
const latest = new Map();
for (const review of reviews) {
if (!review.user || !['APPROVED', 'CHANGES_REQUESTED', 'DISMISSED'].includes(review.state)) {
continue;
}
const login = review.user.login.toLowerCase();
const previous = latest.get(login);
if (!previous || new Date(review.submitted_at) > new Date(previous.submitted_at)) {
latest.set(login, review);
}
}
return [...latest.entries()]
.filter(([login, review]) =>
maintainers.includes(login) &&
review.state === 'APPROVED' &&
review.commit_id === pr.head.sha)
.map(([, review]) => review.user.login);
}
// checkProposal finds the single open proposal for the version and returns
// it with its target commit and maintainer approvals. It throws if there
// isn't exactly one proposal or the maintainer list is empty, so callers
// fail closed.
async function checkProposal({ github, context, version, maintainerLogins }) {
const maintainers = parseMaintainers(maintainerLogins);
if (maintainers.length === 0) {
throw new Error('MAINTAINER_LOGINS is empty; cannot count approvals');
}
const proposals = await findOpenProposals({ github, context, version });
if (proposals.length !== 1) {
throw new Error(`expected exactly one open PR titled "${proposalTitle(version)}" with the release-proposal label, found ${proposals.length}`);
}
const pr = proposals[0];
const approvers = await maintainerApprovals({ github, context, pr, maintainers });
return {
pr,
targetCommit: await targetCommit({ github, context, pr }),
approvers,
approved: approvers.length >= REQUIRED_APPROVALS,
};
}
module.exports = {
REQUIRED_APPROVALS,
checkProposal,
findOpenProposals,
proposalTitle,
validateVersion,
};