fix(release): restrict stable channels to main
This commit is contained in:
1 parent
47ef0177dc
commit
3060cc4614
4 files changed
+141
-9
No files matched your search
@@ -1,5 +1,43 @@
|
||||
const MINIMUM_RELEASE_GAP_MS = 6 * 60 * 60 * 1000;
|
||||
|
||||
function repositoryDefaultBranch(context) {
|
||||
const branch = context.payload.repository?.default_branch;
|
||||
if (!branch) {
|
||||
throw new Error("GitHub did not provide the repository default branch.");
|
||||
}
|
||||
return branch;
|
||||
}
|
||||
|
||||
async function assertCommitOnDefaultBranch({ github, context, sha }) {
|
||||
const defaultBranch = repositoryDefaultBranch(context);
|
||||
const { data: comparison } = await github.rest.repos.compareCommitsWithBasehead({
|
||||
...context.repo,
|
||||
basehead: `${sha}...${defaultBranch}`,
|
||||
per_page: 1,
|
||||
});
|
||||
if (comparison.status !== "ahead" && comparison.status !== "identical") {
|
||||
throw new Error(
|
||||
`Release commit ${sha} is not contained in ${defaultBranch} (${comparison.status}).`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function assertReleaseSource({ github, context, releaseChannel }) {
|
||||
if (releaseChannel === "preview") return;
|
||||
if (releaseChannel !== "stable" && releaseChannel !== "nightly") {
|
||||
throw new Error(`Unsupported release channel: ${releaseChannel}`);
|
||||
}
|
||||
|
||||
const defaultBranch = repositoryDefaultBranch(context);
|
||||
if (context.eventName === "workflow_dispatch" && context.ref !== `refs/heads/${defaultBranch}`) {
|
||||
throw new Error(
|
||||
`${releaseChannel} releases must be dispatched from ${defaultBranch}; selected ${context.ref}. Use the preview channel for branch builds.`,
|
||||
);
|
||||
}
|
||||
|
||||
await assertCommitOnDefaultBranch({ github, context, sha: context.sha });
|
||||
}
|
||||
|
||||
const isNightlyTag = (tag) => /^v.*-nightly\./.test(tag) || tag.startsWith("nightly-v");
|
||||
|
||||
// Newest published nightly by publication time, or undefined when none exists.
|
||||
@@ -64,4 +102,9 @@ async function resolveLatestNightlyCommit({ github, context, core }) {
|
||||
return { tag, sha: commit.sha, version };
|
||||
}
|
||||
|
||||
module.exports = { shouldReleaseNightly, resolveLatestNightlyCommit };
|
||||
module.exports = {
|
||||
assertCommitOnDefaultBranch,
|
||||
assertReleaseSource,
|
||||
shouldReleaseNightly,
|
||||
resolveLatestNightlyCommit,
|
||||
};
|
||||
@@ -1,6 +1,10 @@
|
||||
const assert = require("node:assert/strict");
|
||||
const test = require("node:test");
|
||||
const { shouldReleaseNightly } = require("./check-nightly-release.cjs");
|
||||
const {
|
||||
assertCommitOnDefaultBranch,
|
||||
assertReleaseSource,
|
||||
shouldReleaseNightly,
|
||||
} = require("./check-nightly-release.cjs");
|
||||
|
||||
const now = Date.parse("2026-09-05T12:00:00Z");
|
||||
const hour = 60 * 60 * 1000;
|
||||
@@ -11,6 +15,77 @@ const nightly = (hoursAgo, overrides = {}) => ({
|
||||
...overrides,
|
||||
});
|
||||
|
||||
function releaseSourceFixture({
|
||||
comparisonStatus = "ahead",
|
||||
eventName = "workflow_dispatch",
|
||||
ref = "refs/heads/main",
|
||||
sha = "candidate",
|
||||
} = {}) {
|
||||
const calls = [];
|
||||
return {
|
||||
calls,
|
||||
options: {
|
||||
context: {
|
||||
eventName,
|
||||
payload: { repository: { default_branch: "main" } },
|
||||
ref,
|
||||
repo: { owner: "example", repo: "app" },
|
||||
sha,
|
||||
},
|
||||
github: {
|
||||
rest: {
|
||||
repos: {
|
||||
async compareCommitsWithBasehead(params) {
|
||||
calls.push(params);
|
||||
return { data: { status: comparisonStatus } };
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test("allows preview releases from any branch without consulting main", async () => {
|
||||
const { options, calls } = releaseSourceFixture({ ref: "refs/heads/feature" });
|
||||
await assertReleaseSource({ ...options, releaseChannel: "preview" });
|
||||
assert.equal(calls.length, 0);
|
||||
});
|
||||
|
||||
for (const releaseChannel of ["stable", "nightly"]) {
|
||||
test(`rejects a manual ${releaseChannel} release from a feature branch`, async () => {
|
||||
const { options, calls } = releaseSourceFixture({ ref: "refs/heads/feature" });
|
||||
await assert.rejects(
|
||||
assertReleaseSource({ ...options, releaseChannel }),
|
||||
new RegExp(`${releaseChannel} releases must be dispatched from main`),
|
||||
);
|
||||
assert.equal(calls.length, 0);
|
||||
});
|
||||
|
||||
test(`allows a manual ${releaseChannel} release from main`, async () => {
|
||||
const { options, calls } = releaseSourceFixture();
|
||||
await assertReleaseSource({ ...options, releaseChannel });
|
||||
assert.equal(calls[0].basehead, "candidate...main");
|
||||
});
|
||||
}
|
||||
|
||||
for (const comparisonStatus of ["behind", "diverged"]) {
|
||||
test(`rejects a release commit that is ${comparisonStatus} from main`, async () => {
|
||||
const { options } = releaseSourceFixture({ comparisonStatus, eventName: "push" });
|
||||
await assert.rejects(
|
||||
assertCommitOnDefaultBranch({ ...options, sha: "release-commit" }),
|
||||
new RegExp(`not contained in main \\(${comparisonStatus}\\)`),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
test("accepts a release commit already contained in main", async () => {
|
||||
for (const comparisonStatus of ["ahead", "identical"]) {
|
||||
const { options } = releaseSourceFixture({ comparisonStatus, eventName: "push" });
|
||||
await assertCommitOnDefaultBranch({ ...options, sha: "release-commit" });
|
||||
}
|
||||
});
|
||||
|
||||
function fixture({ releases = [nightly(7)], comparisonStatus = "ahead" } = {}) {
|
||||
const calls = [];
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user