Open History to see the last commit
1const MINIMUM_RELEASE_GAP_MS = 6 * 60 * 60 * 1000;
2
3function repositoryDefaultBranch(context) {
4 const branch = context.payload.repository?.default_branch;
5 if (!branch) {
6 throw new Error("GitHub did not provide the repository default branch.");
7 }
8 return branch;
9}
10
11async function assertCommitOnDefaultBranch({ github, context, sha }) {
12 const defaultBranch = repositoryDefaultBranch(context);
13 const { data: comparison } = await github.rest.repos.compareCommitsWithBasehead({
14 ...context.repo,
15 basehead: `${sha}...${defaultBranch}`,
16 per_page: 1,
17 });
18 if (comparison.status !== "ahead" && comparison.status !== "identical") {
19 throw new Error(
20 `Release commit ${sha} is not contained in ${defaultBranch} (${comparison.status}).`,
21 );
22 }
23}
24
25async function assertReleaseSource({ github, context, releaseChannel }) {
26 if (releaseChannel === "preview") return;
27 if (releaseChannel !== "stable" && releaseChannel !== "nightly") {
28 throw new Error(`Unsupported release channel: ${releaseChannel}`);
29 }
30
31 const defaultBranch = repositoryDefaultBranch(context);
32 if (context.eventName === "workflow_dispatch" && context.ref !== `refs/heads/${defaultBranch}`) {
33 throw new Error(
34 `${releaseChannel} releases must be dispatched from ${defaultBranch}; selected ${context.ref}. Use the preview channel for branch builds.`,
35 );
36 }
37
38 await assertCommitOnDefaultBranch({ github, context, sha: context.sha });
39}
40
41const isNightlyTag = (tag) => /^v.*-nightly\./.test(tag) || tag.startsWith("nightly-v");
42
43async function findLatestNightly({ github, context }) {
44 const releases = await github.paginate(github.rest.repos.listReleases, {
45 ...context.repo,
46 per_page: 100,
47 });
48 return releases
49 .filter((release) => !release.draft && release.published_at && isNightlyTag(release.tag_name))
50 .sort((a, b) => Date.parse(b.published_at) - Date.parse(a.published_at))[0];
51}
52
53async function shouldReleaseNightly({ github, context, core, now = Date.now() }) {
54 const lastNightly = await findLatestNightly({ github, context });
55
56 if (!lastNightly) {
57 core.info("No published nightly found. Proceeding with release.");
58 return true;
59 }
60
61 if (now - Date.parse(lastNightly.published_at) < MINIMUM_RELEASE_GAP_MS) {
62 core.info(`Nightly ${lastNightly.tag_name} was published less than six hours ago. Skipping.`);
63 return false;
64 }
65
66 const { data: comparison } = await github.rest.repos.compareCommitsWithBasehead({
67 ...context.repo,
68 basehead: `${lastNightly.tag_name}...${context.sha}`,
69 per_page: 1,
70 });
71 if (comparison.status !== "ahead") {
72 core.info(
73 `Candidate commit is ${comparison.status} relative to ${lastNightly.tag_name}. Skipping.`,
74 );
75 return false;
76 }
77
78 core.info(`New commits since ${lastNightly.tag_name}, and the six-hour gap has passed.`);
79 return true;
80}
81
82async function resolveLatestNightlyCommit({ github, context, core }) {
83 const lastNightly = await findLatestNightly({ github, context });
84 if (!lastNightly) {
85 throw new Error("No published nightly found. Stable releases build the latest nightly commit.");
86 }
87
88 const tag = lastNightly.tag_name;
89 const { data: commit } = await github.rest.repos.getCommit({ ...context.repo, ref: tag });
90 const version = /^(?:nightly-)?v(\d+\.\d+\.\d+)-nightly\./.exec(tag)?.[1];
91 if (!version) {
92 throw new Error(`Cannot derive a stable version from nightly tag ${tag}.`);
93 }
94
95 core.info(`Latest nightly ${tag} shipped ${commit.sha} as a preview of ${version}.`);
96 return { tag, sha: commit.sha, version };
97}
98
99module.exports = {
100 assertCommitOnDefaultBranch,
101 assertReleaseSource,
102 shouldReleaseNightly,
103 resolveLatestNightlyCommit,
104};