-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcheck-release-notes.mjs
More file actions
246 lines (233 loc) · 7.85 KB
/
Copy pathcheck-release-notes.mjs
File metadata and controls
246 lines (233 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env node
// Pre-publish + PR-CI presence gate for per-release notes files.
//
// Every stable (`latest`) release publishes a committed, hand-curated
// `docs/releases/v<version>.md` as the GitHub Release body — there is
// no auto-generated fallback. This gate asserts that file exists so a
// release can never ship with missing or flat notes.
//
// `package.json.version` on a given ref is the *currently published*
// version on that ref. A change to that value across a branch is a
// release bump; that is the signal both modes key off.
//
// - Publish mode. The version being published is read from
// `--version <v>` if given, else from root `package.json` on the
// `--head` ref. The gate asserts `docs/releases/v<version>.md`
// exists in the working tree (the release checkout). Missing →
// non-zero exit naming the expected path; present → exit 0.
//
// - PR mode. The root `package.json` `version` is read on `--prev`
// (default `origin/main`, falling back to local `main`) and on
// `--head` (default HEAD). If the version changed on the branch
// (a release bump), the gate asserts the matching
// `docs/releases/v<head-version>.md` is present — so a release PR
// fails early rather than only post-merge. If the version is
// unchanged (no bump), the gate is a no-op and exits 0.
//
// Usage:
// node scripts/check-release-notes.mjs [--mode pr|publish]
// [--head <ref>] [--prev <ref>]
// [--version <v>] [--json]
//
// Wired into root `package.json` as `pnpm check:release-notes`.
// Invoked from `.github/workflows/ci.yml` (mode pr) and
// `.github/workflows/publish.yml` (mode publish, gated on tag == 'latest').
import { execFileSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import { argv, cwd, exit, stderr, stdout } from 'node:process';
import { fileURLToPath } from 'node:url';
const RELEASES_DIR = 'docs/releases';
/**
* The committed notes file whose contents become the GitHub Release
* body for a given version. Exported for unit tests.
*/
export function notesFileForVersion(version) {
return `${RELEASES_DIR}/v${version}.md`;
}
function git(repoRoot, ...args) {
return execFileSync('git', args, {
cwd: repoRoot,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
});
}
function tryGit(repoRoot, ...args) {
try {
return git(repoRoot, ...args).trim();
} catch {
return null;
}
}
/**
* Read the root `package.json` `version` at a git ref. Throws if the
* ref can't be read or carries no usable `version`.
*/
export function readVersionAtRef(repoRoot, ref) {
const parsed = JSON.parse(git(repoRoot, 'show', `${ref}:package.json`));
if (typeof parsed.version !== 'string' || parsed.version.length === 0) {
throw new Error(`root package.json at "${ref}" has no usable \`version\` field`);
}
return parsed.version;
}
function resolveDefaultPrev(repoRoot) {
// PR mode only. Prefer `origin/main`; fall back to local `main` (some
// CI checkouts don't preserve the `origin` remote name).
const refs = ['origin/main', 'main'];
for (const ref of refs) {
if (tryGit(repoRoot, 'rev-parse', '--verify', `${ref}^{commit}`)) {
return ref;
}
}
throw new Error(
'check-release-notes: --mode pr default --prev requires either `origin/main` or `main` to exist; pass --prev <ref> explicitly',
);
}
/**
* Parse the supported CLI arguments. Exported for unit tests.
*/
export function parseArgs(args) {
const out = { mode: 'pr', head: 'HEAD', prev: null, version: null, json: false };
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '--mode') {
out.mode = args[++i];
} else if (arg === '--head') {
out.head = args[++i];
} else if (arg === '--prev') {
out.prev = args[++i];
} else if (arg === '--version') {
out.version = args[++i];
} else if (arg === '--json') {
out.json = true;
} else if (arg === '--help' || arg === '-h') {
out.help = true;
} else {
throw new Error(`check-release-notes: unknown argument "${arg}"`);
}
}
if (out.mode !== 'pr' && out.mode !== 'publish') {
throw new Error(`check-release-notes: --mode must be "pr" or "publish" (got "${out.mode}")`);
}
return out;
}
/**
* Run the check. Returns a result envelope; the caller decides how to
* render it (text vs JSON) and owns `process.exit`.
*
* - publish: assert the notes file for the target version exists.
* - pr: a no-op unless the version changed between prev and head; when
* it did, assert the notes file for the head version exists.
*/
export function runCheck({ repoRoot, mode, head, prev, version }) {
if (mode === 'publish') {
const targetVersion = version ?? readVersionAtRef(repoRoot, head);
const notesFile = notesFileForVersion(targetVersion);
const present = existsSync(`${repoRoot}/${notesFile}`);
return {
ok: present,
mode,
version: targetVersion,
bumped: null,
notesFile,
present,
violations: present ? [] : [{ rule: 'missing-notes', version: targetVersion, notesFile }],
};
}
const headVersion = version ?? readVersionAtRef(repoRoot, head);
const prevVersion = readVersionAtRef(repoRoot, prev);
const bumped = headVersion !== prevVersion;
if (!bumped) {
return {
ok: true,
mode,
version: headVersion,
prevVersion,
bumped: false,
notesFile: null,
present: null,
violations: [],
};
}
const notesFile = notesFileForVersion(headVersion);
const present = existsSync(`${repoRoot}/${notesFile}`);
return {
ok: present,
mode,
version: headVersion,
prevVersion,
bumped: true,
notesFile,
present,
violations: present ? [] : [{ rule: 'missing-notes', version: headVersion, notesFile }],
};
}
function renderViolations(result, write) {
for (const v of result.violations) {
write(
`check-release-notes: missing release notes for v${v.version}\n` +
' expected a committed notes file at:\n' +
` ${v.notesFile}\n` +
' A stable release requires a hand-authored notes file — there is no\n' +
' auto-generated fallback. Author one (see docs/releases/README.md for\n' +
' the template and section order) and commit it before publishing.\n',
);
}
}
export function main(args = argv.slice(2), repoRoot = cwd()) {
let parsed;
try {
parsed = parseArgs(args);
} catch (err) {
stderr.write(`${err.message}\n`);
return 2;
}
if (parsed.help) {
stdout.write(
[
'Usage: node scripts/check-release-notes.mjs [--mode pr|publish] [--head <ref>] [--prev <ref>] [--version <v>] [--json]',
'',
' --mode pr (default) or publish',
' --head git ref to read the version from (default: HEAD)',
' --prev pr mode: git ref to compare against (default: origin/main, then main)',
' --version override the detected version (skips reading package.json)',
' --json emit a JSON result envelope on stdout instead of text on stderr',
'',
].join('\n'),
);
return 0;
}
let prev = parsed.prev;
try {
if (parsed.mode === 'pr' && prev === null) {
prev = resolveDefaultPrev(repoRoot);
}
} catch (err) {
stderr.write(`${err.message}\n`);
return 2;
}
let result;
try {
result = runCheck({
repoRoot,
mode: parsed.mode,
head: parsed.head,
prev,
version: parsed.version,
});
} catch (err) {
stderr.write(`check-release-notes: ${err.message}\n`);
return 2;
}
if (parsed.json) {
stdout.write(`${JSON.stringify(result, null, 2)}\n`);
return result.ok ? 0 : 1;
}
if (result.ok) {
return 0;
}
renderViolations(result, (s) => stderr.write(s));
return 1;
}
if (argv[1] && fileURLToPath(import.meta.url) === argv[1]) {
exit(main());
}