CI: auto-release a version tag when Renovate merges a dependency update
This commit was merged in pull request #16.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { bumpFromUpdateType, maxLevel, nextVersion } from "./next-version.ts";
|
||||
|
||||
test("bumpFromUpdateType: only major/minor keep their level; everything else is patch", () => {
|
||||
assert.equal(bumpFromUpdateType("major"), "major");
|
||||
assert.equal(bumpFromUpdateType("minor"), "minor");
|
||||
assert.equal(bumpFromUpdateType("patch"), "patch");
|
||||
assert.equal(bumpFromUpdateType("digest"), "patch");
|
||||
assert.equal(bumpFromUpdateType("pin"), "patch");
|
||||
assert.equal(bumpFromUpdateType("lockFileMaintenance"), "patch");
|
||||
assert.equal(bumpFromUpdateType(""), "patch");
|
||||
});
|
||||
|
||||
test("maxLevel: defaults to patch, escalates on the highest level present", () => {
|
||||
assert.equal(maxLevel([]), "patch");
|
||||
assert.equal(maxLevel(["patch"]), "patch");
|
||||
assert.equal(maxLevel(["patch", "minor"]), "minor");
|
||||
assert.equal(maxLevel(["minor", "major", "patch"]), "major");
|
||||
assert.equal(maxLevel(["digest", "pin"]), "patch");
|
||||
assert.equal(maxLevel(["", "bogus"]), "patch"); // unknown → patch, never throws
|
||||
});
|
||||
|
||||
test("nextVersion pre-1.0 (major===0): shift down so we never auto-cross into 1.0.0", () => {
|
||||
// dep major → 0.x minor (the 0.x "breaking" slot); dep minor/patch → 0.x patch
|
||||
assert.equal(nextVersion("v0.0.2", "major"), "v0.1.0");
|
||||
assert.equal(nextVersion("v0.0.2", "minor"), "v0.0.3");
|
||||
assert.equal(nextVersion("v0.0.2", "patch"), "v0.0.3");
|
||||
assert.equal(nextVersion("v0.3.4", "major"), "v0.4.0");
|
||||
assert.equal(nextVersion("v0.3.4", "minor"), "v0.3.5");
|
||||
assert.equal(nextVersion("v0.3.4", "patch"), "v0.3.5");
|
||||
});
|
||||
|
||||
test("nextVersion at/after 1.0.0: literal semver", () => {
|
||||
assert.equal(nextVersion("v1.2.3", "major"), "v2.0.0");
|
||||
assert.equal(nextVersion("v1.2.3", "minor"), "v1.3.0");
|
||||
assert.equal(nextVersion("v1.2.3", "patch"), "v1.2.4");
|
||||
});
|
||||
|
||||
test("nextVersion rejects a tag that is not vX.Y.Z", () => {
|
||||
assert.throws(() => nextVersion("1.2.3", "patch"), /vX\.Y\.Z/);
|
||||
assert.throws(() => nextVersion("vx.y.z", "patch"), /vX\.Y\.Z/);
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
// Pure release-version math for the Renovate auto-release (see renovate.yml → auto-release job,
|
||||
// README → CI/CD). Renovate stamps each commit with a `Release-Bump: <updateType>` trailer; the
|
||||
// workflow feeds those values here to pick the next `vX.Y.Z` tag. Kept side-effect-free and unit
|
||||
// tested (next-version.test.ts) — the git/tag/push side lives in the workflow shell.
|
||||
|
||||
export type Bump = "major" | "minor" | "patch";
|
||||
|
||||
// A dependency change is always at least a patch; only a real major/minor escalates.
|
||||
export function bumpFromUpdateType(updateType: string): Bump {
|
||||
if (updateType === "major") return "major";
|
||||
if (updateType === "minor") return "minor";
|
||||
return "patch";
|
||||
}
|
||||
|
||||
export function maxLevel(updateTypes: string[]): Bump {
|
||||
let level: Bump = "patch";
|
||||
for (const updateType of updateTypes) {
|
||||
const bump = bumpFromUpdateType(updateType);
|
||||
if (bump === "major") return "major";
|
||||
if (bump === "minor") level = "minor";
|
||||
}
|
||||
return level;
|
||||
}
|
||||
|
||||
// Pre-1.0 (major===0) shifts every level down one notch, so a dependency major only bumps the 0.x
|
||||
// minor and we never auto-cross into 1.0.0 — that stays a deliberate human milestone.
|
||||
export function nextVersion(latestTag: string, level: Bump): string {
|
||||
const match = /^v(\d+)\.(\d+)\.(\d+)$/.exec(latestTag);
|
||||
if (!match) throw new Error(`latest tag must be vX.Y.Z, got ${JSON.stringify(latestTag)}`);
|
||||
const major = Number(match[1]);
|
||||
const minor = Number(match[2]);
|
||||
const patch = Number(match[3]);
|
||||
const effective: Bump = major === 0 ? (level === "major" ? "minor" : "patch") : level;
|
||||
if (effective === "major") return `v${major + 1}.0.0`;
|
||||
if (effective === "minor") return `v${major}.${minor + 1}.0`;
|
||||
return `v${major}.${minor}.${patch + 1}`;
|
||||
}
|
||||
|
||||
// CLI: node auto-release/next-version.ts <latestTag> [updateType...] → prints the next tag.
|
||||
if (process.argv[1]?.endsWith("/next-version.ts")) {
|
||||
const [, , latestTag, ...updateTypes] = process.argv;
|
||||
process.stdout.write(nextVersion(latestTag ?? "", maxLevel(updateTypes)));
|
||||
}
|
||||
Reference in New Issue
Block a user