@d-zero/release-automation 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +58 -2
- package/dist/changelog.js +26 -0
- package/dist/classify.js +406 -0
- package/dist/cli.js +126 -0
- package/dist/commands/approve-renovate.js +35 -0
- package/dist/commands/context.js +139 -0
- package/dist/commands/generate.js +97 -0
- package/dist/commands/local.js +128 -0
- package/dist/commands/nightly.js +125 -0
- package/dist/commands/publish.js +246 -0
- package/dist/commands/release-pr.js +297 -0
- package/dist/commands/runs.js +21 -0
- package/dist/commands/verify-release-pr.js +116 -0
- package/dist/conventional-commit.js +35 -0
- package/dist/dist-tag.js +32 -0
- package/dist/guard.js +58 -0
- package/dist/io/exec.js +56 -0
- package/dist/io/git.js +78 -0
- package/dist/io/github.js +87 -0
- package/dist/io/npm.js +25 -0
- package/dist/io/repo.js +170 -0
- package/dist/lines-check.js +147 -0
- package/dist/next-version.js +88 -0
- package/dist/nightly.js +56 -0
- package/dist/pr-checks.js +64 -0
- package/dist/release-content.js +231 -0
- package/dist/release-pr-decision.js +67 -0
- package/dist/retry.js +32 -0
- package/dist/rotation.js +133 -0
- package/dist/semver.js +87 -0
- package/dist/support-end.js +26 -0
- package/dist/types.js +14 -0
- package/package.json +83 -5
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { distTag } from '../dist-tag.js';
|
|
2
|
+
import { runShell } from '../io/exec.js';
|
|
3
|
+
import { GitHubError } from '../io/github.js';
|
|
4
|
+
import { fetchPackument } from '../io/npm.js';
|
|
5
|
+
import { publicPackages, publishModeAt, readLinesFile, versionAt } from '../io/repo.js';
|
|
6
|
+
import { releasePrOrigin } from '../pr-checks.js';
|
|
7
|
+
import { PUBLISH_ATTEMPTS, retry } from '../retry.js';
|
|
8
|
+
import { isFirstStableOfMajor, rotationIssue } from '../rotation.js';
|
|
9
|
+
import { classifiedLines, ensureIssue, github, localGit, RELEASE_LABEL, requireEnv, summary, VERIFY_CHECK, } from './context.js';
|
|
10
|
+
import { lineMarker } from './release-pr.js';
|
|
11
|
+
/** The check of the Minimum CI the sync waits for. */
|
|
12
|
+
const CI_OK = 'ci-ok';
|
|
13
|
+
/**
|
|
14
|
+
* Publishes the release a merged Release PR carries, when the pushed commit
|
|
15
|
+
* is the merge of one; otherwise does nothing. In order: verify the Release
|
|
16
|
+
* PR, tag the merge commit (as the Publish App), publish with retries, verify
|
|
17
|
+
* every package and dist-tag on the registry, bring the release back to
|
|
18
|
+
* `dev` (current line only), and open the rotation Issue after the first
|
|
19
|
+
* stable release of a major. Any failure stops the rest.
|
|
20
|
+
* @param options - Flags
|
|
21
|
+
* @param options.dryRun - Decide and report without writing
|
|
22
|
+
* @example
|
|
23
|
+
* await publishCommand({ dryRun: false });
|
|
24
|
+
*/
|
|
25
|
+
export async function publishCommand(options) {
|
|
26
|
+
const git = localGit();
|
|
27
|
+
const gh = github('GITHUB_TOKEN');
|
|
28
|
+
const publishGh = github('PUBLISH_APP_TOKEN');
|
|
29
|
+
const branch = requireEnv('GITHUB_REF_NAME');
|
|
30
|
+
const sha = git.sha('HEAD');
|
|
31
|
+
const { lines } = classifiedLines(git, new Date());
|
|
32
|
+
const line = lines.find(({ publishBranch }) => publishBranch === branch);
|
|
33
|
+
if (line === undefined) {
|
|
34
|
+
await summary(`${branch} is not a publish branch`);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const pulls = await gh.rest('GET', `/repos/{repo}/commits/${sha}/pulls`);
|
|
38
|
+
const pr = pulls.find((candidate) => candidate.merge_commit_sha === sha &&
|
|
39
|
+
candidate.merged_at !== null &&
|
|
40
|
+
candidate.labels.some(({ name }) => name === RELEASE_LABEL) &&
|
|
41
|
+
(candidate.body ?? '').includes(lineMarker(line.branch)));
|
|
42
|
+
if (pr === undefined) {
|
|
43
|
+
await summary(`${sha} is not the merge of a Release PR; nothing to publish`);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const headCommit = await gh.rest('GET', `/repos/{repo}/commits/${pr.head.sha}`);
|
|
47
|
+
const origin = releasePrOrigin({
|
|
48
|
+
author: pr.user.login,
|
|
49
|
+
commits: [{ author: headCommit.author?.login ?? null, verified: true }],
|
|
50
|
+
body: pr.body ?? '',
|
|
51
|
+
labels: pr.labels.map(({ name }) => name),
|
|
52
|
+
}, `${requireEnv('RELEASE_APP_SLUG')}[bot]`);
|
|
53
|
+
if (!origin.ok) {
|
|
54
|
+
throw new Error(`#${pr.number} was not generated by the Release App: ${origin.reasons.join('; ')}`);
|
|
55
|
+
}
|
|
56
|
+
const checks = await gh.rest('GET', `/repos/{repo}/commits/${pr.head.sha}/check-runs?check_name=${VERIFY_CHECK}`);
|
|
57
|
+
if (!checks.check_runs.some(({ conclusion }) => conclusion === 'success')) {
|
|
58
|
+
throw new Error(`#${pr.number} has no successful ${VERIFY_CHECK} check`);
|
|
59
|
+
}
|
|
60
|
+
const mode = publishModeAt(git, 'HEAD');
|
|
61
|
+
const packages = publicPackages(git, 'HEAD');
|
|
62
|
+
const stableTagsBefore = git.tags('HEAD').filter((tag) => /^v\d+\.\d+\.\d+$/.test(tag));
|
|
63
|
+
const unpublished = await unpublishedPackages(packages);
|
|
64
|
+
const version = versionAt(git, 'HEAD');
|
|
65
|
+
const tag = await decideDistTag({ mode, line, lines, unpublished, version });
|
|
66
|
+
const tags = mode === 'lerna-independent'
|
|
67
|
+
? unpublished.map((pkg) => `${pkg.name}@${pkg.version}`)
|
|
68
|
+
: [`v${version ?? ''}`];
|
|
69
|
+
await summary(`#${pr.number}: publishing ${tags.join(', ')} with dist-tag ${tag}`);
|
|
70
|
+
if (options.dryRun) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
for (const name of tags) {
|
|
74
|
+
await ensureTag(publishGh, name, sha);
|
|
75
|
+
}
|
|
76
|
+
const command = publishCommandFor(mode, tag, readLinesFile(git, 'HEAD')?.publishCommand);
|
|
77
|
+
await retry(async (attempt) => {
|
|
78
|
+
await summary(`publish attempt ${attempt}: ${command}`);
|
|
79
|
+
runShell(command, git.cwd);
|
|
80
|
+
}, { attempts: PUBLISH_ATTEMPTS, baseDelayMs: 30_000 });
|
|
81
|
+
const verified = [];
|
|
82
|
+
if (mode !== 'custom') {
|
|
83
|
+
await retry(async () => {
|
|
84
|
+
verified.length = 0;
|
|
85
|
+
for (const pkg of packages) {
|
|
86
|
+
const packument = await fetchPackument(pkg.name);
|
|
87
|
+
if (!packument?.versions.includes(pkg.version)) {
|
|
88
|
+
throw new Error(`${pkg.name}@${pkg.version} is not on the registry`);
|
|
89
|
+
}
|
|
90
|
+
if (unpublished.includes(pkg) && packument.distTags[tag] !== pkg.version) {
|
|
91
|
+
throw new Error(`${pkg.name}: ${tag} points at ${packument.distTags[tag] ?? 'nothing'}, not ${pkg.version}`);
|
|
92
|
+
}
|
|
93
|
+
verified.push(`${pkg.name}@${pkg.version} (${tag})`);
|
|
94
|
+
}
|
|
95
|
+
}, { attempts: 5, baseDelayMs: 10_000 });
|
|
96
|
+
}
|
|
97
|
+
await summary(`published and verified:\n${verified.map((entry) => `- ${entry}`).join('\n')}`);
|
|
98
|
+
if (line.role === 'current') {
|
|
99
|
+
await syncToDev(git, publishGh, sha);
|
|
100
|
+
}
|
|
101
|
+
if (version !== null && isFirstStableOfMajor(version, stableTagsBefore)) {
|
|
102
|
+
const rules = JSON.parse(git.show('origin/dev', '.github/renovate.json') ?? '{}');
|
|
103
|
+
const stop = rules.packageRules?.find(({ description }) => description?.startsWith('release-automation: lock file maintenance stopped'));
|
|
104
|
+
await ensureIssue(gh, rotationIssue({
|
|
105
|
+
version,
|
|
106
|
+
publishedAt: new Date(),
|
|
107
|
+
verified,
|
|
108
|
+
lockfileStop: stop?.matchBaseBranches === undefined
|
|
109
|
+
? 'No previous line had lock file maintenance to stop.'
|
|
110
|
+
: `Stopped on ${stop.matchBaseBranches.join(', ')} (in dev's renovate.json).`,
|
|
111
|
+
}), []);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* @param packages - The public packages at the release commit
|
|
116
|
+
* @returns The packages whose version is not on the registry yet
|
|
117
|
+
* @throws {Error} For a package that was never published: npm accepts no
|
|
118
|
+
* trusted publisher for it, so a person publishes a placeholder first
|
|
119
|
+
*/
|
|
120
|
+
async function unpublishedPackages(packages) {
|
|
121
|
+
const result = [];
|
|
122
|
+
for (const pkg of packages) {
|
|
123
|
+
const packument = await fetchPackument(pkg.name);
|
|
124
|
+
if (packument === null) {
|
|
125
|
+
throw new Error(`${pkg.name} was never published. Publish a placeholder by hand and set its trusted publisher first`);
|
|
126
|
+
}
|
|
127
|
+
if (!packument.versions.includes(pkg.version)) {
|
|
128
|
+
result.push(pkg);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* @param input - The release
|
|
135
|
+
* @returns The dist-tag of the release
|
|
136
|
+
*/
|
|
137
|
+
async function decideDistTag(input) {
|
|
138
|
+
const successorStablePublished = input.line.successorStable !== null;
|
|
139
|
+
const versions = input.mode === 'lerna-independent'
|
|
140
|
+
? input.unpublished.map((pkg) => ({ version: pkg.version, name: pkg.name }))
|
|
141
|
+
: [{ version: input.version ?? '', name: input.unpublished[0]?.name ?? null }];
|
|
142
|
+
const tags = new Set();
|
|
143
|
+
for (const { version, name } of versions) {
|
|
144
|
+
const packument = name === null ? null : await fetchPackument(name);
|
|
145
|
+
tags.add(distTag({
|
|
146
|
+
version,
|
|
147
|
+
role: input.line.role,
|
|
148
|
+
independent: input.mode === 'lerna-independent',
|
|
149
|
+
successorStablePublished,
|
|
150
|
+
latestOnRegistry: packument?.distTags['latest'] ?? null,
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
if (tags.size !== 1) {
|
|
154
|
+
throw new Error(`The packages of one release need one dist-tag, not ${[...tags].join(', ')}`);
|
|
155
|
+
}
|
|
156
|
+
return [...tags][0] ?? 'latest';
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* @param mode - The publishing mode
|
|
160
|
+
* @param tag - The dist-tag
|
|
161
|
+
* @param override - The command of the lines file
|
|
162
|
+
* @returns The publish command
|
|
163
|
+
*/
|
|
164
|
+
function publishCommandFor(mode, tag, override) {
|
|
165
|
+
if (override !== undefined) {
|
|
166
|
+
return `DIST_TAG=${tag} ${override}`;
|
|
167
|
+
}
|
|
168
|
+
if (mode === 'npm-single') {
|
|
169
|
+
return `npm publish --provenance --access public --tag ${tag}`;
|
|
170
|
+
}
|
|
171
|
+
if (mode === 'custom') {
|
|
172
|
+
throw new Error('A repository without npm packages sets "publishCommand"');
|
|
173
|
+
}
|
|
174
|
+
return `yarn lerna publish from-package --dist-tag ${tag} --yes`;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Creates a tag on the release commit, or checks that it already points there.
|
|
178
|
+
* @param gh - The Publish App's client
|
|
179
|
+
* @param name - The tag
|
|
180
|
+
* @param sha - The release commit
|
|
181
|
+
*/
|
|
182
|
+
async function ensureTag(gh, name, sha) {
|
|
183
|
+
try {
|
|
184
|
+
await gh.rest('POST', '/repos/{repo}/git/refs', { ref: `refs/tags/${name}`, sha });
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
if (!(error instanceof GitHubError) || error.status !== 422) {
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
const existing = await gh.rest('GET', `/repos/{repo}/git/ref/tags/${encodeURIComponent(name)}`);
|
|
191
|
+
if (existing.object.sha !== sha) {
|
|
192
|
+
throw new Error(`Tag ${name} points at ${existing.object.sha}, not the release ${sha}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Brings a release on `main` back to `dev`: merges `main` into the head of
|
|
198
|
+
* `dev`, runs the Minimum CI on the result through a `sync/<sha>` branch,
|
|
199
|
+
* and moves `dev` to it without force. When `dev` moved meanwhile, it tries
|
|
200
|
+
* again from the new head, three times in all.
|
|
201
|
+
* @param git - Git access
|
|
202
|
+
* @param gh - The Publish App's client
|
|
203
|
+
* @param sha - The release commit on `main`
|
|
204
|
+
*/
|
|
205
|
+
async function syncToDev(git, gh, sha) {
|
|
206
|
+
const token = requireEnv('PUBLISH_APP_TOKEN');
|
|
207
|
+
const slug = requireEnv('PUBLISH_APP_SLUG');
|
|
208
|
+
const bot = await gh.rest('GET', `/users/${encodeURIComponent(`${slug}[bot]`)}`);
|
|
209
|
+
const syncBranch = `sync/${sha.slice(0, 12)}`;
|
|
210
|
+
await retry(async () => {
|
|
211
|
+
git.raw('fetch', 'origin', 'dev');
|
|
212
|
+
git.raw('checkout', '--force', '--detach', 'origin/dev');
|
|
213
|
+
git.raw('-c', `user.name=${slug}[bot]`, '-c', `user.email=${bot.id}+${slug}[bot]@users.noreply.github.com`, 'merge', '--no-ff', '--no-edit', sha);
|
|
214
|
+
const merged = git.sha('HEAD');
|
|
215
|
+
git.pushAs(token, gh.repository, `HEAD:refs/heads/${syncBranch}`);
|
|
216
|
+
await waitForCheck(gh, merged, CI_OK);
|
|
217
|
+
await gh.rest('PATCH', '/repos/{repo}/git/refs/heads/dev', {
|
|
218
|
+
sha: merged,
|
|
219
|
+
force: false,
|
|
220
|
+
});
|
|
221
|
+
await summary(`dev moved to ${merged}`);
|
|
222
|
+
}, { attempts: 3, baseDelayMs: 5000 });
|
|
223
|
+
await gh.rest('DELETE', `/repos/{repo}/git/refs/heads/${syncBranch}`);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Waits until a check on a commit concludes.
|
|
227
|
+
* @param gh - The client
|
|
228
|
+
* @param sha - The commit
|
|
229
|
+
* @param name - The check
|
|
230
|
+
* @throws {Error} When it fails or does not finish within an hour
|
|
231
|
+
*/
|
|
232
|
+
async function waitForCheck(gh, sha, name) {
|
|
233
|
+
const deadline = Date.now() + 60 * 60 * 1000;
|
|
234
|
+
while (Date.now() < deadline) {
|
|
235
|
+
const { check_runs } = await gh.rest('GET', `/repos/{repo}/commits/${sha}/check-runs?check_name=${name}`);
|
|
236
|
+
const run = check_runs[0];
|
|
237
|
+
if (run?.status === 'completed') {
|
|
238
|
+
if (run.conclusion === 'success') {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
throw new Error(`${name} on ${sha} concluded ${run.conclusion ?? 'without a result'}`);
|
|
242
|
+
}
|
|
243
|
+
await new Promise((resolve) => setTimeout(resolve, 30_000));
|
|
244
|
+
}
|
|
245
|
+
throw new Error(`${name} on ${sha} did not finish within an hour`);
|
|
246
|
+
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { runShell } from '../io/exec.js';
|
|
2
|
+
import { fetchPackument } from '../io/npm.js';
|
|
3
|
+
import { publicPackages, publishModeAt, readLinesFile, versionAt } from '../io/repo.js';
|
|
4
|
+
import { isBlocked, isStale } from '../nightly.js';
|
|
5
|
+
import { decideReleasePr } from '../release-pr-decision.js';
|
|
6
|
+
import { releaseContent } from '../release-content.js';
|
|
7
|
+
import { DEFAULT_SECURITY_BATCH_MINUTES } from '../types.js';
|
|
8
|
+
import { classifiedLines, ensureIssue, github, localGit, RELEASE_LABEL, REPLACED_LABEL, requireEnv, summary, } from './context.js';
|
|
9
|
+
import { generateRelease } from './generate.js';
|
|
10
|
+
import { nightlyRuns } from './runs.js';
|
|
11
|
+
/** The label of a pull request that fixes a vulnerability. */
|
|
12
|
+
export const SECURITY_LABEL = 'security';
|
|
13
|
+
/**
|
|
14
|
+
* The markers a Release PR body carries.
|
|
15
|
+
* @param branch - The line branch
|
|
16
|
+
* @returns The marker of the line
|
|
17
|
+
* @example
|
|
18
|
+
* lineMarker('dev'); // '<!-- release-automation:line:dev -->'
|
|
19
|
+
*/
|
|
20
|
+
export function lineMarker(branch) {
|
|
21
|
+
return `<!-- release-automation:line:${branch} -->`;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Reads a value marker from a Release PR body.
|
|
25
|
+
* @param body - The body
|
|
26
|
+
* @param name - The marker name (`fingerprint`, `promote`)
|
|
27
|
+
* @returns The value, or `null`
|
|
28
|
+
* @example
|
|
29
|
+
* markerValue('<!-- release-automation:promote:rc -->', 'promote'); // 'rc'
|
|
30
|
+
*/
|
|
31
|
+
export function markerValue(body, name) {
|
|
32
|
+
return (new RegExp(`<!-- release-automation:${name}:([^ ]+) -->`).exec(body ?? '')?.[1] ??
|
|
33
|
+
null);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The branch a line's Release PR is pushed to.
|
|
37
|
+
* @param branch - The line branch
|
|
38
|
+
* @returns The head branch
|
|
39
|
+
* @example
|
|
40
|
+
* releaseBranch('dev'); // 'release/dev'
|
|
41
|
+
*/
|
|
42
|
+
export function releaseBranch(branch) {
|
|
43
|
+
return `release/${branch}`;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The revision of the last release of a line: `main` for the current line,
|
|
47
|
+
* the tag of the version on the line's branch for the others.
|
|
48
|
+
* @param git - Git access
|
|
49
|
+
* @param line - The line
|
|
50
|
+
* @param mode - The publishing mode
|
|
51
|
+
* @returns The revision
|
|
52
|
+
*/
|
|
53
|
+
export function baselineOf(git, line, mode) {
|
|
54
|
+
if (line.role === 'current' || mode === 'lerna-independent') {
|
|
55
|
+
return `origin/${line.publishBranch}`;
|
|
56
|
+
}
|
|
57
|
+
return `v${versionAt(git, `origin/${line.branch}`) ?? ''}`;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Runs the Release PR decision for every line, or one, and acts on it with
|
|
61
|
+
* the Release App's token (`RELEASE_APP_TOKEN`). The checkout must hold every
|
|
62
|
+
* branch and tag.
|
|
63
|
+
* @param options - What to run
|
|
64
|
+
* @param options.line - Only this line branch
|
|
65
|
+
* @param options.manual - Whether a person started the run
|
|
66
|
+
* @param options.promote - The stage a person asked for
|
|
67
|
+
* @param options.dryRun - Decide and report without writing
|
|
68
|
+
* @example
|
|
69
|
+
* await releasePrCommand({ manual: false, promote: null, dryRun: false });
|
|
70
|
+
*/
|
|
71
|
+
export async function releasePrCommand(options) {
|
|
72
|
+
const now = new Date();
|
|
73
|
+
const git = localGit();
|
|
74
|
+
const gh = github('RELEASE_APP_TOKEN');
|
|
75
|
+
const { lines, collaborative } = classifiedLines(git, now);
|
|
76
|
+
const mode = publishModeAt(git, 'origin/dev');
|
|
77
|
+
const linesFile = readLinesFile(git, 'origin/dev');
|
|
78
|
+
const targets = options.line === undefined
|
|
79
|
+
? lines
|
|
80
|
+
: lines.filter(({ branch }) => branch === options.line);
|
|
81
|
+
if (targets.length === 0) {
|
|
82
|
+
throw new Error(`${options.line ?? ''} is not a line`);
|
|
83
|
+
}
|
|
84
|
+
const prs = await gh.paginate('/repos/{repo}/pulls?state=all&sort=updated&direction=desc');
|
|
85
|
+
const releasePrs = prs.filter((pr) => pr.labels.some(({ name }) => name === RELEASE_LABEL));
|
|
86
|
+
for (const line of targets) {
|
|
87
|
+
const runs = await nightlyRuns(gh, line.branch);
|
|
88
|
+
if (isStale(runs, now)) {
|
|
89
|
+
await ensureIssue(gh, {
|
|
90
|
+
marker: `<!-- release-automation:nightly-stale:${line.branch} -->`,
|
|
91
|
+
title: `The Nightly of ${line.branch} stopped running`,
|
|
92
|
+
body: `<!-- release-automation:nightly-stale:${line.branch} -->\n\nNo Nightly of \`${line.branch}\` finished in the last two days. GitHub disables the schedule of a public repository after 60 days without activity; enable the Nightly workflow again in the Actions tab, then close this Issue.\n`,
|
|
93
|
+
}, []);
|
|
94
|
+
}
|
|
95
|
+
const baseline = baselineOf(git, line, mode);
|
|
96
|
+
const head = `origin/${line.branch}`;
|
|
97
|
+
const content = releaseContent(git, baseline, head);
|
|
98
|
+
const securityLandedAt = [];
|
|
99
|
+
for (const carrier of content.carriers) {
|
|
100
|
+
const pulls = await gh.rest('GET', `/repos/{repo}/commits/${carrier.sha}/pulls`);
|
|
101
|
+
if (pulls.some((pr) => pr.labels.some(({ name }) => name === SECURITY_LABEL))) {
|
|
102
|
+
securityLandedAt.push(new Date(carrier.time * 1000));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const mine = releasePrs.filter((pr) => (pr.body ?? '').includes(lineMarker(line.branch)));
|
|
106
|
+
const open = mine.find((pr) => pr.state === 'open') ?? null;
|
|
107
|
+
const declined = mine
|
|
108
|
+
.filter((pr) => pr.state === 'closed' &&
|
|
109
|
+
pr.merged_at === null &&
|
|
110
|
+
!pr.labels.some(({ name }) => name === REPLACED_LABEL))
|
|
111
|
+
.map((pr) => markerValue(pr.body, 'fingerprint'))
|
|
112
|
+
.filter((value) => value !== null);
|
|
113
|
+
const openPr = open === null ? null : await describeOpenPr(gh, git, open);
|
|
114
|
+
const decision = decideReleasePr({
|
|
115
|
+
now,
|
|
116
|
+
manual: options.manual,
|
|
117
|
+
fingerprint: content.fingerprint,
|
|
118
|
+
userFacingSince: content.carriers[0] === undefined
|
|
119
|
+
? null
|
|
120
|
+
: new Date(content.carriers[0].time * 1000),
|
|
121
|
+
securityLandedAt,
|
|
122
|
+
openPr,
|
|
123
|
+
declinedFingerprints: declined,
|
|
124
|
+
nightlyFailing: isBlocked(runs),
|
|
125
|
+
syncPending: line.role === 'current' && !git.isAncestor('origin/main', 'origin/dev'),
|
|
126
|
+
publishIncomplete: await publishIncomplete(git, line, mode),
|
|
127
|
+
securityBatchMinutes: linesFile?.securityBatchMinutes ?? DEFAULT_SECURITY_BATCH_MINUTES,
|
|
128
|
+
});
|
|
129
|
+
await summary(`${line.branch}: ${JSON.stringify(decision)}`);
|
|
130
|
+
if (options.dryRun || decision.action === 'none') {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (decision.action === 'replace' && open !== null) {
|
|
134
|
+
await gh.rest('POST', `/repos/{repo}/issues/${open.number}/labels`, {
|
|
135
|
+
labels: [REPLACED_LABEL],
|
|
136
|
+
});
|
|
137
|
+
await gh.rest('POST', `/repos/{repo}/issues/${open.number}/comments`, {
|
|
138
|
+
body: 'Replaced: the release changed after this pull request was approved. A successor Release PR carries the current content and needs a new review and Full CI.',
|
|
139
|
+
});
|
|
140
|
+
await gh.rest('PATCH', `/repos/{repo}/pulls/${open.number}`, { state: 'closed' });
|
|
141
|
+
if (!decision.openSuccessor) {
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const promote = options.manual ? options.promote : null;
|
|
146
|
+
const result = await pushRelease({
|
|
147
|
+
git,
|
|
148
|
+
gh,
|
|
149
|
+
line,
|
|
150
|
+
lines,
|
|
151
|
+
mode,
|
|
152
|
+
baseline,
|
|
153
|
+
promote,
|
|
154
|
+
fingerprint: content.fingerprint ?? '',
|
|
155
|
+
now,
|
|
156
|
+
});
|
|
157
|
+
if (!result.ok) {
|
|
158
|
+
await summary(`${line.branch}: ${result.reason}`);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const body = releaseBody({
|
|
162
|
+
line,
|
|
163
|
+
version: result.version,
|
|
164
|
+
fingerprint: content.fingerprint ?? '',
|
|
165
|
+
promote,
|
|
166
|
+
changes: content.changes,
|
|
167
|
+
});
|
|
168
|
+
const title = result.version === null ? `Release ${line.branch}` : `Release v${result.version}`;
|
|
169
|
+
if (decision.action === 'update' && open !== null) {
|
|
170
|
+
await gh.rest('PATCH', `/repos/{repo}/pulls/${open.number}`, { title, body });
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const pr = await gh.rest('POST', '/repos/{repo}/pulls', {
|
|
174
|
+
title,
|
|
175
|
+
body,
|
|
176
|
+
head: releaseBranch(line.branch),
|
|
177
|
+
base: line.publishBranch,
|
|
178
|
+
});
|
|
179
|
+
await gh.rest('POST', `/repos/{repo}/issues/${pr.number}/labels`, {
|
|
180
|
+
labels: [RELEASE_LABEL],
|
|
181
|
+
});
|
|
182
|
+
if (collaborative) {
|
|
183
|
+
await gh.graphql('mutation($id: ID!) { enablePullRequestAutoMerge(input: { pullRequestId: $id, mergeMethod: MERGE }) { clientMutationId } }', { id: pr.node_id });
|
|
184
|
+
}
|
|
185
|
+
await summary(`${line.branch}: opened #${pr.number}`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* @param gh - The client
|
|
190
|
+
* @param git - Git access
|
|
191
|
+
* @param pr - The open Release PR
|
|
192
|
+
* @returns What the decision reads about it
|
|
193
|
+
*/
|
|
194
|
+
async function describeOpenPr(gh, git, pr) {
|
|
195
|
+
const reviews = await gh.paginate(`/repos/{repo}/pulls/${pr.number}/reviews`);
|
|
196
|
+
const latest = new Map();
|
|
197
|
+
for (const review of reviews) {
|
|
198
|
+
if (review.user === null ||
|
|
199
|
+
review.user.type !== 'User' ||
|
|
200
|
+
review.state === 'COMMENTED') {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
latest.set(review.user.login, {
|
|
204
|
+
state: review.state,
|
|
205
|
+
at: new Date(review.submitted_at),
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
const approvals = [...latest.values()].filter(({ state }) => state === 'APPROVED');
|
|
209
|
+
const approvedAt = approvals.length === 0
|
|
210
|
+
? null
|
|
211
|
+
: new Date(Math.min(...approvals.map(({ at }) => at.getTime())));
|
|
212
|
+
const baseTime = Number(git.raw('log', '-1', '--format=%ct', `origin/${pr.base.ref}`)) * 1000;
|
|
213
|
+
return {
|
|
214
|
+
number: pr.number,
|
|
215
|
+
fingerprint: markerValue(pr.body, 'fingerprint') ?? '',
|
|
216
|
+
approvedAt,
|
|
217
|
+
baseUpdatedSinceApproval: approvedAt !== null && baseTime > approvedAt.getTime(),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Tells whether the last release of a line is not fully out: a public package
|
|
222
|
+
* whose version on the publish branch is missing from the registry, or for a
|
|
223
|
+
* repository without npm packages, a version without its tag.
|
|
224
|
+
* @param git - Git access
|
|
225
|
+
* @param line - The line
|
|
226
|
+
* @param mode - The publishing mode
|
|
227
|
+
* @returns Whether the release is incomplete
|
|
228
|
+
*/
|
|
229
|
+
async function publishIncomplete(git, line, mode) {
|
|
230
|
+
const rev = `origin/${line.publishBranch}`;
|
|
231
|
+
if (mode === 'custom') {
|
|
232
|
+
const version = versionAt(git, rev);
|
|
233
|
+
return version !== null && !git.tags(rev).includes(`v${version}`);
|
|
234
|
+
}
|
|
235
|
+
for (const pkg of publicPackages(git, rev)) {
|
|
236
|
+
const packument = await fetchPackument(pkg.name);
|
|
237
|
+
if (packument !== null && !packument.versions.includes(pkg.version)) {
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Generates the release commit on top of the line's head as the Release App
|
|
245
|
+
* and force-pushes it to the release branch.
|
|
246
|
+
* @param input - The line and what to release
|
|
247
|
+
* @returns The version, or why there is none
|
|
248
|
+
*/
|
|
249
|
+
async function pushRelease(input) {
|
|
250
|
+
const { git } = input;
|
|
251
|
+
git.raw('checkout', '--force', '--detach', `origin/${input.line.branch}`);
|
|
252
|
+
runShell('yarn install --immutable', git.cwd);
|
|
253
|
+
const generated = generateRelease({
|
|
254
|
+
git,
|
|
255
|
+
mode: input.mode,
|
|
256
|
+
line: input.line,
|
|
257
|
+
lines: input.lines,
|
|
258
|
+
baseline: input.baseline,
|
|
259
|
+
promote: input.promote,
|
|
260
|
+
now: input.now,
|
|
261
|
+
});
|
|
262
|
+
if (!generated.ok) {
|
|
263
|
+
git.raw('checkout', '--force', '--detach', `origin/${input.line.branch}`);
|
|
264
|
+
return generated;
|
|
265
|
+
}
|
|
266
|
+
const appSlug = requireEnv('RELEASE_APP_SLUG');
|
|
267
|
+
const bot = await input.gh.rest('GET', `/users/${encodeURIComponent(`${appSlug}[bot]`)}`);
|
|
268
|
+
git.raw('add', '--all');
|
|
269
|
+
git.raw('-c', `user.name=${appSlug}[bot]`, '-c', `user.email=${bot.id}+${appSlug}[bot]@users.noreply.github.com`, 'commit', '--no-verify', '--message', generated.message);
|
|
270
|
+
const token = requireEnv('RELEASE_APP_TOKEN');
|
|
271
|
+
git.pushAs(token, input.gh.repository, `HEAD:refs/heads/${releaseBranch(input.line.branch)}`);
|
|
272
|
+
return { ok: true, version: generated.version };
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* @param input - The release
|
|
276
|
+
* @returns The Release PR body, with the markers the automation reads
|
|
277
|
+
*/
|
|
278
|
+
function releaseBody(input) {
|
|
279
|
+
return [
|
|
280
|
+
lineMarker(input.line.branch),
|
|
281
|
+
`<!-- release-automation:fingerprint:${input.fingerprint} -->`,
|
|
282
|
+
...(input.promote === null
|
|
283
|
+
? []
|
|
284
|
+
: [`<!-- release-automation:promote:${input.promote} -->`]),
|
|
285
|
+
'',
|
|
286
|
+
input.version === null
|
|
287
|
+
? `Releases the changed packages of \`${input.line.branch}\`.`
|
|
288
|
+
: `Releases v${input.version} from \`${input.line.branch}\`.`,
|
|
289
|
+
'',
|
|
290
|
+
'Merging this pull request publishes the release. It is generated and kept up to date automatically: do not push to it. After the first approval it is frozen; later changes go to the next Release PR.',
|
|
291
|
+
'',
|
|
292
|
+
'### Changes that reach users',
|
|
293
|
+
'',
|
|
294
|
+
...input.changes.map((change) => `- \`${change}\``),
|
|
295
|
+
'',
|
|
296
|
+
].join('\n');
|
|
297
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FULL_CI_WORKFLOW } from './context.js';
|
|
2
|
+
/**
|
|
3
|
+
* Reads the finished Nightly runs of a line: the Full CI runs started by
|
|
4
|
+
* `workflow_dispatch` on the line's branch, newest first. A run that was
|
|
5
|
+
* cancelled or skipped tells nothing about the code and is left out.
|
|
6
|
+
* @param gh - The client
|
|
7
|
+
* @param branch - The branch of the line
|
|
8
|
+
* @returns The runs
|
|
9
|
+
* @example
|
|
10
|
+
* isBlocked(await nightlyRuns(gh, 'dev'));
|
|
11
|
+
*/
|
|
12
|
+
export async function nightlyRuns(gh, branch) {
|
|
13
|
+
const response = await gh.rest('GET', `/repos/{repo}/actions/workflows/${FULL_CI_WORKFLOW}/runs?branch=${encodeURIComponent(branch)}&event=workflow_dispatch&status=completed&per_page=20`);
|
|
14
|
+
return response.workflow_runs
|
|
15
|
+
.filter((run) => run.conclusion === 'success' || run.conclusion === 'failure')
|
|
16
|
+
.map((run) => ({
|
|
17
|
+
sha: run.head_sha,
|
|
18
|
+
conclusion: run.conclusion,
|
|
19
|
+
finishedAt: new Date(run.updated_at),
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { runShell } from '../io/exec.js';
|
|
2
|
+
import { fetchPackument } from '../io/npm.js';
|
|
3
|
+
import { publicPackages, publishModeAt } from '../io/repo.js';
|
|
4
|
+
import { releasePrOrigin } from '../pr-checks.js';
|
|
5
|
+
import { CURRENT_PUBLISH_BRANCH } from '../types.js';
|
|
6
|
+
import { classifiedLines, github, localGit, requireEnv, summary } from './context.js';
|
|
7
|
+
import { generateRelease } from './generate.js';
|
|
8
|
+
import { baselineOf, lineMarker, markerValue } from './release-pr.js';
|
|
9
|
+
/**
|
|
10
|
+
* Normalizes the dates a changelog generator writes, so that a release
|
|
11
|
+
* generated on another day compares equal.
|
|
12
|
+
* @param text - A changelog
|
|
13
|
+
* @returns The changelog with `YYYY-MM-DD` dates replaced
|
|
14
|
+
* @example
|
|
15
|
+
* normalizeDates('## 1.0.0 (2026-11-25)'); // '## 1.0.0 (DATE)'
|
|
16
|
+
*/
|
|
17
|
+
export function normalizeDates(text) {
|
|
18
|
+
return text.replaceAll(/\b\d{4}-\d{2}-\d{2}\b/g, 'DATE');
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Verifies that a pull request into a publish branch is a Release PR the
|
|
22
|
+
* automation generated: the Release App wrote the commit on top of the
|
|
23
|
+
* line's branch, the line's branch contains its parent, and generating the
|
|
24
|
+
* release again from that parent gives the same files. Runs on
|
|
25
|
+
* `pull_request_target`, so the checks of the target branch judge the pull
|
|
26
|
+
* request, and no code of the pull request runs: the generation runs on the
|
|
27
|
+
* parent, which the protected line branch already holds.
|
|
28
|
+
* @param options - The pull request
|
|
29
|
+
* @param options.pr - Its number
|
|
30
|
+
* @throws {Error} When the pull request is not a valid Release PR
|
|
31
|
+
* @example
|
|
32
|
+
* await verifyReleasePrCommand({ pr: 12 });
|
|
33
|
+
*/
|
|
34
|
+
export async function verifyReleasePrCommand(options) {
|
|
35
|
+
const git = localGit();
|
|
36
|
+
const gh = github('GITHUB_TOKEN');
|
|
37
|
+
const appSlug = requireEnv('RELEASE_APP_SLUG');
|
|
38
|
+
const pr = await gh.rest('GET', `/repos/{repo}/pulls/${options.pr}`);
|
|
39
|
+
const { lines } = classifiedLines(git, new Date());
|
|
40
|
+
const line = lines.find((candidate) => candidate.publishBranch === pr.base.ref &&
|
|
41
|
+
(pr.body ?? '').includes(lineMarker(candidate.branch)));
|
|
42
|
+
if (line === undefined) {
|
|
43
|
+
if (pr.base.ref !== CURRENT_PUBLISH_BRANCH) {
|
|
44
|
+
// A line branch other than main also takes ordinary pull requests;
|
|
45
|
+
// the publish step refuses to publish one that this check did not pass.
|
|
46
|
+
await summary(`#${options.pr} is not a Release PR; nothing to verify`);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
throw new Error(`#${options.pr} targets ${pr.base.ref} but is not a Release PR of a line that publishes there`);
|
|
50
|
+
}
|
|
51
|
+
git.raw('fetch', 'origin', `+refs/pull/${options.pr}/head:refs/remotes/pr/${options.pr}`);
|
|
52
|
+
const head = git.sha(`pr/${options.pr}`);
|
|
53
|
+
if (head !== pr.head.sha) {
|
|
54
|
+
throw new Error(`#${options.pr} moved while it was verified; run again`);
|
|
55
|
+
}
|
|
56
|
+
const parent = git.sha(`${head}^1`);
|
|
57
|
+
if (git.raw('rev-list', '--parents', '-n', '1', head).split(' ').length !== 2) {
|
|
58
|
+
throw new Error('The release commit must have one parent');
|
|
59
|
+
}
|
|
60
|
+
if (!git.isAncestor(parent, `origin/${line.branch}`)) {
|
|
61
|
+
throw new Error(`The release commit is not on top of ${line.branch}`);
|
|
62
|
+
}
|
|
63
|
+
const commit = await gh.rest('GET', `/repos/{repo}/commits/${head}`);
|
|
64
|
+
const origin = releasePrOrigin({
|
|
65
|
+
author: pr.user.login,
|
|
66
|
+
commits: [{ author: commit.author?.login ?? null, verified: true }],
|
|
67
|
+
body: pr.body ?? '',
|
|
68
|
+
labels: pr.labels.map(({ name }) => name),
|
|
69
|
+
}, `${appSlug}[bot]`);
|
|
70
|
+
if (!origin.ok) {
|
|
71
|
+
throw new Error(`#${options.pr} was not generated by the Release App: ${origin.reasons.join('; ')}`);
|
|
72
|
+
}
|
|
73
|
+
const promote = markerValue(pr.body, 'promote');
|
|
74
|
+
const mode = publishModeAt(git, `origin/${line.branch}`);
|
|
75
|
+
if (mode !== 'custom') {
|
|
76
|
+
const never = [];
|
|
77
|
+
for (const pkg of publicPackages(git, head)) {
|
|
78
|
+
if ((await fetchPackument(pkg.name)) === null) {
|
|
79
|
+
never.push(pkg.name);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (never.length > 0) {
|
|
83
|
+
throw new Error(`Never published, so no trusted publisher can be set: ${never.join(', ')}. Publish a placeholder by hand and set its trusted publisher first`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
git.raw('checkout', '--force', '--detach', parent);
|
|
87
|
+
runShell('yarn install --immutable', git.cwd);
|
|
88
|
+
const generated = generateRelease({
|
|
89
|
+
git,
|
|
90
|
+
mode,
|
|
91
|
+
line,
|
|
92
|
+
lines,
|
|
93
|
+
baseline: baselineOf(git, line, mode),
|
|
94
|
+
promote,
|
|
95
|
+
now: new Date(),
|
|
96
|
+
});
|
|
97
|
+
if (!generated.ok) {
|
|
98
|
+
throw new Error(`The release cannot be generated again: ${generated.reason}`);
|
|
99
|
+
}
|
|
100
|
+
git.raw('add', '--all');
|
|
101
|
+
const changed = git
|
|
102
|
+
.raw('diff', '--cached', '--name-only', head)
|
|
103
|
+
.split('\n')
|
|
104
|
+
.filter((file) => file !== '');
|
|
105
|
+
const differing = changed.filter((file) => {
|
|
106
|
+
const expected = git.show(head, file);
|
|
107
|
+
const actual = git.raw('show', `:${file}`);
|
|
108
|
+
return !(expected !== null &&
|
|
109
|
+
file.endsWith('CHANGELOG.md') &&
|
|
110
|
+
normalizeDates(expected) === normalizeDates(actual));
|
|
111
|
+
});
|
|
112
|
+
if (differing.length > 0) {
|
|
113
|
+
throw new Error(`#${options.pr} differs from a fresh generation in: ${differing.join(', ')}`);
|
|
114
|
+
}
|
|
115
|
+
await summary(`#${options.pr}: a valid Release PR of ${line.branch}`);
|
|
116
|
+
}
|