Open History to see the last commit
1const MINIMUM_RELEASE_GAP_MS = 6 * 60 * 60 * 1000;
2
3const isNightlyTag = (tag) => /^v.*-nightly\./.test(tag) || tag.startsWith("nightly-v");
4
5// Newest published nightly by publication time, or undefined when none exists.
6async function findLatestNightly({ github, context }) {
7 const releases = await github.paginate(github.rest.repos.listReleases, {
8 ...context.repo,
9 per_page: 100,
10 });
11 return releases
12 .filter((release) => !release.draft && release.published_at && isNightlyTag(release.tag_name))
13 .sort((a, b) => Date.parse(b.published_at) - Date.parse(a.published_at))[0];
14}
15
16// Runs after the workflow acquires the nightly concurrency lock.
17async function shouldReleaseNightly({ github, context, core, now = Date.now() }) {
18 const lastNightly = await findLatestNightly({ github, context });
19
20 if (!lastNightly) {
21 core.info("No published nightly found. Proceeding with release.");
22 return true;
23 }
24
25 if (now - Date.parse(lastNightly.published_at) < MINIMUM_RELEASE_GAP_MS) {
26 core.info(`Nightly ${lastNightly.tag_name} was published less than six hours ago. Skipping.`);
27 return false;
28 }
29
30 const { data: comparison } = await github.rest.repos.compareCommitsWithBasehead({
31 ...context.repo,
32 basehead: `${lastNightly.tag_name}...${context.sha}`,
33 per_page: 1,
34 });
35 if (comparison.status !== "ahead") {
36 core.info(
37 `Candidate commit is ${comparison.status} relative to ${lastNightly.tag_name}. Skipping.`,
38 );
39 return false;
40 }
41
42 core.info(`New commits since ${lastNightly.tag_name}, and the six-hour gap has passed.`);
43 return true;
44}
45
46// Stable releases build the commit the latest nightly shipped, so the stable
47// build is one nightly users already ran. Returns the nightly tag, its commit,
48// and the stable version that nightly was a preview of.
49async function resolveLatestNightlyCommit({ github, context, core }) {
50 const lastNightly = await findLatestNightly({ github, context });
51 if (!lastNightly) {
52 throw new Error("No published nightly found. Stable releases build the latest nightly commit.");
53 }
54
55 const tag = lastNightly.tag_name;
56 // repos.getCommit dereferences annotated tags, so this is the commit either way.
57 const { data: commit } = await github.rest.repos.getCommit({ ...context.repo, ref: tag });
58 const version = /^(?:nightly-)?v(\d+\.\d+\.\d+)-nightly\./.exec(tag)?.[1];
59 if (!version) {
60 throw new Error(`Cannot derive a stable version from nightly tag ${tag}.`);
61 }
62
63 core.info(`Latest nightly ${tag} shipped ${commit.sha} as a preview of ${version}.`);
64 return { tag, sha: commit.sha, version };
65}
66
67module.exports = { shouldReleaseNightly, resolveLatestNightlyCommit };