Repository navigation
Expand file tree
/
Copy pathcli.mjs
More file actions
360 lines (341 loc) · 15 KB
/
Copy pathcli.mjs
File metadata and controls
360 lines (341 loc) · 15 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Entry point for the dep-changeset workflows. Each subcommand gathers facts from git,
// the installed node_modules or the GitHub API, and hands them to lib.mjs.
//
// node cli.mjs plan which mode a Renovate PR is in, before any install
// node cli.mjs check P8 leak checks on the base and head installs
// node cli.mjs write write .changeset/renovate-<PR>.md
// node cli.mjs release-guard P9 on a Version Packages PR
//
// Inputs come from the environment; see each function. Outputs go to $GITHUB_OUTPUT,
// files in $DEP_CHANGESET_OUT, and the job summary.
import { execFileSync } from 'node:child_process'
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
import {
catalogChanges,
checkEngines,
checkPeers,
diffRuntimeDeps,
diffSurface,
expandGroups,
isBot,
isWorkspaceMember,
LOCK_FILES,
parseCatalogDiff,
parseChangeset,
parseWorkspaceYaml,
plan,
releaseGuard,
renderChangeset,
renderReport,
} from './lib.mjs'
import { snapshot } from './type-surface.mjs'
const env = process.env
const OUT = env.DEP_CHANGESET_OUT ?? join(env.RUNNER_TEMP ?? '/tmp', 'dep-changeset')
function git(args, cwd = '.') {
return execFileSync('git', args, { cwd, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 }).trimEnd()
}
function tryGit(args, cwd = '.') {
try {
return git(args, cwd)
} catch {
return null
}
}
function output(name, value) {
if (env.GITHUB_OUTPUT) appendFileSync(env.GITHUB_OUTPUT, `${name}=${value}\n`)
console.log(`${name}=${value}`)
}
function summary(md) {
if (env.GITHUB_STEP_SUMMARY) appendFileSync(env.GITHUB_STEP_SUMMARY, `${md}\n`)
}
function readJsonAt(rev, path) {
const text = tryGit(['show', `${rev}:${path}`])
if (text === null) return null
try {
return JSON.parse(text)
} catch {
return null
}
}
// Public workspace packages: the root package.json and the workspace members named by
// pnpm-workspace.yaml or the root `workspaces` field, when they have a name and are not
// private. Other package.json files (test fixtures, examples) are not released.
function publicPackages(rev) {
const rootJson = readJsonAt(rev, 'package.json') ?? {}
const ws = rootJson.workspaces
const patterns = [
...parseWorkspaceYaml(tryGit(['show', `${rev}:pnpm-workspace.yaml`]) ?? ''),
...(Array.isArray(ws) ? ws : Array.isArray(ws?.packages) ? ws.packages : []),
]
const files = git(['ls-tree', '-r', '--name-only', rev]).split('\n')
return files
.filter((f) => /(^|\/)package\.json$/.test(f) && !f.includes('node_modules/'))
.map((file) => ({ file, dir: dirname(file), json: readJsonAt(rev, file) }))
.filter((p) => isWorkspaceMember(p.dir, patterns) && p.json?.name && !p.json.private)
}
// Changeset files the PR added or changed that a person committed, and the packages they
// name. Bot commits (renovate[bot], github-actions[bot]) do not count.
function personChangesets(base, head = 'HEAD') {
const files = git(['diff', '--name-only', '--diff-filter=AM', `${base}...${head}`, '--', '.changeset'])
.split('\n')
.filter((f) => /^\.changeset\/[^/]+\.md$/.test(f) && !/readme\.md$/i.test(f))
const covered = new Set()
const byPerson = []
for (const f of files) {
const authors = git(['log', '--format=%an%x09%ae', `${base}..${head}`, '--', f])
.split('\n')
.filter(Boolean)
.map((l) => {
const [name, email] = l.split('\t')
return { name, email }
})
if (!authors.some((a) => !isBot(a))) continue
const text = tryGit(['show', `${head}:${f}`])
if (text === null) continue
byPerson.push(f)
for (const [name, level] of parseChangeset(text)) if (level !== 'none') covered.add(name)
}
return { covered, files: byPerson }
}
function readLabels() {
if (env.LABELS) return JSON.parse(env.LABELS)
if (!env.PR_NUMBER) return []
// Read the labels live rather than from the event payload: Renovate opens the PR and
// labels it in two calls, so the `opened` payload often has none.
const out = execFileSync('gh', ['pr', 'view', env.PR_NUMBER, '--json', 'labels', '--jq', '[.labels[].name]'], { encoding: 'utf8' })
return JSON.parse(out)
}
function cmdPlan() {
const baseRef = env.BASE_REF ?? 'origin/main'
const mergeBase = git(['merge-base', baseRef, 'HEAD'])
const changed = git(['diff', '--name-only', `${mergeBase}...HEAD`]).split('\n').filter(Boolean)
const lockChanged = changed.some((f) => LOCK_FILES.includes(f.split('/').pop()))
const catalogs = changed.includes('pnpm-workspace.yaml')
? parseCatalogDiff(git(['diff', '-U0', `${mergeBase}...HEAD`, '--', 'pnpm-workspace.yaml']))
: new Map()
const packages = publicPackages('HEAD').map((p) => {
const before = changed.includes(p.file) ? (readJsonAt(mergeBase, p.file) ?? {}) : p.json
const changes = [...diffRuntimeDeps(before, p.json), ...catalogChanges(p.json, catalogs)]
return { name: p.json.name, dir: p.dir, changes, engines: p.json.engines?.node }
})
const labels = readLabels()
const bundled = env.BUNDLED === 'true'
const { covered, files } = personChangesets(mergeBase)
const result = plan({ labels, bundled, packages, lockChanged, covered })
const botFile = `.changeset/renovate-${env.PR_NUMBER}.md`
const botChangeset = existsSync(botFile) ? botFile : null
const needsChecks = result.mode === 'auto' && result.uncovered.length > 0
mkdirSync(OUT, { recursive: true })
const data = { ...result, labels, bundled, lockChanged, mergeBase, packages, personChangesets: files, botChangeset }
writeFileSync(join(OUT, 'plan.json'), JSON.stringify(data, null, 2))
console.log(JSON.stringify(data, null, 2))
output('mode', result.mode)
output('merge-base', mergeBase)
output('needs-checks', String(needsChecks))
if (!needsChecks) finish(data, [], [])
}
// Finds an installed dependency the way Node does: the nearest node_modules/<name> from
// the package directory upward, stopping at the checkout root.
function readInstalled(root, pkgDir, name) {
let dir = resolve(root, pkgDir)
const top = resolve(root)
for (;;) {
const f = join(dir, 'node_modules', name, 'package.json')
if (existsSync(f)) return JSON.parse(readFileSync(f, 'utf8'))
if (dir === top) return null
dir = dirname(dir)
}
}
function cmdCheck() {
const p = JSON.parse(readFileSync(join(OUT, 'plan.json'), 'utf8'))
const baseDir = env.BASE_DIR
const headDir = env.HEAD_DIR ?? '.'
const findings = []
const notes = []
if (env.SETUP_FAILED === 'true') {
findings.push({ package: '(all)', check: 'setup', message: 'installing or building the base or head commit failed, so nothing could be verified. See the job log.' })
} else {
for (const name of p.uncovered) {
const pkg = p.packages.find((x) => x.name === name)
checkPackage(pkg, { baseDir, headDir, findings, notes })
}
}
finish(p, findings, notes)
}
function checkPackage(pkg, { baseDir, headDir, findings, notes }) {
if (!existsSync(join(baseDir, pkg.dir, 'package.json'))) {
findings.push({ package: pkg.name, check: 'setup', message: 'the package does not exist on the base commit' })
return
}
for (const c of pkg.changes) {
if (c.field === 'peerDependencies') continue
const depBase = readInstalled(baseDir, pkg.dir, c.name)
const depHead = readInstalled(headDir, pkg.dir, c.name)
if (!depBase || !depHead) {
findings.push({ package: pkg.name, dep: c.name, check: 'setup', message: `not installed on the ${!depBase ? 'base' : 'head'} commit, so its engines and peers cannot be read` })
continue
}
const label = `${depBase.version} → ${depHead.version}`
const eng = checkEngines({ pkgEngines: pkg.engines, depBase, depHead })
if (eng) findings.push({ package: pkg.name, dep: `${c.name} ${label}`, check: 'engines', message: eng })
const peers = checkPeers({ depBase, depHead })
for (const m of peers.findings) findings.push({ package: pkg.name, dep: `${c.name} ${label}`, check: 'peers', message: m })
for (const m of peers.notes) notes.push(`\`${pkg.name}\`, \`${c.name} ${label}\`: ${m}`)
}
let base
let head
try {
base = snapshot(join(baseDir, pkg.dir))
head = snapshot(join(headDir, pkg.dir))
} catch (e) {
findings.push({ package: pkg.name, check: 'types', message: `the type surface could not be read: ${e.message}` })
return
}
for (const [side, s] of [['base', base], ['head', head]]) {
if (s.missing.length) findings.push({ package: pkg.name, check: 'types', message: `declared declaration files are missing on the ${side} build: ${s.missing.map((m) => `\`${m}\``).join(', ')}` })
}
if (base.entries.length === 0 && head.entries.length === 0) {
notes.push(`\`${pkg.name}\` publishes no declaration files, so its type surface was not compared.`)
return
}
const diff = diffSurface(base.surface, head.surface)
if (diff.length) {
const detail = diff
.flatMap((d) => [`# ${d.group}`, ...d.removed.map((l) => `- ${l}`), ...d.added.map((l) => `+ ${l}`)])
.map((l) => (l.length > 300 ? `${l.slice(0, 300)}…` : l))
const shown = detail.length > 80 ? [...detail.slice(0, 80), `… ${detail.length - 80} more lines`] : detail
findings.push({
package: pkg.name,
check: 'types',
message: `the public type surface changed in ${diff.map((d) => `\`${d.group}\``).join(', ')}`,
detail: shown.join('\n'),
})
}
}
// Settles the decision, writes the report and the outputs.
// pass: nothing to do, or every target is covered.
// write: P8 is clean; write a patch changeset for the targets.
// fail: a person must write the changeset.
function finish(p, findings, notes) {
let decision
const reasons = [...p.reasons]
if (p.mode === 'none' || p.uncovered.length === 0) decision = 'pass'
else if (p.mode === 'human') decision = 'fail'
else if (findings.length) {
decision = 'fail'
reasons.push('A leak check tripped or could not run, so the workflow fails closed.')
} else {
const existing = p.botChangeset ? parseChangeset(readFileSync(p.botChangeset, 'utf8')) : new Map()
decision = p.uncovered.every((t) => existing.has(t)) ? 'pass' : 'write'
reasons.push('Every leak check passed on the base and head commits.')
}
const report = renderReport({ decision, plan: { ...p, reasons }, findings, notes, botChangeset: decision === 'fail' ? p.botChangeset : null })
mkdirSync(OUT, { recursive: true })
writeFileSync(join(OUT, 'report.md'), report)
writeFileSync(join(OUT, 'decision.json'), JSON.stringify({ decision, targets: p.uncovered, findings, notes }, null, 2))
summary(report)
console.log(report)
output('decision', decision)
output('targets', JSON.stringify(decision === 'write' ? p.uncovered : []))
}
function cmdWrite() {
const targets = JSON.parse(env.TARGETS ?? '[]')
if (targets.length === 0) return
const file = `.changeset/renovate-${env.PR_NUMBER}.md`
const existing = existsSync(file) ? parseChangeset(readFileSync(file, 'utf8')) : new Map()
const names = new Set([...existing.keys(), ...targets])
const text = renderChangeset(names)
mkdirSync('.changeset', { recursive: true })
writeFileSync(file, text)
console.log(text)
}
// P9. Fails when the Version Packages PR raises a package's major version and no
// person-written changeset among those the PR consumes asked for it.
function cmdReleaseGuard() {
const baseRef = env.BASE_REF ?? 'origin/main'
const mergeBase = git(['merge-base', baseRef, 'HEAD'])
const bumps = []
for (const f of git(['diff', '--name-only', `${mergeBase}...HEAD`]).split('\n')) {
if (!/(^|\/)package\.json$/.test(f) || f.includes('node_modules/')) continue
const a = readJsonAt(mergeBase, f)
const b = readJsonAt('HEAD', f)
if (a?.version && b?.version && a.version !== b.version) bumps.push({ name: b.name, from: a.version, to: b.version })
}
const consumed = git(['diff', '--name-only', '--diff-filter=D', `${mergeBase}...HEAD`, '--', '.changeset'])
.split('\n')
.filter((f) => /^\.changeset\/[^/]+\.md$/.test(f) && !/readme\.md$/i.test(f))
const changesets = consumed.map((file) => ({
file,
releases: parseChangeset(git(['show', `${mergeBase}:${file}`])),
byPerson: writtenByPerson(file, mergeBase),
}))
const config = readJsonAt(mergeBase, '.changeset/config.json') ?? {}
const groups = expandGroups(config, publicPackages('HEAD').map((p) => p.json.name))
const bad = releaseGuard({ bumps, changesets, groups })
const lines = ['## dep-changeset release guard', '']
if (bad.length === 0) {
lines.push(
bumps.length ? 'Every major bump in this release was asked for by a person-written changeset.' : 'This release has no version bumps.',
)
} else {
lines.push('These packages get a major bump that no person-written changeset asked for:', '')
for (const b of bad) {
lines.push(`- \`${b.name}\` ${b.from} → ${b.to}${b.botMajor.length ? ` (a bot wrote \`major\` in ${b.botMajor.map((f) => `\`${f}\``).join(', ')})` : ' (changesets raised it, for example because a peer dependency range moved)'}`)
}
lines.push(
'',
'A major release has to be a person\'s decision. Either add a changeset on the default branch that names the package with `major`, written by a person, or lower the level of the changeset that caused it.',
)
}
lines.push('', '| Changeset | Written by a person | Releases |', '|---|---|---|')
for (const c of changesets) lines.push(`| \`${c.file}\` | ${c.byPerson ? 'yes' : 'no'} | ${[...c.releases].map(([n, l]) => `\`${n}\`: ${l}`).join(', ')} |`)
const md = lines.join('\n')
summary(md)
console.log(md)
if (bad.length) {
console.log(`::error::${bad.length} major bump(s) not requested by a person-written changeset: ${bad.map((b) => b.name).join(', ')}`)
process.exitCode = 1
}
}
// A changeset counts as person-written when any commit that added or edited it on the
// default branch came from a person. Squash merges make the merged commit's author the
// PR author, so a person's commit inside a Renovate PR is only visible through the PR:
// look the PR up, then its commits.
function writtenByPerson(file, rev) {
const commits = git(['log', '--format=%H%x09%an%x09%ae', rev, '--', file]).split('\n').filter(Boolean)
for (const line of commits) {
const [sha, name, email] = line.split('\t')
const prs = ghJson(`repos/${env.GITHUB_REPOSITORY}/commits/${sha}/pulls`)
if (!prs || prs.length === 0) {
if (!isBot({ name, email })) return true
continue
}
for (const pr of prs) {
if (pr.user?.type !== 'Bot' && !isBot({ name: pr.user?.login })) return true
const prCommits = ghJson(`repos/${env.GITHUB_REPOSITORY}/pulls/${pr.number}/commits?per_page=100`) ?? []
for (const c of prCommits) {
const person = c.author ? c.author.type !== 'Bot' && !isBot({ name: c.author.login }) : !isBot({ name: c.commit.author.name, email: c.commit.author.email })
if (!person) continue
const detail = ghJson(`repos/${env.GITHUB_REPOSITORY}/commits/${c.sha}`)
if (detail?.files?.some((f) => f.filename === file)) return true
}
}
}
return false
}
function ghJson(path) {
try {
return JSON.parse(execFileSync('gh', ['api', path], { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 }))
} catch (e) {
console.log(`::warning::gh api ${path} failed: ${e.message.split('\n')[0]}`)
return null
}
}
const commands = { plan: cmdPlan, check: cmdCheck, write: cmdWrite, 'release-guard': cmdReleaseGuard }
const cmd = commands[process.argv[2]]
if (!cmd) {
console.error(`usage: cli.mjs ${Object.keys(commands).join('|')}`)
process.exit(2)
}
cmd()