@d-zero/release-automation 0.0.0-stage → 0.0.0
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/classify.js +406 -0
- package/dist/cli.js +126 -0
- package/dist/commands/approve-renovate.js +35 -0
- package/dist/commands/context.js +138 -0
- package/dist/commands/generate.js +97 -0
- package/dist/commands/local.js +125 -0
- package/dist/commands/nightly.js +125 -0
- package/dist/commands/publish.js +247 -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 +33 -0
- package/dist/io/git.js +49 -0
- package/dist/io/github.js +87 -0
- package/dist/io/npm.js +25 -0
- package/dist/io/repo.js +163 -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 +81 -5
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const HEADER = /^(?<type>[a-z]+)(?:\((?<scope>[^)]*)\))?(?<bang>!)?: \S/i;
|
|
2
|
+
const BREAKING_FOOTER = /^BREAKING[ -]CHANGE: /m;
|
|
3
|
+
/**
|
|
4
|
+
* Parses a commit message.
|
|
5
|
+
* @param message - The full commit message
|
|
6
|
+
* @returns The type, scope and breaking marker
|
|
7
|
+
* @example
|
|
8
|
+
* parseCommit('feat(core)!: drop Node 20'); // { type: 'feat', scope: 'core', breaking: true }
|
|
9
|
+
*/
|
|
10
|
+
export function parseCommit(message) {
|
|
11
|
+
const [subject = '', ...rest] = message.split('\n');
|
|
12
|
+
const match = HEADER.exec(subject);
|
|
13
|
+
if (!match?.groups) {
|
|
14
|
+
return { type: null, scope: null, breaking: BREAKING_FOOTER.test(rest.join('\n')) };
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
type: match.groups['type']?.toLowerCase() ?? null,
|
|
18
|
+
scope: match.groups['scope'] ?? null,
|
|
19
|
+
breaking: match.groups['bang'] === '!' || BREAKING_FOOTER.test(rest.join('\n')),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** The types whose commits reach users and so count toward a release. */
|
|
23
|
+
export const RELEASE_TYPES = ['feat', 'fix', 'perf', 'revert'];
|
|
24
|
+
/**
|
|
25
|
+
* Tells whether a commit counts toward a release: a type in
|
|
26
|
+
* {@link RELEASE_TYPES}, or a breaking change.
|
|
27
|
+
* @param commit - A parsed commit
|
|
28
|
+
* @returns Whether it counts
|
|
29
|
+
* @example
|
|
30
|
+
* countsForRelease(parseCommit('chore: update lockfile')); // false
|
|
31
|
+
*/
|
|
32
|
+
export function countsForRelease(commit) {
|
|
33
|
+
return (commit.breaking ||
|
|
34
|
+
(commit.type !== null && RELEASE_TYPES.includes(commit.type)));
|
|
35
|
+
}
|
package/dist/dist-tag.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { parseVersion } from './semver.js';
|
|
2
|
+
/**
|
|
3
|
+
* Decides the dist-tag of a release: `alpha`, `beta` and `rc` for those
|
|
4
|
+
* prereleases, `latest` for a stable release of the current line, and
|
|
5
|
+
* `v<major>` for a stable release of a previous line once a later major is
|
|
6
|
+
* stable. `next` is never used.
|
|
7
|
+
* @param input - The release
|
|
8
|
+
* @returns The dist-tag
|
|
9
|
+
* @throws {Error} For a version that is not valid, an unknown prerelease
|
|
10
|
+
* identifier, a stable release on the next-major line, and a release that
|
|
11
|
+
* would move `latest` to an older major
|
|
12
|
+
* @example
|
|
13
|
+
* distTag({ version: '4.0.0-beta.2', role: 'current', independent: false, successorStablePublished: false, latestOnRegistry: '3.5.0' }); // 'beta'
|
|
14
|
+
* distTag({ version: '3.5.1', role: 'previous', independent: false, successorStablePublished: true, latestOnRegistry: '4.2.0' }); // 'v3'
|
|
15
|
+
*/
|
|
16
|
+
export function distTag(input) {
|
|
17
|
+
const version = parseVersion(input.version);
|
|
18
|
+
if (version.pre !== null) {
|
|
19
|
+
return version.pre.id;
|
|
20
|
+
}
|
|
21
|
+
if (input.independent || input.role === 'current') {
|
|
22
|
+
const latest = input.latestOnRegistry === null ? null : parseVersion(input.latestOnRegistry);
|
|
23
|
+
if (!input.independent && latest !== null && latest.major > version.major) {
|
|
24
|
+
throw new Error(`${input.version} would move latest from ${input.latestOnRegistry} to an older major`);
|
|
25
|
+
}
|
|
26
|
+
return 'latest';
|
|
27
|
+
}
|
|
28
|
+
if (input.role === 'next') {
|
|
29
|
+
throw new Error(`${input.version}: the next-major line publishes prereleases only; its stable release is published from main`);
|
|
30
|
+
}
|
|
31
|
+
return input.successorStablePublished ? `v${version.major}` : 'latest';
|
|
32
|
+
}
|
package/dist/guard.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { parseCommit } from './conventional-commit.js';
|
|
2
|
+
import { CURRENT_PUBLISH_BRANCH } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The branches a local commit must not land on: every line branch and
|
|
5
|
+
* `main`. They change only through pull requests (and the publishing App's
|
|
6
|
+
* push of a release back to `dev`, which happens on GitHub, not locally).
|
|
7
|
+
* @param lines - The lines file
|
|
8
|
+
* @returns The guarded branch names
|
|
9
|
+
* @example
|
|
10
|
+
* guardedBranches({ lines: [{ branch: 'dev', role: 'current' }, { branch: 'v3', role: 'previous' }] });
|
|
11
|
+
* // ['dev', 'main', 'v3']
|
|
12
|
+
*/
|
|
13
|
+
export function guardedBranches(lines) {
|
|
14
|
+
return [
|
|
15
|
+
...new Set([
|
|
16
|
+
...lines.lines.map(({ branch }) => branch),
|
|
17
|
+
...(lines.lines.some(({ role }) => role === 'current')
|
|
18
|
+
? [CURRENT_PUBLISH_BRANCH]
|
|
19
|
+
: []),
|
|
20
|
+
]),
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Tells whether a commit message may land on a branch. A breaking change is
|
|
25
|
+
* refused on a previous line, and on the current line once it is stable at
|
|
26
|
+
* major 1 or above, because it would make the Release PR propose the next
|
|
27
|
+
* major. The next-major line and a current line in a prerelease or at
|
|
28
|
+
* major 0 accept it.
|
|
29
|
+
*
|
|
30
|
+
* A local hook cannot know where a topic branch will be merged, so this runs
|
|
31
|
+
* in CI on the commits of a pull request, with its target branch.
|
|
32
|
+
* @param input - The message, the target branch, and the current line's version
|
|
33
|
+
* @param input.message - The commit message
|
|
34
|
+
* @param input.target - The branch the pull request merges into
|
|
35
|
+
* @param input.currentVersion - The version on `dev`; `null` in Lerna independent mode
|
|
36
|
+
* @param input.lines - The lines file
|
|
37
|
+
* @returns `null` when it may land, the reason otherwise
|
|
38
|
+
* @example
|
|
39
|
+
* commitMessageError({ message: 'feat!: x', target: 'dev', currentVersion: '2.1.0', lines }); // the reason
|
|
40
|
+
*/
|
|
41
|
+
export function commitMessageError(input) {
|
|
42
|
+
if (!parseCommit(input.message).breaking) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const target = input.target;
|
|
46
|
+
const line = input.lines.lines.find(({ branch }) => branch === target);
|
|
47
|
+
if (line === undefined || line.role === 'next') {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
if (line.role === 'previous') {
|
|
51
|
+
return `A breaking change cannot land on ${target}: a previous line takes fixes only`;
|
|
52
|
+
}
|
|
53
|
+
const version = input.currentVersion;
|
|
54
|
+
if (version === null || version.includes('-') || version.startsWith('0.')) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
return `A breaking change cannot land on ${target} while it is stable (${version}): start the next major on a v<major> branch`;
|
|
58
|
+
}
|
package/dist/io/exec.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
/**
|
|
3
|
+
* Runs a program and returns its standard output.
|
|
4
|
+
* @param file - The program
|
|
5
|
+
* @param args - Its arguments
|
|
6
|
+
* @param options - Where to run it and extra environment variables
|
|
7
|
+
* @param options.cwd - The working directory
|
|
8
|
+
* @param options.env - Variables added to the environment
|
|
9
|
+
* @returns The standard output, without the trailing newline
|
|
10
|
+
* @throws {Error} When the program exits with a non-zero code
|
|
11
|
+
* @example
|
|
12
|
+
* run('git', ['rev-parse', 'HEAD'], { cwd: process.cwd() });
|
|
13
|
+
*/
|
|
14
|
+
export function run(file, args, options) {
|
|
15
|
+
return execFileSync(file, args, {
|
|
16
|
+
cwd: options.cwd,
|
|
17
|
+
encoding: 'utf8',
|
|
18
|
+
maxBuffer: 256 * 1024 * 1024,
|
|
19
|
+
env: { ...process.env, ...options.env },
|
|
20
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
21
|
+
}).replace(/\n$/, '');
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Runs a program with its output passed through, for long commands such as a publish.
|
|
25
|
+
* @param command - A shell command line
|
|
26
|
+
* @param cwd - The working directory
|
|
27
|
+
* @throws {Error} When the command exits with a non-zero code
|
|
28
|
+
* @example
|
|
29
|
+
* runShell('yarn build', process.cwd());
|
|
30
|
+
*/
|
|
31
|
+
export function runShell(command, cwd) {
|
|
32
|
+
execFileSync('sh', ['-c', command], { cwd, stdio: 'inherit' });
|
|
33
|
+
}
|
package/dist/io/git.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { run } from './exec.js';
|
|
2
|
+
/**
|
|
3
|
+
* @param cwd - The root of the checkout
|
|
4
|
+
* @returns Git access for it
|
|
5
|
+
* @example
|
|
6
|
+
* createGit('/path/to/repo').sha('origin/dev');
|
|
7
|
+
*/
|
|
8
|
+
export function createGit(cwd) {
|
|
9
|
+
const git = (...args) => run('git', args, { cwd });
|
|
10
|
+
const tryGit = (...args) => {
|
|
11
|
+
try {
|
|
12
|
+
return git(...args);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const lines = (text) => text.split('\n').filter((line) => line !== '');
|
|
19
|
+
const commits = (range, extra) => lines(git('log', '--reverse', '--format=%H%x00%ct%x00%s', ...extra, range)).map((line) => {
|
|
20
|
+
const [sha = '', time = '0', subject = ''] = line.split('\0');
|
|
21
|
+
return { sha, time: Number(time), subject };
|
|
22
|
+
});
|
|
23
|
+
return {
|
|
24
|
+
cwd,
|
|
25
|
+
tags: (ref) => lines(git('tag', '--merged', ref)),
|
|
26
|
+
diffNames: (from, to, path) => lines(git('diff', '--name-only', from, to, '--', path)),
|
|
27
|
+
show: (rev, path) => tryGit('show', `${rev}:${path}`),
|
|
28
|
+
listFiles: (rev, path) => lines(git('ls-tree', '-r', '--name-only', rev, '--', path)),
|
|
29
|
+
firstParents: (from, to) => commits(`${from}..${to}`, ['--first-parent']),
|
|
30
|
+
blobId: (rev, path) => tryGit('rev-parse', `${rev}:${path}`),
|
|
31
|
+
remoteBranches: () => lines(git('for-each-ref', '--format=%(refname:lstrip=3)', 'refs/remotes/origin')).filter((name) => name !== 'HEAD'),
|
|
32
|
+
tagsWithDates: () => lines(git('for-each-ref', '--format=%(refname:lstrip=2)%00%(creatordate:iso-strict)', 'refs/tags')).map((line) => {
|
|
33
|
+
const [tag = '', date = ''] = line.split('\0');
|
|
34
|
+
return { tag, date: new Date(date) };
|
|
35
|
+
}),
|
|
36
|
+
sha: (rev) => git('rev-parse', `${rev}^{commit}`),
|
|
37
|
+
isAncestor: (ancestor, descendant) => tryGit('merge-base', '--is-ancestor', ancestor, descendant) !== null,
|
|
38
|
+
currentBranch: () => tryGit('symbolic-ref', '--short', '-q', 'HEAD'),
|
|
39
|
+
messages: (from, to) => git('log', '--reverse', '--format=%H%x00%B%x01', `${from}..${to}`)
|
|
40
|
+
.split('\u0001')
|
|
41
|
+
.map((entry) => entry.replace(/^\n/, ''))
|
|
42
|
+
.filter((entry) => entry !== '')
|
|
43
|
+
.map((entry) => {
|
|
44
|
+
const [sha = '', message = ''] = entry.split('\0');
|
|
45
|
+
return { sha, message: message.trim() };
|
|
46
|
+
}),
|
|
47
|
+
raw: (...args) => git(...args),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error response of the GitHub API.
|
|
3
|
+
* @example
|
|
4
|
+
* if (error instanceof GitHubError && error.status === 404) {}
|
|
5
|
+
*/
|
|
6
|
+
export class GitHubError extends Error {
|
|
7
|
+
status;
|
|
8
|
+
/**
|
|
9
|
+
* @param status - The HTTP status
|
|
10
|
+
* @param message - The response body
|
|
11
|
+
*/
|
|
12
|
+
constructor(status, message) {
|
|
13
|
+
super(`GitHub API ${status}: ${message}`);
|
|
14
|
+
this.status = status;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const API = 'https://api.github.com';
|
|
18
|
+
/**
|
|
19
|
+
* @param options - The token and the repository
|
|
20
|
+
* @param options.token - A token with the permissions the command needs
|
|
21
|
+
* @param options.repository - `owner/name`
|
|
22
|
+
* @returns The client
|
|
23
|
+
* @example
|
|
24
|
+
* createGitHub({ token, repository: process.env.GITHUB_REPOSITORY! });
|
|
25
|
+
*/
|
|
26
|
+
export function createGitHub(options) {
|
|
27
|
+
const headers = {
|
|
28
|
+
authorization: `Bearer ${options.token}`,
|
|
29
|
+
accept: 'application/vnd.github+json',
|
|
30
|
+
'x-github-api-version': '2022-11-28',
|
|
31
|
+
'user-agent': '@d-zero/release-automation',
|
|
32
|
+
};
|
|
33
|
+
const url = (path) => path.startsWith('http')
|
|
34
|
+
? path
|
|
35
|
+
: `${API}${path.replaceAll('{repo}', options.repository)}`;
|
|
36
|
+
/**
|
|
37
|
+
* @param method - The HTTP method
|
|
38
|
+
* @param path - The path or URL
|
|
39
|
+
* @param body - The JSON body
|
|
40
|
+
* @returns The response
|
|
41
|
+
*/
|
|
42
|
+
async function request(method, path, body) {
|
|
43
|
+
const response = await fetch(url(path), {
|
|
44
|
+
method,
|
|
45
|
+
headers: body === undefined ? headers : { ...headers, 'content-type': 'application/json' },
|
|
46
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
47
|
+
});
|
|
48
|
+
if (!response.ok) {
|
|
49
|
+
throw new GitHubError(response.status, await response.text());
|
|
50
|
+
}
|
|
51
|
+
return response;
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
repository: options.repository,
|
|
55
|
+
async rest(method, path, body) {
|
|
56
|
+
const response = await request(method, path, body);
|
|
57
|
+
const text = await response.text();
|
|
58
|
+
return (text === '' ? null : JSON.parse(text));
|
|
59
|
+
},
|
|
60
|
+
async paginate(path) {
|
|
61
|
+
const items = [];
|
|
62
|
+
let next = url(`${path}${path.includes('?') ? '&' : '?'}per_page=100`);
|
|
63
|
+
while (next !== null) {
|
|
64
|
+
const response = await request('GET', next);
|
|
65
|
+
const page = (await response.json());
|
|
66
|
+
if (Array.isArray(page)) {
|
|
67
|
+
items.push(...page);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
const list = Object.values(page).find((value) => Array.isArray(value));
|
|
71
|
+
items.push(...(list ?? []));
|
|
72
|
+
}
|
|
73
|
+
const link = response.headers.get('link') ?? '';
|
|
74
|
+
next = /<([^>]+)>;\s*rel="next"/.exec(link)?.[1] ?? null;
|
|
75
|
+
}
|
|
76
|
+
return items;
|
|
77
|
+
},
|
|
78
|
+
async graphql(query, variables) {
|
|
79
|
+
const response = await request('POST', '/graphql', { query, variables });
|
|
80
|
+
const result = (await response.json());
|
|
81
|
+
if (result.errors && result.errors.length > 0) {
|
|
82
|
+
throw new Error(`GitHub GraphQL: ${JSON.stringify(result.errors)}`);
|
|
83
|
+
}
|
|
84
|
+
return result.data;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
package/dist/io/npm.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads a package from the npm registry, bypassing caches.
|
|
3
|
+
* @param name - The package name
|
|
4
|
+
* @returns Its versions and dist-tags, or `null` when it was never published
|
|
5
|
+
* @throws {Error} For a registry error other than "not found"
|
|
6
|
+
* @example
|
|
7
|
+
* await fetchPackument('@d-zero/roar');
|
|
8
|
+
*/
|
|
9
|
+
export async function fetchPackument(name) {
|
|
10
|
+
const response = await fetch(`https://registry.npmjs.org/${name.replace('/', '%2F')}`, {
|
|
11
|
+
headers: { accept: 'application/json', 'cache-control': 'no-cache' },
|
|
12
|
+
});
|
|
13
|
+
if (response.status === 404) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
if (!response.ok) {
|
|
17
|
+
throw new Error(`npm registry ${response.status} for ${name}`);
|
|
18
|
+
}
|
|
19
|
+
const body = (await response.json());
|
|
20
|
+
return {
|
|
21
|
+
versions: Object.keys(body.versions ?? {}),
|
|
22
|
+
distTags: body['dist-tags'] ?? {},
|
|
23
|
+
times: body.time ?? {},
|
|
24
|
+
};
|
|
25
|
+
}
|
package/dist/io/repo.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { CURRENT_BRANCH } from '../types.js';
|
|
4
|
+
/** Where the lines file lives in a repository. */
|
|
5
|
+
export const LINES_FILE = '.github/release-lines.json';
|
|
6
|
+
/**
|
|
7
|
+
* Parses and validates a lines file.
|
|
8
|
+
* @param text - The file content
|
|
9
|
+
* @returns The lines file
|
|
10
|
+
* @throws {Error} When the content does not have the shape of a lines file
|
|
11
|
+
* @example
|
|
12
|
+
* parseLinesFile('{"lines":[{"branch":"dev","role":"current"}]}');
|
|
13
|
+
*/
|
|
14
|
+
export function parseLinesFile(text) {
|
|
15
|
+
const data = JSON.parse(text);
|
|
16
|
+
if (!Array.isArray(data.lines)) {
|
|
17
|
+
throw new TypeError(`${LINES_FILE}: "lines" must be an array`);
|
|
18
|
+
}
|
|
19
|
+
for (const line of data.lines) {
|
|
20
|
+
if (typeof line.branch !== 'string' ||
|
|
21
|
+
!['current', 'next', 'previous'].includes(line.role)) {
|
|
22
|
+
throw new TypeError(`${LINES_FILE}: each line needs "branch" and a "role" of current, next or previous`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
for (const key of ['supportMonths', 'securityBatchMinutes']) {
|
|
26
|
+
const value = data[key];
|
|
27
|
+
if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {
|
|
28
|
+
throw new TypeError(`${LINES_FILE}: "${key}" must be a positive integer`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (data.publishCommand !== undefined && typeof data.publishCommand !== 'string') {
|
|
32
|
+
throw new TypeError(`${LINES_FILE}: "publishCommand" must be a string`);
|
|
33
|
+
}
|
|
34
|
+
return data;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Reads a file of the checkout at a revision, or from the working tree.
|
|
38
|
+
* @param git - Git access
|
|
39
|
+
* @param file - The path in the repository
|
|
40
|
+
* @param rev - The revision; `null` for the working tree
|
|
41
|
+
* @returns The content, or `null`
|
|
42
|
+
*/
|
|
43
|
+
function read(git, file, rev) {
|
|
44
|
+
if (rev !== null) {
|
|
45
|
+
return git.show(rev, file);
|
|
46
|
+
}
|
|
47
|
+
const full = path.join(git.cwd, file);
|
|
48
|
+
return existsSync(full) ? readFileSync(full, 'utf8') : null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @param git - Git access
|
|
52
|
+
* @param rev - The revision; `null` for the working tree
|
|
53
|
+
* @returns The lines file, or `null` when there is none
|
|
54
|
+
* @example
|
|
55
|
+
* readLinesFile(git, 'origin/dev');
|
|
56
|
+
*/
|
|
57
|
+
export function readLinesFile(git, rev) {
|
|
58
|
+
const text = read(git, LINES_FILE, rev);
|
|
59
|
+
return text === null ? null : parseLinesFile(text);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Lists the owners `.github/CODEOWNERS` names.
|
|
63
|
+
* @param text - The file content
|
|
64
|
+
* @returns The `@user` and `@org/team` owners, without duplicates
|
|
65
|
+
* @example
|
|
66
|
+
* parseCodeowners('* @alice @bob\n'); // ['@alice', '@bob']
|
|
67
|
+
*/
|
|
68
|
+
export function parseCodeowners(text) {
|
|
69
|
+
const owners = text
|
|
70
|
+
.split('\n')
|
|
71
|
+
.map((line) => line.replace(/#.*/, '').trim())
|
|
72
|
+
.filter((line) => line !== '')
|
|
73
|
+
.flatMap((line) => line.split(/\s+/).slice(1))
|
|
74
|
+
.filter((token) => token.startsWith('@'));
|
|
75
|
+
return [...new Set(owners)];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Lists the public packages at a revision: the non-private workspaces, or the
|
|
79
|
+
* root package when it is not private and has no workspaces.
|
|
80
|
+
* @param git - Git access
|
|
81
|
+
* @param rev - The revision
|
|
82
|
+
* @returns The packages
|
|
83
|
+
* @example
|
|
84
|
+
* publicPackages(git, 'origin/main');
|
|
85
|
+
*/
|
|
86
|
+
export function publicPackages(git, rev) {
|
|
87
|
+
const manifests = git
|
|
88
|
+
.listFiles(rev, 'packages')
|
|
89
|
+
.filter((file) => /^packages\/(?:@[^/]+\/)?[^/]+\/package\.json$/.test(file));
|
|
90
|
+
const candidates = manifests.length > 0 ? manifests : ['package.json'];
|
|
91
|
+
return candidates.flatMap((file) => {
|
|
92
|
+
const manifest = JSON.parse(git.show(rev, file) ?? '{}');
|
|
93
|
+
if (manifest.private === true || !manifest.name || !manifest.version) {
|
|
94
|
+
return [];
|
|
95
|
+
}
|
|
96
|
+
return [
|
|
97
|
+
{ name: manifest.name, version: manifest.version, dir: path.posix.dirname(file) },
|
|
98
|
+
];
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The version a line states at a revision: `lerna.json#version`, or the root
|
|
103
|
+
* `package.json#version` without Lerna. `null` in Lerna independent mode.
|
|
104
|
+
* @param git - Git access
|
|
105
|
+
* @param rev - The revision
|
|
106
|
+
* @returns The version
|
|
107
|
+
* @example
|
|
108
|
+
* versionAt(git, 'origin/dev'); // '5.4.2'
|
|
109
|
+
*/
|
|
110
|
+
export function versionAt(git, rev) {
|
|
111
|
+
const lerna = git.show(rev, 'lerna.json');
|
|
112
|
+
if (lerna !== null) {
|
|
113
|
+
const version = JSON.parse(lerna).version ?? null;
|
|
114
|
+
return version === 'independent' ? null : version;
|
|
115
|
+
}
|
|
116
|
+
return (JSON.parse(git.show(rev, 'package.json') ?? '{}').version ??
|
|
117
|
+
null);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @param git - Git access
|
|
121
|
+
* @param rev - The revision
|
|
122
|
+
* @returns The publishing mode at the revision
|
|
123
|
+
* @example
|
|
124
|
+
* publishModeAt(git, 'origin/dev'); // 'lerna-fixed'
|
|
125
|
+
*/
|
|
126
|
+
export function publishModeAt(git, rev) {
|
|
127
|
+
const lerna = git.show(rev, 'lerna.json');
|
|
128
|
+
if (lerna !== null) {
|
|
129
|
+
return JSON.parse(lerna).version === 'independent'
|
|
130
|
+
? 'lerna-independent'
|
|
131
|
+
: 'lerna-fixed';
|
|
132
|
+
}
|
|
133
|
+
const root = JSON.parse(git.show(rev, 'package.json') ?? '{}');
|
|
134
|
+
return root.private === true ? 'custom' : 'npm-single';
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Reads the facts `classify` decides on, from the remote refs and `dev`.
|
|
138
|
+
* The caller fetches the branches and tags first.
|
|
139
|
+
* @param git - Git access
|
|
140
|
+
* @param now - The current time
|
|
141
|
+
* @returns The facts
|
|
142
|
+
* @example
|
|
143
|
+
* classify(readFacts(git, new Date()));
|
|
144
|
+
*/
|
|
145
|
+
export function readFacts(git, now) {
|
|
146
|
+
const rev = `origin/${CURRENT_BRANCH}`;
|
|
147
|
+
const lerna = git.show(rev, 'lerna.json');
|
|
148
|
+
const root = JSON.parse(git.show(rev, 'package.json') ?? '{}');
|
|
149
|
+
const codeowners = git.show(rev, '.github/CODEOWNERS');
|
|
150
|
+
return {
|
|
151
|
+
devVersion: versionAt(git, rev),
|
|
152
|
+
lernaVersion: lerna === null
|
|
153
|
+
? null
|
|
154
|
+
: (JSON.parse(lerna).version ?? null),
|
|
155
|
+
rootPrivate: root.private === true,
|
|
156
|
+
packageManager: root.packageManager ?? null,
|
|
157
|
+
branches: git.remoteBranches(),
|
|
158
|
+
stableTags: git.tagsWithDates().filter(({ tag }) => /^v\d+\.\d+\.\d+$/.test(tag)),
|
|
159
|
+
codeowners: codeowners === null ? null : parseCodeowners(codeowners),
|
|
160
|
+
linesFile: readLinesFile(git, rev),
|
|
161
|
+
now,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
@@ -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
|
+
}
|