@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,147 @@
|
|
|
1
|
+
import { parse as parseYaml } from 'yaml';
|
|
2
|
+
import { formatDate } from './support-end.js';
|
|
3
|
+
import { CURRENT_PUBLISH_BRANCH } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* The description of the Renovate rule that limits previous lines to
|
|
6
|
+
* vulnerability fixes and lock file maintenance. `lines check` finds the rule
|
|
7
|
+
* by it.
|
|
8
|
+
*/
|
|
9
|
+
export const PREVIOUS_LINES_RULE = 'release-automation: previous lines take vulnerability fixes and lock file maintenance only';
|
|
10
|
+
/**
|
|
11
|
+
* The description of the Renovate rule that stops lock file maintenance on
|
|
12
|
+
* previous lines whose successor is stable.
|
|
13
|
+
*/
|
|
14
|
+
export const LOCKFILE_STOP_RULE = 'release-automation: lock file maintenance stopped on security-only lines';
|
|
15
|
+
/** The name of the job that aggregates the checks of the Minimum CI. */
|
|
16
|
+
export const CI_OK_JOB = 'ci-ok';
|
|
17
|
+
/**
|
|
18
|
+
* Compares the files that name branches with the lines, and reports every
|
|
19
|
+
* difference. Only what the files state is compared: a check whose result
|
|
20
|
+
* changes with the date would fail unrelated pull requests on the day a
|
|
21
|
+
* line's support ends. The support end dates come from tags, which do not
|
|
22
|
+
* change with the date.
|
|
23
|
+
* @param lines - The lines, as `classify` derives them
|
|
24
|
+
* @param files - The files to compare
|
|
25
|
+
* @returns The differences; empty when everything agrees
|
|
26
|
+
* @example
|
|
27
|
+
* checkLines(classify(facts).lines, files); // []
|
|
28
|
+
*/
|
|
29
|
+
export function checkLines(lines, files) {
|
|
30
|
+
const problems = [];
|
|
31
|
+
const lineBranches = lines.map(({ branch }) => branch);
|
|
32
|
+
const previous = lines
|
|
33
|
+
.filter(({ role }) => role === 'previous')
|
|
34
|
+
.map(({ branch }) => branch);
|
|
35
|
+
const publishBranches = lines.some(({ role }) => role === 'current')
|
|
36
|
+
? [...lineBranches, CURRENT_PUBLISH_BRANCH]
|
|
37
|
+
: lineBranches;
|
|
38
|
+
const renovate = asRecord(files.renovate);
|
|
39
|
+
const bases = asStrings(renovate['baseBranchPatterns']);
|
|
40
|
+
if (!sameSet(bases, lineBranches)) {
|
|
41
|
+
problems.push(`renovate.json baseBranchPatterns is [${bases.join(', ')}]; the lines are [${lineBranches.join(', ')}]`);
|
|
42
|
+
}
|
|
43
|
+
const rules = Array.isArray(renovate['packageRules'])
|
|
44
|
+
? renovate['packageRules'].map(asRecord)
|
|
45
|
+
: [];
|
|
46
|
+
const previousRule = rules.find((rule) => rule['description'] === PREVIOUS_LINES_RULE);
|
|
47
|
+
const previousRuleBranches = asStrings(previousRule?.['matchBaseBranches']);
|
|
48
|
+
if (previous.length > 0 && (!previousRule || previousRule['enabled'] !== false)) {
|
|
49
|
+
problems.push(`renovate.json has no packageRule "${PREVIOUS_LINES_RULE}" with enabled: false`);
|
|
50
|
+
}
|
|
51
|
+
else if (!sameSet(previousRuleBranches, previous)) {
|
|
52
|
+
problems.push(`renovate.json "${PREVIOUS_LINES_RULE}" matches [${previousRuleBranches.join(', ')}]; the previous lines are [${previous.join(', ')}]`);
|
|
53
|
+
}
|
|
54
|
+
const stopRule = rules.find((rule) => rule['description'] === LOCKFILE_STOP_RULE);
|
|
55
|
+
for (const branch of asStrings(stopRule?.['matchBaseBranches'])) {
|
|
56
|
+
if (!previous.includes(branch)) {
|
|
57
|
+
problems.push(`renovate.json "${LOCKFILE_STOP_RULE}" names ${branch}, which is not a previous line`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
let ciFound = false;
|
|
61
|
+
for (const workflow of files.workflows) {
|
|
62
|
+
let doc;
|
|
63
|
+
try {
|
|
64
|
+
doc = asRecord(parseYaml(workflow.content));
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
problems.push(`${workflow.path}: ${String(error)}`);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const on = asRecord(doc['on']);
|
|
71
|
+
for (const event of ['pull_request', 'pull_request_target', 'push']) {
|
|
72
|
+
for (const branch of asStrings(asRecord(on[event])['branches'])) {
|
|
73
|
+
if (!publishBranches.includes(branch) && !branch.includes('*')) {
|
|
74
|
+
problems.push(`${workflow.path}: on.${event}.branches names ${branch}, which is not a line branch`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (CI_OK_JOB in asRecord(doc['jobs'])) {
|
|
79
|
+
ciFound = true;
|
|
80
|
+
const prBranches = asStrings(asRecord(on['pull_request'])['branches']);
|
|
81
|
+
for (const branch of publishBranches) {
|
|
82
|
+
if (!prBranches.includes(branch)) {
|
|
83
|
+
problems.push(`${workflow.path}: on.pull_request.branches lacks ${branch}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!('merge_group' in on)) {
|
|
87
|
+
problems.push(`${workflow.path}: the Minimum CI does not run on merge_group`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (!ciFound) {
|
|
92
|
+
problems.push(`No workflow has a "${CI_OK_JOB}" job`);
|
|
93
|
+
}
|
|
94
|
+
if (files.securityMd === null) {
|
|
95
|
+
problems.push('SECURITY.md does not exist');
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
const rows = files.securityMd.split('\n');
|
|
99
|
+
for (const line of lines) {
|
|
100
|
+
if (line.role === 'current' && line.stage === null) {
|
|
101
|
+
// Lerna independent mode: the packages have majors of their own, so
|
|
102
|
+
// SECURITY.md states its support without a version table.
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const row = rows.find((text) => new RegExp(`\\bv${line.major}\\b`).test(text));
|
|
106
|
+
if (row === undefined) {
|
|
107
|
+
problems.push(`SECURITY.md does not list v${line.major}`);
|
|
108
|
+
}
|
|
109
|
+
else if (line.supportEnd !== null && !row.includes(formatDate(line.supportEnd))) {
|
|
110
|
+
problems.push(`SECURITY.md does not state ${formatDate(line.supportEnd)} as the end of v${line.major}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (!files.preCommit?.includes('release-automation guard')) {
|
|
115
|
+
problems.push('.husky/pre-commit does not run "release-automation guard"');
|
|
116
|
+
}
|
|
117
|
+
return problems;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @param value - Any value
|
|
121
|
+
* @returns The value as a record, or an empty one
|
|
122
|
+
*/
|
|
123
|
+
function asRecord(value) {
|
|
124
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
125
|
+
? value
|
|
126
|
+
: {};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* @param value - Any value
|
|
130
|
+
* @returns The strings of an array (a single string becomes one), or none
|
|
131
|
+
*/
|
|
132
|
+
function asStrings(value) {
|
|
133
|
+
if (typeof value === 'string') {
|
|
134
|
+
return [value];
|
|
135
|
+
}
|
|
136
|
+
return Array.isArray(value)
|
|
137
|
+
? value.filter((item) => typeof item === 'string')
|
|
138
|
+
: [];
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* @param a - A list
|
|
142
|
+
* @param b - Another list
|
|
143
|
+
* @returns Whether both hold the same items
|
|
144
|
+
*/
|
|
145
|
+
function sameSet(a, b) {
|
|
146
|
+
return a.length === b.length && a.every((item) => b.includes(item));
|
|
147
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { countsForRelease } from './conventional-commit.js';
|
|
2
|
+
import { formatVersion, parseVersion } from './semver.js';
|
|
3
|
+
import { PRERELEASE_IDS } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Decides the next version of a line.
|
|
6
|
+
*
|
|
7
|
+
* - A prerelease stays in its stage and takes the next number. Moving to the
|
|
8
|
+
* next stage (alpha → beta → rc → stable) happens only when `promote`
|
|
9
|
+
* names that stage.
|
|
10
|
+
* - A stable version takes a major bump for a breaking change (a minor bump
|
|
11
|
+
* in major 0), a minor bump for a feature, and a patch bump otherwise.
|
|
12
|
+
* - A version above the major of the line is refused: whether to start a
|
|
13
|
+
* new major is a person's decision, not a commit's.
|
|
14
|
+
* @param input - The current version, the commits and the requested stage
|
|
15
|
+
* @returns The next version, or why there is none
|
|
16
|
+
* @example
|
|
17
|
+
* nextVersion({ current: '1.2.3', commits: [parseCommit('feat: add x')], promote: null, lineMajor: 1 });
|
|
18
|
+
* // { ok: true, version: '1.3.0' }
|
|
19
|
+
*/
|
|
20
|
+
export function nextVersion(input) {
|
|
21
|
+
const current = parseVersion(input.current);
|
|
22
|
+
const counted = input.commits.filter((commit) => countsForRelease(commit));
|
|
23
|
+
if (current.pre !== null) {
|
|
24
|
+
if (input.promote === null) {
|
|
25
|
+
if (counted.length === 0) {
|
|
26
|
+
return nothing();
|
|
27
|
+
}
|
|
28
|
+
return ok({ ...current, pre: { id: current.pre.id, n: current.pre.n + 1 } });
|
|
29
|
+
}
|
|
30
|
+
const from = PRERELEASE_IDS.indexOf(current.pre.id);
|
|
31
|
+
const to = input.promote === 'stable'
|
|
32
|
+
? PRERELEASE_IDS.length
|
|
33
|
+
: PRERELEASE_IDS.indexOf(input.promote);
|
|
34
|
+
if (to !== from + 1) {
|
|
35
|
+
return {
|
|
36
|
+
ok: false,
|
|
37
|
+
reason: 'invalid-promotion',
|
|
38
|
+
message: `${input.current} can move to ${PRERELEASE_IDS[from + 1] ?? 'stable'} only, not ${input.promote}`,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return ok({
|
|
42
|
+
...current,
|
|
43
|
+
pre: input.promote === 'stable' ? null : { id: input.promote, n: 0 },
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (input.promote !== null) {
|
|
47
|
+
return {
|
|
48
|
+
ok: false,
|
|
49
|
+
reason: 'invalid-promotion',
|
|
50
|
+
message: `${input.current} is stable; there is no stage to move to`,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (counted.length === 0) {
|
|
54
|
+
return nothing();
|
|
55
|
+
}
|
|
56
|
+
const breaking = counted.some((commit) => commit.breaking);
|
|
57
|
+
const feature = counted.some((commit) => commit.type === 'feat');
|
|
58
|
+
const next = breaking && current.major > 0
|
|
59
|
+
? { major: current.major + 1, minor: 0, patch: 0, pre: null }
|
|
60
|
+
: breaking || feature
|
|
61
|
+
? { ...current, minor: current.minor + 1, patch: 0 }
|
|
62
|
+
: { ...current, patch: current.patch + 1 };
|
|
63
|
+
return ok(next);
|
|
64
|
+
/**
|
|
65
|
+
* @param version - The candidate
|
|
66
|
+
* @returns The candidate, or a refusal when it leaves the line
|
|
67
|
+
*/
|
|
68
|
+
function ok(version) {
|
|
69
|
+
if (version.major > input.lineMajor) {
|
|
70
|
+
return {
|
|
71
|
+
ok: false,
|
|
72
|
+
reason: 'exceeds-line-major',
|
|
73
|
+
message: `${formatVersion(version)} is above v${input.lineMajor}; a breaking change on this line needs a person's decision`,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return { ok: true, version: formatVersion(version) };
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @returns The result for no counted commits
|
|
80
|
+
*/
|
|
81
|
+
function nothing() {
|
|
82
|
+
return {
|
|
83
|
+
ok: false,
|
|
84
|
+
reason: 'nothing-to-release',
|
|
85
|
+
message: `No commit since ${input.current} counts toward a release`,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
package/dist/nightly.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decides whether the Nightly runs the Full CI of a line, or reuses the
|
|
3
|
+
* last result: a success on the same commit is reused, a failure is tested
|
|
4
|
+
* again, a new commit is always tested.
|
|
5
|
+
* @param headSha - The head of the line's branch
|
|
6
|
+
* @param runs - The finished runs of the line, newest first
|
|
7
|
+
* @param force - Whether a person asked to run anyway
|
|
8
|
+
* @returns `run` or `reuse`
|
|
9
|
+
* @example
|
|
10
|
+
* nightlyAction('abc', [{ sha: 'abc', conclusion: 'success', finishedAt: new Date() }], false); // 'reuse'
|
|
11
|
+
*/
|
|
12
|
+
export function nightlyAction(headSha, runs, force) {
|
|
13
|
+
const last = runs[0];
|
|
14
|
+
if (force || last === undefined) {
|
|
15
|
+
return 'run';
|
|
16
|
+
}
|
|
17
|
+
return last.sha === headSha && last.conclusion === 'success' ? 'reuse' : 'run';
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Tells whether a line is blocked by its Nightly: the newest finished run
|
|
21
|
+
* failed. Only a later success clears it, whether it ran on schedule or by
|
|
22
|
+
* hand; the state of the Issue does not count.
|
|
23
|
+
* @param runs - The finished runs of the line, newest first
|
|
24
|
+
* @returns Whether new Release PRs of the line are stopped
|
|
25
|
+
* @example
|
|
26
|
+
* isBlocked([{ sha: 'abc', conclusion: 'failure', finishedAt: new Date() }]); // true
|
|
27
|
+
*/
|
|
28
|
+
export function isBlocked(runs) {
|
|
29
|
+
return runs[0]?.conclusion === 'failure';
|
|
30
|
+
}
|
|
31
|
+
/** How long without a finished Nightly before it counts as stopped. */
|
|
32
|
+
export const NIGHTLY_STALE_MS = 2 * 24 * 60 * 60 * 1000;
|
|
33
|
+
/**
|
|
34
|
+
* Tells whether the Nightly of a line stopped running. GitHub disables the
|
|
35
|
+
* schedule of a public repository after 60 days without activity, and a
|
|
36
|
+
* stopped Nightly would leave the gate on stale results.
|
|
37
|
+
* @param runs - The finished runs of the line, newest first
|
|
38
|
+
* @param now - The current time
|
|
39
|
+
* @returns Whether the newest run is older than {@link NIGHTLY_STALE_MS}, or there is none
|
|
40
|
+
* @example
|
|
41
|
+
* isStale([], new Date()); // true
|
|
42
|
+
*/
|
|
43
|
+
export function isStale(runs, now) {
|
|
44
|
+
const last = runs[0];
|
|
45
|
+
return (last === undefined || now.getTime() - last.finishedAt.getTime() > NIGHTLY_STALE_MS);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The marker that identifies the `release-blocker` Issue of a line.
|
|
49
|
+
* @param branch - The branch of the line
|
|
50
|
+
* @returns An HTML comment placed at the top of the Issue body
|
|
51
|
+
* @example
|
|
52
|
+
* blockerMarker('v3'); // '<!-- release-blocker:v3 -->'
|
|
53
|
+
*/
|
|
54
|
+
export function blockerMarker(branch) {
|
|
55
|
+
return `<!-- release-blocker:${branch} -->`;
|
|
56
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** The login of Renovate's pull requests and commits. */
|
|
2
|
+
export const RENOVATE_LOGIN = 'renovate[bot]';
|
|
3
|
+
/** The label of a Renovate pull request that a person reviews. */
|
|
4
|
+
export const MANUAL_REVIEW_LABEL = 'manual-review';
|
|
5
|
+
/**
|
|
6
|
+
* Tells whether the Approve App may approve a Renovate pull request: Renovate
|
|
7
|
+
* opened it, every commit is Renovate's and signed, Renovate merges it by
|
|
8
|
+
* itself, and it is not one a person reviews (the major update of a package
|
|
9
|
+
* the repository excepts from automerge carries {@link MANUAL_REVIEW_LABEL}).
|
|
10
|
+
* A pull request someone pushed to is a person's change and gets no approval.
|
|
11
|
+
* @param pr - The pull request
|
|
12
|
+
* @returns Whether to approve, and why not
|
|
13
|
+
* @example
|
|
14
|
+
* canApproveRenovatePr(pr).ok; // true
|
|
15
|
+
*/
|
|
16
|
+
export function canApproveRenovatePr(pr) {
|
|
17
|
+
const reasons = [];
|
|
18
|
+
if (pr.author !== RENOVATE_LOGIN) {
|
|
19
|
+
reasons.push(`opened by ${pr.author}, not ${RENOVATE_LOGIN}`);
|
|
20
|
+
}
|
|
21
|
+
if (pr.commits.length === 0) {
|
|
22
|
+
reasons.push('has no commits');
|
|
23
|
+
}
|
|
24
|
+
for (const [index, commit] of pr.commits.entries()) {
|
|
25
|
+
if (commit.author !== RENOVATE_LOGIN) {
|
|
26
|
+
reasons.push(`commit ${index + 1} is by ${commit.author ?? 'an unknown author'}`);
|
|
27
|
+
}
|
|
28
|
+
else if (!commit.verified) {
|
|
29
|
+
reasons.push(`commit ${index + 1} is not signed`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (!/\*\*Automerge\*\*: Enabled\./.test(pr.body)) {
|
|
33
|
+
reasons.push('Renovate does not merge it by itself');
|
|
34
|
+
}
|
|
35
|
+
if (pr.labels.includes(MANUAL_REVIEW_LABEL)) {
|
|
36
|
+
reasons.push(`labelled ${MANUAL_REVIEW_LABEL}`);
|
|
37
|
+
}
|
|
38
|
+
return { ok: reasons.length === 0, reasons };
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Tells whether a pull request comes from the Release PR generator: the
|
|
42
|
+
* Release App opened it and wrote every commit. Whether its content matches
|
|
43
|
+
* a fresh generation is checked separately, against the target branch.
|
|
44
|
+
* @param pr - The pull request
|
|
45
|
+
* @param appLogin - The login of the Release App (`<slug>[bot]`)
|
|
46
|
+
* @returns Whether its origin is the generator, and why not
|
|
47
|
+
* @example
|
|
48
|
+
* releasePrOrigin(pr, 'd-zero-release[bot]').ok; // true
|
|
49
|
+
*/
|
|
50
|
+
export function releasePrOrigin(pr, appLogin) {
|
|
51
|
+
const reasons = [];
|
|
52
|
+
if (pr.author !== appLogin) {
|
|
53
|
+
reasons.push(`opened by ${pr.author}, not ${appLogin}`);
|
|
54
|
+
}
|
|
55
|
+
if (pr.commits.length === 0) {
|
|
56
|
+
reasons.push('has no commits');
|
|
57
|
+
}
|
|
58
|
+
for (const [index, commit] of pr.commits.entries()) {
|
|
59
|
+
if (commit.author !== appLogin) {
|
|
60
|
+
reasons.push(`commit ${index + 1} is by ${commit.author ?? 'an unknown author'}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return { ok: reasons.length === 0, reasons };
|
|
64
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
/** The `package.json` fields whose change reaches a consumer. */
|
|
3
|
+
export const PACKAGE_FIELDS = [
|
|
4
|
+
'dependencies',
|
|
5
|
+
'peerDependencies',
|
|
6
|
+
'peerDependenciesMeta',
|
|
7
|
+
'optionalDependencies',
|
|
8
|
+
'engines',
|
|
9
|
+
'bin',
|
|
10
|
+
'exports',
|
|
11
|
+
'main',
|
|
12
|
+
'module',
|
|
13
|
+
'types',
|
|
14
|
+
'type',
|
|
15
|
+
'files',
|
|
16
|
+
];
|
|
17
|
+
const NOT_SHIPPED_CONTENT = [
|
|
18
|
+
/\.md$/i,
|
|
19
|
+
/\.spec\.[cm]?[jt]sx?$/,
|
|
20
|
+
/(?:^|\/)(?:__snapshots__|__mocks__|fixtures?|test)\//,
|
|
21
|
+
/^\.(?:github|husky|claude|yarn|vscode)\//,
|
|
22
|
+
/^(?:yarn\.lock|pnpm-lock\.yaml|package-lock\.json|\.yarnrc\.yml)$/,
|
|
23
|
+
];
|
|
24
|
+
/** Files npm puts in every tarball, whatever `files` says. */
|
|
25
|
+
const ALWAYS_SHIPPED = /^(?:package\.json|readme(?:\.[^/]*)?|licen[cs]e(?:\.[^/]*)?)$/i;
|
|
26
|
+
/**
|
|
27
|
+
* Compiles `.npmignore` lines with the `.gitignore` semantics npm applies: a
|
|
28
|
+
* pattern without an inner slash matches a name at any depth, a trailing
|
|
29
|
+
* slash matches a directory only, the last matching line wins.
|
|
30
|
+
* @param source - The content of an `.npmignore`
|
|
31
|
+
* @returns A predicate that tells whether a path relative to the package is left out of the tarball
|
|
32
|
+
* @example
|
|
33
|
+
* npmIgnore('src/\n')('src/a.ts'); // true
|
|
34
|
+
*/
|
|
35
|
+
export function npmIgnore(source) {
|
|
36
|
+
const rules = [];
|
|
37
|
+
for (const raw of source.split('\n')) {
|
|
38
|
+
const line = raw.trim();
|
|
39
|
+
if (line === '' || line.startsWith('#')) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const negate = line.startsWith('!');
|
|
43
|
+
let body = negate ? line.slice(1) : line;
|
|
44
|
+
const directoryOnly = body.endsWith('/');
|
|
45
|
+
body = body.replace(/\/+$/, '');
|
|
46
|
+
const anchored = body.includes('/');
|
|
47
|
+
body = body.replace(/^\/+/, '');
|
|
48
|
+
const glob = body
|
|
49
|
+
.split('**')
|
|
50
|
+
.map((part) => part
|
|
51
|
+
.split('*')
|
|
52
|
+
.map((piece) => piece.replaceAll(/[.+^${}()|[\]\\?]/g, String.raw `\$&`))
|
|
53
|
+
.join('[^/]*'))
|
|
54
|
+
.join('.*');
|
|
55
|
+
const tail = directoryOnly ? '/.*' : '(?:/.*)?';
|
|
56
|
+
rules.push({
|
|
57
|
+
negate,
|
|
58
|
+
pattern: new RegExp(`${anchored ? '^' : '(?:^|/)'}${glob}${tail}$`),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return (path) => {
|
|
62
|
+
let ignored = false;
|
|
63
|
+
for (const rule of rules) {
|
|
64
|
+
if (rule.pattern.test(path)) {
|
|
65
|
+
ignored = !rule.negate;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return ignored;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Lists the consumer-facing `package.json` fields that differ.
|
|
73
|
+
* @param before - `package.json` at the older revision, or `null`
|
|
74
|
+
* @param after - `package.json` at the newer revision, or `null`
|
|
75
|
+
* @returns `package.json#<field>` for a changed scalar field,
|
|
76
|
+
* `package.json#<field>.<key>` for a changed entry of an object field
|
|
77
|
+
* @example
|
|
78
|
+
* manifestChanges('{"dependencies":{"a":"1"}}', '{"dependencies":{"a":"2"}}'); // ['package.json#dependencies.a']
|
|
79
|
+
*/
|
|
80
|
+
export function manifestChanges(before, after) {
|
|
81
|
+
const a = JSON.parse(before ?? '{}');
|
|
82
|
+
const b = JSON.parse(after ?? '{}');
|
|
83
|
+
const changes = [];
|
|
84
|
+
for (const field of PACKAGE_FIELDS) {
|
|
85
|
+
const x = a[field];
|
|
86
|
+
const y = b[field];
|
|
87
|
+
if (isRecord(x) || isRecord(y)) {
|
|
88
|
+
const xs = isRecord(x) ? x : {};
|
|
89
|
+
const ys = isRecord(y) ? y : {};
|
|
90
|
+
for (const key of [
|
|
91
|
+
...new Set([...Object.keys(xs), ...Object.keys(ys)]),
|
|
92
|
+
].toSorted()) {
|
|
93
|
+
if (JSON.stringify(xs[key]) !== JSON.stringify(ys[key])) {
|
|
94
|
+
changes.push(`package.json#${field}.${key}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else if (JSON.stringify(x) !== JSON.stringify(y)) {
|
|
99
|
+
changes.push(`package.json#${field}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return changes;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Finds what a line holds that reaches users since a baseline revision.
|
|
106
|
+
*
|
|
107
|
+
* What counts is decided per public package (or the root package when the
|
|
108
|
+
* repository has no `packages/` directory):
|
|
109
|
+
*
|
|
110
|
+
* - A file `npm pack` ships (not matched by `.npmignore`), or a build input
|
|
111
|
+
* under `src/`, other than Markdown, specs and test data
|
|
112
|
+
* - In `package.json`, the fields in {@link PACKAGE_FIELDS}
|
|
113
|
+
*
|
|
114
|
+
* A carrier is a first-parent commit of the branch that brought in a change
|
|
115
|
+
* still present in the cumulative diff, so its date is when the change landed
|
|
116
|
+
* on the branch, and the merge that brings a release back does not count.
|
|
117
|
+
* @param git - Read access to the repository
|
|
118
|
+
* @param baseline - The revision of the last release (a tag), or `null` before the first
|
|
119
|
+
* @param head - The revision of the line's branch
|
|
120
|
+
* @returns The release content
|
|
121
|
+
* @example
|
|
122
|
+
* releaseContent(git, 'v3.5.0', 'origin/v3').fingerprint;
|
|
123
|
+
*/
|
|
124
|
+
export function releaseContent(git, baseline, head) {
|
|
125
|
+
const from = baseline ?? emptyTree();
|
|
126
|
+
const packages = publicPackages(git, head);
|
|
127
|
+
const cumulative = countedChanges(git, packages, from, head);
|
|
128
|
+
if (cumulative.size === 0) {
|
|
129
|
+
return { fingerprint: null, carriers: [], changes: [] };
|
|
130
|
+
}
|
|
131
|
+
const carriers = baseline === null
|
|
132
|
+
? []
|
|
133
|
+
: git.firstParents(baseline, head).filter((commit) => {
|
|
134
|
+
const own = countedChanges(git, packages, `${commit.sha}^1`, commit.sha);
|
|
135
|
+
return [...own].some((key) => cumulative.has(key));
|
|
136
|
+
});
|
|
137
|
+
const changes = [...cumulative].toSorted();
|
|
138
|
+
const hash = createHash('sha256');
|
|
139
|
+
for (const key of changes) {
|
|
140
|
+
const [path, field] = key.split('#');
|
|
141
|
+
hash.update(key);
|
|
142
|
+
hash.update('\0');
|
|
143
|
+
if (field === undefined) {
|
|
144
|
+
hash.update(git.blobId(head, path) ?? 'deleted');
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
const [name, sub] = field.split('.');
|
|
148
|
+
const manifest = JSON.parse(git.show(head, path) ?? '{}');
|
|
149
|
+
const value = manifest[name];
|
|
150
|
+
hash.update(JSON.stringify(sub === undefined || !isRecord(value) ? value : value[sub]) ??
|
|
151
|
+
'deleted');
|
|
152
|
+
}
|
|
153
|
+
hash.update('\n');
|
|
154
|
+
}
|
|
155
|
+
return { fingerprint: hash.digest('hex'), carriers, changes };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The id of git's empty tree, the baseline of a line with no release yet.
|
|
159
|
+
* @returns The object id
|
|
160
|
+
*/
|
|
161
|
+
function emptyTree() {
|
|
162
|
+
return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* @param git - Read access
|
|
166
|
+
* @param rev - A revision
|
|
167
|
+
* @returns The public packages at the revision
|
|
168
|
+
*/
|
|
169
|
+
function publicPackages(git, rev) {
|
|
170
|
+
const manifests = git
|
|
171
|
+
.listFiles(rev, 'packages')
|
|
172
|
+
.filter((file) => /^packages\/(?:@[^/]+\/)?[^/]+\/package\.json$/.test(file));
|
|
173
|
+
const candidates = manifests.length > 0 ? manifests : ['package.json'];
|
|
174
|
+
return candidates.flatMap((file) => {
|
|
175
|
+
const manifest = JSON.parse(git.show(rev, file) ?? '{}');
|
|
176
|
+
if (manifest.private === true || !manifest.name) {
|
|
177
|
+
return [];
|
|
178
|
+
}
|
|
179
|
+
const root = file === 'package.json' ? '' : file.slice(0, -'/package.json'.length);
|
|
180
|
+
const prefix = root === '' ? '' : `${root}/`;
|
|
181
|
+
if (Array.isArray(manifest.files)) {
|
|
182
|
+
// `files` lists what ships; matching it is the inverse of an ignore list.
|
|
183
|
+
const listed = npmIgnore(manifest.files
|
|
184
|
+
.filter((entry) => typeof entry === 'string')
|
|
185
|
+
.join('\n'));
|
|
186
|
+
return [
|
|
187
|
+
{ root, ignored: (path) => !ALWAYS_SHIPPED.test(path) && !listed(path) },
|
|
188
|
+
];
|
|
189
|
+
}
|
|
190
|
+
return [{ root, ignored: npmIgnore(git.show(rev, `${prefix}.npmignore`) ?? '') }];
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* @param git - Read access
|
|
195
|
+
* @param packages - The public packages
|
|
196
|
+
* @param from - The older revision
|
|
197
|
+
* @param to - The newer revision
|
|
198
|
+
* @returns The counted changes between two revisions
|
|
199
|
+
*/
|
|
200
|
+
function countedChanges(git, packages, from, to) {
|
|
201
|
+
const keys = new Set();
|
|
202
|
+
const scope = packages.some(({ root }) => root === '') ? '.' : 'packages';
|
|
203
|
+
for (const file of git.diffNames(from, to, scope)) {
|
|
204
|
+
const pkg = packages.find(({ root }) => root === '' || file.startsWith(`${root}/`));
|
|
205
|
+
if (!pkg) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const prefix = pkg.root === '' ? '' : `${pkg.root}/`;
|
|
209
|
+
const rel = file.slice(prefix.length);
|
|
210
|
+
if (rel === 'package.json') {
|
|
211
|
+
for (const change of manifestChanges(git.show(from, file), git.show(to, file))) {
|
|
212
|
+
keys.add(`${prefix}${change}`);
|
|
213
|
+
}
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (NOT_SHIPPED_CONTENT.some((pattern) => pattern.test(rel))) {
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (rel.startsWith('src/') || !pkg.ignored(rel)) {
|
|
220
|
+
keys.add(`${prefix}${rel}`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return keys;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* @param value - Any value
|
|
227
|
+
* @returns Whether it is a plain object
|
|
228
|
+
*/
|
|
229
|
+
function isRecord(value) {
|
|
230
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
231
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** How long a change that reaches users waits before a Release PR is opened for it. */
|
|
2
|
+
export const RELEASE_WAIT_MS = 3 * 24 * 60 * 60 * 1000;
|
|
3
|
+
/**
|
|
4
|
+
* Decides what happens to the Release PR of a line. The order of the checks
|
|
5
|
+
* is the order of precedence:
|
|
6
|
+
*
|
|
7
|
+
* 1. A release that is not fully published, or not yet brought back to the
|
|
8
|
+
* development branch, stops every new Release PR.
|
|
9
|
+
* 2. An approved Release PR is frozen. It is replaced when its target branch
|
|
10
|
+
* moved or a security fix landed after the approval; the successor waits
|
|
11
|
+
* while the Nightly of the line fails.
|
|
12
|
+
* 3. An unapproved Release PR is updated when the release content changed.
|
|
13
|
+
* 4. No new Release PR opens while the Nightly of the line fails.
|
|
14
|
+
* 5. A person's run opens one at once. A scheduled run does not reopen a
|
|
15
|
+
* release a person declined, gathers security fixes for the batch window,
|
|
16
|
+
* and otherwise waits three days from the oldest change.
|
|
17
|
+
* @param input - The state of the line
|
|
18
|
+
* @returns The decision
|
|
19
|
+
* @example
|
|
20
|
+
* decideReleasePr({ ...input, userFacingSince: new Date(Date.now() - 4 * 86_400_000) }); // { action: 'create' }
|
|
21
|
+
*/
|
|
22
|
+
export function decideReleasePr(input) {
|
|
23
|
+
if (input.publishIncomplete) {
|
|
24
|
+
return { action: 'none', reason: 'publish-incomplete' };
|
|
25
|
+
}
|
|
26
|
+
if (input.syncPending) {
|
|
27
|
+
return { action: 'none', reason: 'sync-pending' };
|
|
28
|
+
}
|
|
29
|
+
const pr = input.openPr;
|
|
30
|
+
if (pr !== null && pr.approvedAt !== null) {
|
|
31
|
+
const approvedAt = pr.approvedAt.getTime();
|
|
32
|
+
const securityAfterApproval = input.securityLandedAt.some((date) => date.getTime() > approvedAt);
|
|
33
|
+
if (pr.baseUpdatedSinceApproval || securityAfterApproval) {
|
|
34
|
+
return { action: 'replace', openSuccessor: !input.nightlyFailing };
|
|
35
|
+
}
|
|
36
|
+
return { action: 'none', reason: 'frozen' };
|
|
37
|
+
}
|
|
38
|
+
if (input.fingerprint === null) {
|
|
39
|
+
return { action: 'none', reason: 'nothing-to-release' };
|
|
40
|
+
}
|
|
41
|
+
if (pr !== null) {
|
|
42
|
+
return pr.fingerprint === input.fingerprint
|
|
43
|
+
? { action: 'none', reason: 'up-to-date' }
|
|
44
|
+
: { action: 'update' };
|
|
45
|
+
}
|
|
46
|
+
if (input.nightlyFailing) {
|
|
47
|
+
return { action: 'none', reason: 'nightly-failing' };
|
|
48
|
+
}
|
|
49
|
+
if (input.manual) {
|
|
50
|
+
return { action: 'create' };
|
|
51
|
+
}
|
|
52
|
+
if (input.declinedFingerprints.includes(input.fingerprint)) {
|
|
53
|
+
return { action: 'none', reason: 'declined' };
|
|
54
|
+
}
|
|
55
|
+
const now = input.now.getTime();
|
|
56
|
+
if (input.securityLandedAt.length > 0) {
|
|
57
|
+
const first = Math.min(...input.securityLandedAt.map((date) => date.getTime()));
|
|
58
|
+
return now - first >= input.securityBatchMinutes * 60 * 1000
|
|
59
|
+
? { action: 'create' }
|
|
60
|
+
: { action: 'none', reason: 'security-batching' };
|
|
61
|
+
}
|
|
62
|
+
if (input.userFacingSince !== null &&
|
|
63
|
+
now - input.userFacingSince.getTime() >= RELEASE_WAIT_MS) {
|
|
64
|
+
return { action: 'create' };
|
|
65
|
+
}
|
|
66
|
+
return { action: 'none', reason: 'waiting' };
|
|
67
|
+
}
|
package/dist/retry.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** How many times a publish is tried in all. */
|
|
2
|
+
export const PUBLISH_ATTEMPTS = 3;
|
|
3
|
+
/**
|
|
4
|
+
* Runs a task until it succeeds, at most `attempts` times, waiting longer
|
|
5
|
+
* between each try (`baseDelayMs`, then twice that, and so on).
|
|
6
|
+
* @param task - The task; it receives the attempt number, starting at 1
|
|
7
|
+
* @param options - The limits
|
|
8
|
+
* @param options.attempts - Total tries
|
|
9
|
+
* @param options.baseDelayMs - The first wait
|
|
10
|
+
* @param options.sleep - Waits; replaced in tests
|
|
11
|
+
* @returns The task's result
|
|
12
|
+
* @throws {Error} The last error when every try failed
|
|
13
|
+
* @example
|
|
14
|
+
* await retry((attempt) => publish(), { attempts: 3, baseDelayMs: 30_000 });
|
|
15
|
+
*/
|
|
16
|
+
export async function retry(task, options) {
|
|
17
|
+
const sleep = options.sleep ??
|
|
18
|
+
((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
19
|
+
let lastError;
|
|
20
|
+
for (let attempt = 1; attempt <= options.attempts; attempt++) {
|
|
21
|
+
try {
|
|
22
|
+
return await task(attempt);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
lastError = error;
|
|
26
|
+
if (attempt < options.attempts) {
|
|
27
|
+
await sleep(options.baseDelayMs * 2 ** (attempt - 1));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
throw lastError instanceof Error ? lastError : new Error(String(lastError));
|
|
32
|
+
}
|