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
126 changes: 89 additions & 37 deletions scripts/ci/release-only-diff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,63 +25,115 @@ function withoutVersion(text) {
}
}

/** The new version when the only difference between two manifest texts is `version`, else null. */
/** `{ from, to }` when the only difference between two manifest texts is `version`, else null. */
function versionBump(was, now) {
const a = was === null ? null : withoutVersion(was);
const b = now === null ? null : withoutVersion(now);
return a && b && sameJson(a.rest, b.rest) && a.version !== b.version ? b.version : null;
return a && b && sameJson(a.rest, b.rest) && a.version !== b.version
? { from: a.version, to: b.version }
: null;
}

/** Sorts changed paths into the release's own and the rest; before and after read a path at each end, or null. */
export function classify(paths, before, after) {
const other = [];
const versions = new Map();
const previous = new Set();
for (const path of paths) {
if (RELEASE_NOTES.test(path) || path === CHANGELOG) continue;
const bumped = MANIFESTS.has(basename(path)) ? versionBump(before(path), after(path)) : null;
if (bumped === null) other.push(path);
else versions.set(path, bumped);
else {
versions.set(path, bumped.to);
previous.add(bumped.from);
}
}
return { releaseOnly: versions.size > 0 && other.length === 0, other, versions };
return { releaseOnly: versions.size > 0 && other.length === 0, other, versions, previous };
}

/** The commits in `commits` ({ sha, subject }) that release notes should list but do not link. */
export function unlistedCommits(commits, notes) {
return commits.filter(({ sha, subject }) => !skipsChangelog(subject) && !notes.includes(sha));
}

/** Problems with a release that is otherwise only versions and notes; empty when it is consistent. */
export function verifyRelease({ versions }, after, commits = []) {
/** A conventional `type(scope)!:` subject. Only the `!` is read: this repo marks breaking changes in the subject. */
const BREAKING_SUBJECT = /^[a-z]+(?:\([^)]+\))?!:/;

/** The commits still in effect, newest first: a later commit in effect that names one as reverted takes it out. */
function inEffect(commits) {
const reverted = [];
return commits.filter(({ sha, body = "" }) => {
if (reverted.some((ref) => sha.startsWith(ref))) return false;
for (const [, ref] of body.matchAll(/This reverts commit ([0-9a-f]{7,40})/g))
reverted.push(ref);
return true;
});
}

/** The version a breaking change needs after `from`: the next minor below 1.0, the next major from 1.0 on. */
function breakingVersionAfter(from) {
const [major, minor] = from.split(".").map(Number);
return major === 0 ? `0.${minor + 1}.0` : `${major + 1}.0.0`;
}

/** Whether `to` is at least the version a breaking change needs after `from`. */
function allowsBreaking(from, to) {
const needed = breakingVersionAfter(from).split(".").map(Number);
const got = to.split(".").map((part) => Number.parseInt(part, 10));
for (let i = 0; i < 3; i++) if (got[i] !== needed[i]) return got[i] > needed[i];
return true;
}

/** The problem when an unreverted breaking change since the last release rides a bump below the one it needs. */
function breakingBumpProblems(from, version, commits) {
const breaking = inEffect(commits).find(({ subject }) => BREAKING_SUBJECT.test(subject));
if (!breaking || !from || allowsBreaking(from, version)) return [];
return [
`v${version} is too small a bump for a breaking change: ` +
`${breaking.sha.slice(0, 9)} ${breaking.subject}. ` +
`Release it as v${breakingVersionAfter(from)}, or revert the change first.`,
];
}

function notesProblems(version, notes, commits) {
if (notes === null) return [`releases/v${version}.md is missing`];
const problems = [];
const distinct = new Set(versions.values());
if (distinct.size !== 1) {
if (UNREVIEWED.some((marker) => notes.includes(marker)))
problems.push(`releases/v${version}.md still has the generated TODO or style note`);
const unlisted = unlistedCommits(commits, notes);
if (unlisted.length)
problems.push(
`manifests move to different versions: ${[...distinct].join(", ") || "none changed"}`,
`releases/v${version}.md misses ${unlisted.length} commit(s) merged since the notes were drafted: ` +
unlisted.map(({ sha, subject }) => `${sha.slice(0, 9)} ${subject}`).join("; ") +
". Link each in the notes, or rebase on main and rerun `bun run release:prepare` if main moved.",
);
return problems;
}
const [version] = distinct;
const notes = after(`releases/v${version}.md`);
if (notes === null) problems.push(`releases/v${version}.md is missing`);
else {
if (UNREVIEWED.some((marker) => notes.includes(marker)))
problems.push(`releases/v${version}.md still has the generated TODO or style note`);
const unlisted = unlistedCommits(commits, notes);
if (unlisted.length)
problems.push(
`releases/v${version}.md misses ${unlisted.length} commit(s) merged since the notes were drafted: ` +
unlisted.map(({ sha, subject }) => `${sha.slice(0, 9)} ${subject}`).join("; ") +
". Link each in the notes, or rebase on main and rerun `bun run release:prepare` if main moved.",
);
}
const changelog = after(CHANGELOG);
if (changelog === null || !changelog.includes(`label="HyperFrames v${version}"`))
problems.push(`${CHANGELOG} has no entry for v${version}`);
else if (UNREVIEWED.some((marker) => changelog.includes(marker)))
problems.push(`${CHANGELOG} still has the generated TODO or style note`);
return problems;
}

function changelogProblems(version, changelog) {
if (changelog === null || !changelog.includes(`label="HyperFrames v${version}"`))
return [`${CHANGELOG} has no entry for v${version}`];
if (UNREVIEWED.some((marker) => changelog.includes(marker)))
return [`${CHANGELOG} still has the generated TODO or style note`];
return [];
}

/** Problems with a release that is otherwise only versions and notes; empty when it is consistent. */
export function verifyRelease({ versions, previous = new Set() }, after, commits = []) {
const distinct = new Set(versions.values());
if (distinct.size !== 1)
return [`manifests move to different versions: ${[...distinct].join(", ") || "none changed"}`];
if (previous.size > 1)
return [`manifests move from different versions: ${[...previous].join(", ")}`];
const [version] = distinct;
const [from] = previous;
return [
...breakingBumpProblems(from, version, commits),
...notesProblems(version, after(`releases/v${version}.md`), commits),
...changelogProblems(version, after(CHANGELOG)),
];
}

const git = (...args) => execFileSync("git", args, { encoding: "utf8", maxBuffer: 1 << 26 });
const show = (ref) => (path) => {
try {
Expand All @@ -91,19 +143,19 @@ const show = (ref) => (path) => {
}
};

/** The commits between the last release commit and `base`; empty when `base` holds no earlier release. */
/** The commits between the last release commit and `base`, newest first; empty when `base` holds no earlier release. */
function commitsSinceLastRelease(base) {
const last = git("log", "--format=%H%x09%s", base)
.split("\n")
.find((line) => releaseVersionOf(line.split("\t").slice(1).join("\t")) !== null)
?.split("\t")[0];
if (!last) return [];
return git("log", "--no-merges", "--format=%H%x09%s", `${last}..${base}`)
.split("\n")
.filter(Boolean)
.map((line) => {
const [sha, ...subject] = line.split("\t");
return { sha, subject: subject.join("\t") };
return git("log", "--no-merges", "--format=%H%x1f%s%x1f%b%x1e", `${last}..${base}`)
.split("\x1e")
.filter((record) => record.trim())
.map((record) => {
const [sha, subject, body] = record.trim().split("\x1f");
return { sha, subject, body };
});
}

Expand Down
74 changes: 74 additions & 0 deletions scripts/ci/release-only-diff.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,80 @@ test("the last release is found by its subject, not by a line in another commit'
assert.match(result.stderr, /misses 2 commit\(s\)/);
});

/** A release v0.8.1 commit, a breaking change on main, then `later` commits; the release moves to `version`. */
function repoWithBreakingChange(version, later = () => {}) {
const shas = [];
return repo(
(write) => {
bump(version)(write);
const links = shas.map((sha) => `- ([x](/commit/${sha}))`).join("\n");
write(`releases/v${version}.md`, NOTES(version, links));
},
(write, git) => {
git("commit", "-q", "--allow-empty", "-m", "chore: release v0.8.1 (#1)");
write("packages/cli/src/floor.ts", "export const node = 22;\n");
git("add", "-A");
git("commit", "-qm", "fix(cli)!: needs Node 22 (#2)");
shas.push(git("rev-parse", "HEAD").trim());
later(shas[0], (...message) => {
git("commit", "-q", "--allow-empty", ...message.flatMap((m) => ["-m", m]));
shas.push(git("rev-parse", "HEAD").trim());
return shas.at(-1);
});
},
);
}

// fails without the fix: a patch release carrying a "fix(cli)!:" commit passed the release checks.
test("--verify refuses a patch release that carries a breaking change", () => {
const result = run(repoWithBreakingChange("0.8.2"), "--verify");
assert.equal(result.code, 1);
assert.match(
result.stderr,
/v0\.8\.2 is too small a bump for a breaking change: [0-9a-f]{9} fix\(cli\)!: needs Node 22 \(#2\)/,
);
assert.match(result.stderr, /Release it as v0\.9\.0/);
});

test("--verify passes a breaking change released as the next minor below 1.0", () => {
assert.equal(run(repoWithBreakingChange("0.9.0"), "--verify").code, 0);
});

test("--verify passes a patch release once the breaking change is reverted", () => {
const reverted = repoWithBreakingChange("0.8.2", (breaking, commit) => {
commit(
"revert(cli): hold the Node floor (#3)",
`This reverts commit ${breaking.slice(0, 10)}.`,
);
});
assert.equal(run(reverted, "--verify").code, 0);
});

test("--verify refuses again once the revert is itself reverted", () => {
const relanded = repoWithBreakingChange("0.8.2", (breaking, commit) => {
const revert = commit(
"revert(cli): hold the Node floor (#3)",
`This reverts commit ${breaking}.`,
);
commit("feat(cli): bring back the Node floor (#4)", `This reverts commit ${revert}.`);
});
assert.match(run(relanded, "--verify").stderr, /too small a bump for a breaking change/);
});

test("--verify refuses manifests that move from different versions", () => {
const root = repo(
(write) => {
bump("0.8.2")(write);
},
(write, git) => {
write("plugin.json", manifest("0.8.0"));
git("add", "-A");
git("commit", "-qm", "chore: plugin.json was behind");
},
);
assert.match(run(root, "--verify").stderr, /move from different versions: 0\.8\.1, 0\.8\.0/);
});

test("the merge queue runs the release checks on a release diff", () => {
const ci = readFileSync(new URL("../../.github/workflows/ci.yml", import.meta.url), "utf8");
assert.match(ci, /release-checks:[\s\S]*merge_group[\s\S]*release_diff/);
Expand Down
Loading