@zairakai/js-dev-tools 3.21.0 → 4.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/.editorconfig +6 -1
- package/README.md +49 -13
- package/config/.markdownlintignore +3 -4
- package/config/.prettierignore +11 -6
- package/config/.stylelintignore +13 -10
- package/config/knip.config.js +6 -2
- package/config/stylelint.config.js +1 -0
- package/package.json +19 -5
- package/scripts/update-dependencies.mjs +504 -133
- package/stubs/githooks/commit-msg +7 -4
- package/stubs/style/stylelint.config.js.stub +7 -0
- package/tools/make/bats.mk +3 -3
- package/tools/make/core.mk +3 -3
- package/tools/make/variables.mk +1 -1
|
@@ -58,13 +58,26 @@ const DEFAULTS = {
|
|
|
58
58
|
cascade: null,
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
const RUNTIME_SECTIONS = [
|
|
62
|
-
|
|
61
|
+
const RUNTIME_SECTIONS = [
|
|
62
|
+
'dependencies',
|
|
63
|
+
'peerDependencies',
|
|
64
|
+
'optionalDependencies',
|
|
65
|
+
]
|
|
66
|
+
const ALL_SECTIONS = [
|
|
67
|
+
'dependencies',
|
|
68
|
+
'devDependencies',
|
|
69
|
+
'peerDependencies',
|
|
70
|
+
'optionalDependencies',
|
|
71
|
+
]
|
|
63
72
|
|
|
64
73
|
/** The manifest of each ecosystem, with the sections that are published and all of them. */
|
|
65
74
|
const ECOSYSTEMS = {
|
|
66
75
|
npm: { file: 'package.json', runtime: RUNTIME_SECTIONS, all: ALL_SECTIONS },
|
|
67
|
-
composer: {
|
|
76
|
+
composer: {
|
|
77
|
+
file: 'composer.json',
|
|
78
|
+
runtime: ['require'],
|
|
79
|
+
all: ['require', 'require-dev'],
|
|
80
|
+
},
|
|
68
81
|
}
|
|
69
82
|
|
|
70
83
|
/** The ecosystem of the project: a package.json at its root, otherwise a composer.json. */
|
|
@@ -107,12 +120,16 @@ export function nextVersion(last, level) {
|
|
|
107
120
|
}
|
|
108
121
|
|
|
109
122
|
function sorted(section) {
|
|
110
|
-
return JSON.stringify(
|
|
123
|
+
return JSON.stringify(
|
|
124
|
+
Object.entries(section ?? {}).sort(([a], [b]) => a.localeCompare(b)),
|
|
125
|
+
)
|
|
111
126
|
}
|
|
112
127
|
|
|
113
128
|
/** True when a range that is published (dependencies, peerDependencies, optionalDependencies) is not the same. */
|
|
114
129
|
export function runtimeChanged(before, after, sections = RUNTIME_SECTIONS) {
|
|
115
|
-
return sections.some(
|
|
130
|
+
return sections.some(
|
|
131
|
+
(section) => sorted(before?.[section]) !== sorted(after?.[section]),
|
|
132
|
+
)
|
|
116
133
|
}
|
|
117
134
|
|
|
118
135
|
/** The dependencies whose range changed, with the section, the old range and the new one. */
|
|
@@ -123,22 +140,38 @@ export function diffDependencies(before, after, sections = ALL_SECTIONS) {
|
|
|
123
140
|
const old = before?.[section] ?? {}
|
|
124
141
|
const current = after?.[section] ?? {}
|
|
125
142
|
|
|
126
|
-
for (const name of new Set([
|
|
143
|
+
for (const name of new Set([
|
|
144
|
+
...Object.keys(old),
|
|
145
|
+
...Object.keys(current),
|
|
146
|
+
])) {
|
|
127
147
|
if (old[name] !== current[name]) {
|
|
128
|
-
rows.push({
|
|
148
|
+
rows.push({
|
|
149
|
+
section,
|
|
150
|
+
name,
|
|
151
|
+
from: old[name] ?? null,
|
|
152
|
+
to: current[name] ?? null,
|
|
153
|
+
})
|
|
129
154
|
}
|
|
130
155
|
}
|
|
131
156
|
}
|
|
132
157
|
|
|
133
|
-
return rows.sort(
|
|
158
|
+
return rows.sort(
|
|
159
|
+
(a, b) =>
|
|
160
|
+
a.section.localeCompare(b.section) || a.name.localeCompare(b.name),
|
|
161
|
+
)
|
|
134
162
|
}
|
|
135
163
|
|
|
136
164
|
/** The changes as a Markdown table, for the merge request and the commit. */
|
|
137
165
|
export function formatChanges(rows) {
|
|
138
|
-
const lines = [
|
|
166
|
+
const lines = [
|
|
167
|
+
'| Section | Package | From | To |',
|
|
168
|
+
'| --- | --- | --- | --- |',
|
|
169
|
+
]
|
|
139
170
|
|
|
140
171
|
for (const row of rows) {
|
|
141
|
-
lines.push(
|
|
172
|
+
lines.push(
|
|
173
|
+
`| ${row.section} | ${row.name} | ${row.from ?? '-'} | ${row.to ?? '-'} |`,
|
|
174
|
+
)
|
|
142
175
|
}
|
|
143
176
|
|
|
144
177
|
return lines.join('\n')
|
|
@@ -146,7 +179,11 @@ export function formatChanges(rows) {
|
|
|
146
179
|
|
|
147
180
|
/** The identity and the key used to sign, from the group variables. It fails when one is missing: no unsigned tag. */
|
|
148
181
|
export function signerIdentity(env) {
|
|
149
|
-
const missing = [
|
|
182
|
+
const missing = [
|
|
183
|
+
'GPG_PRIVATE_KEY_B64',
|
|
184
|
+
'RELEASE_SIGNER_NAME',
|
|
185
|
+
'RELEASE_SIGNER_EMAIL',
|
|
186
|
+
].filter((name) => !env[name])
|
|
150
187
|
|
|
151
188
|
if (missing.length > 0) {
|
|
152
189
|
throw new Error(
|
|
@@ -162,16 +199,38 @@ export function signerIdentity(env) {
|
|
|
162
199
|
}
|
|
163
200
|
|
|
164
201
|
/** What `tag` has to do: the version to create, or null (and why). */
|
|
165
|
-
export function planTag({
|
|
166
|
-
|
|
202
|
+
export function planTag({
|
|
203
|
+
config,
|
|
204
|
+
last,
|
|
205
|
+
commits,
|
|
206
|
+
before,
|
|
207
|
+
after,
|
|
208
|
+
sections = RUNTIME_SECTIONS,
|
|
209
|
+
}) {
|
|
210
|
+
if (config.tag.when === 'never')
|
|
211
|
+
return {
|
|
212
|
+
version: null,
|
|
213
|
+
reason: 'the project is never tagged by the update',
|
|
214
|
+
}
|
|
167
215
|
if (!last) return { version: null, reason: 'there is no tag yet' }
|
|
168
|
-
if (commits.length === 0)
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
216
|
+
if (commits.length === 0)
|
|
217
|
+
return { version: null, reason: 'no update since the last tag' }
|
|
218
|
+
|
|
219
|
+
if (
|
|
220
|
+
config.tag.when === 'runtime' &&
|
|
221
|
+
!runtimeChanged(before, after, sections)
|
|
222
|
+
) {
|
|
223
|
+
return {
|
|
224
|
+
version: null,
|
|
225
|
+
reason:
|
|
226
|
+
'only the development dependencies changed: the published archive is the same',
|
|
227
|
+
}
|
|
172
228
|
}
|
|
173
229
|
|
|
174
|
-
return {
|
|
230
|
+
return {
|
|
231
|
+
version: nextVersion(last, config.tag.level),
|
|
232
|
+
reason: 'a published range changed',
|
|
233
|
+
}
|
|
175
234
|
}
|
|
176
235
|
|
|
177
236
|
/**
|
|
@@ -179,9 +238,14 @@ export function planTag({ config, last, commits, before, after, sections = RUNTI
|
|
|
179
238
|
* pipeline to the installed version: that is the only change of the pipeline file an update may commit.
|
|
180
239
|
*/
|
|
181
240
|
export function isRefOnlyChange(diff) {
|
|
182
|
-
const changed = diff
|
|
241
|
+
const changed = diff
|
|
242
|
+
.split('\n')
|
|
243
|
+
.filter((line) => /^[+-]/.test(line) && !/^(\+\+\+|---)/.test(line))
|
|
183
244
|
|
|
184
|
-
return
|
|
245
|
+
return (
|
|
246
|
+
changed.length === 2 &&
|
|
247
|
+
changed.every((line) => /^[+-]\s*ref:\s*\S+\s*$/.test(line))
|
|
248
|
+
)
|
|
185
249
|
}
|
|
186
250
|
|
|
187
251
|
/** The problems of a policy document, as a list of sentences (empty when it is valid). */
|
|
@@ -199,14 +263,21 @@ export function validatePolicy(doc) {
|
|
|
199
263
|
else if (seen.has(name)) errors.push(`${where}: "${name}" is listed twice.`)
|
|
200
264
|
seen.add(name)
|
|
201
265
|
|
|
202
|
-
if (typeof entry?.reason !== 'string' || entry.reason.trim() === '')
|
|
203
|
-
|
|
266
|
+
if (typeof entry?.reason !== 'string' || entry.reason.trim() === '')
|
|
267
|
+
errors.push(`${where}: "reason" is missing.`)
|
|
268
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(entry?.since ?? ''))
|
|
269
|
+
errors.push(`${where}: "since" must be a date (YYYY-MM-DD).`)
|
|
204
270
|
|
|
205
271
|
if (undefined !== entry?.unblock) {
|
|
206
272
|
const { version, peers } = entry.unblock ?? {}
|
|
207
273
|
|
|
208
|
-
if (semver.valid(version) === null)
|
|
209
|
-
|
|
274
|
+
if (semver.valid(version) === null)
|
|
275
|
+
errors.push(`${where}: "unblock.version" must be a version (X.Y.Z).`)
|
|
276
|
+
if (
|
|
277
|
+
!Array.isArray(peers) ||
|
|
278
|
+
peers.length === 0 ||
|
|
279
|
+
!peers.every((peer) => typeof peer === 'string' && peer)
|
|
280
|
+
) {
|
|
210
281
|
errors.push(`${where}: "unblock.peers" must be a list of packages.`)
|
|
211
282
|
}
|
|
212
283
|
}
|
|
@@ -225,22 +296,35 @@ export async function fetchPolicy(
|
|
|
225
296
|
let doc
|
|
226
297
|
|
|
227
298
|
try {
|
|
228
|
-
const response = await fetchImpl(url, {
|
|
299
|
+
const response = await fetchImpl(url, {
|
|
300
|
+
signal: AbortSignal.timeout(20000),
|
|
301
|
+
})
|
|
229
302
|
if (!response.ok) throw new Error(`HTTP ${response.status}`)
|
|
230
303
|
doc = await response.json()
|
|
231
304
|
} catch (error) {
|
|
232
|
-
throw new Error(
|
|
305
|
+
throw new Error(
|
|
306
|
+
`The dependency policy could not be read (${url}): ${error.message}`,
|
|
307
|
+
{ cause: error },
|
|
308
|
+
)
|
|
233
309
|
}
|
|
234
310
|
|
|
235
311
|
const errors = validatePolicy(doc)
|
|
236
|
-
if (errors.length > 0)
|
|
312
|
+
if (errors.length > 0)
|
|
313
|
+
throw new Error(
|
|
314
|
+
`The dependency policy is not valid (${url}): ${errors.join(' ')}`,
|
|
315
|
+
)
|
|
237
316
|
|
|
238
317
|
return doc
|
|
239
318
|
}
|
|
240
319
|
|
|
241
320
|
/** The packages that the project never updates: its own `reject` and the packages of the policy. */
|
|
242
321
|
export function rejectedPackages(config, policy) {
|
|
243
|
-
return [
|
|
322
|
+
return [
|
|
323
|
+
...new Set([
|
|
324
|
+
...config.reject,
|
|
325
|
+
...policy.entries.map((entry) => entry.package),
|
|
326
|
+
]),
|
|
327
|
+
]
|
|
244
328
|
}
|
|
245
329
|
|
|
246
330
|
/** Whether the condition of an entry is met: every package of `peers` accepts the version in its peer range. */
|
|
@@ -251,7 +335,9 @@ export function isLifted(entry, ranges) {
|
|
|
251
335
|
const range = ranges[peer]
|
|
252
336
|
|
|
253
337
|
return (
|
|
254
|
-
typeof range === 'string' &&
|
|
338
|
+
typeof range === 'string' &&
|
|
339
|
+
semver.validRange(range) !== null &&
|
|
340
|
+
semver.satisfies(entry.unblock.version, range)
|
|
255
341
|
)
|
|
256
342
|
})
|
|
257
343
|
}
|
|
@@ -263,8 +349,12 @@ export function lockedVersions(lock) {
|
|
|
263
349
|
for (const [path, info] of Object.entries(lock?.packages ?? {})) {
|
|
264
350
|
if (path === '' || !info?.version) continue
|
|
265
351
|
|
|
266
|
-
const name = path.slice(
|
|
267
|
-
|
|
352
|
+
const name = path.slice(
|
|
353
|
+
path.lastIndexOf('node_modules/') + 'node_modules/'.length,
|
|
354
|
+
)
|
|
355
|
+
versions[name] = [
|
|
356
|
+
...new Set([...(versions[name] ?? []), info.version]),
|
|
357
|
+
].sort()
|
|
268
358
|
}
|
|
269
359
|
|
|
270
360
|
return versions
|
|
@@ -274,12 +364,20 @@ export function lockedVersions(lock) {
|
|
|
274
364
|
* The packages of the policy whose version changes: a range of package.json or a locked version. A package that is
|
|
275
365
|
* added or removed is not an update.
|
|
276
366
|
*/
|
|
277
|
-
export function policyViolations({
|
|
367
|
+
export function policyViolations({
|
|
368
|
+
blocked,
|
|
369
|
+
beforeManifest,
|
|
370
|
+
afterManifest,
|
|
371
|
+
beforeLock,
|
|
372
|
+
afterLock,
|
|
373
|
+
}) {
|
|
278
374
|
const problems = []
|
|
279
375
|
|
|
280
376
|
for (const row of diffDependencies(beforeManifest, afterManifest)) {
|
|
281
377
|
if (blocked.includes(row.name) && row.from !== null && row.to !== null) {
|
|
282
|
-
problems.push(
|
|
378
|
+
problems.push(
|
|
379
|
+
`package.json: ${row.name} moves from ${row.from} to ${row.to}.`,
|
|
380
|
+
)
|
|
283
381
|
}
|
|
284
382
|
}
|
|
285
383
|
|
|
@@ -287,8 +385,14 @@ export function policyViolations({ blocked, beforeManifest, afterManifest, befor
|
|
|
287
385
|
const after = lockedVersions(afterLock)
|
|
288
386
|
|
|
289
387
|
for (const name of blocked) {
|
|
290
|
-
if (
|
|
291
|
-
|
|
388
|
+
if (
|
|
389
|
+
before[name] &&
|
|
390
|
+
after[name] &&
|
|
391
|
+
before[name].join(', ') !== after[name].join(', ')
|
|
392
|
+
) {
|
|
393
|
+
problems.push(
|
|
394
|
+
`package-lock.json: ${name} moves from ${before[name].join(', ')} to ${after[name].join(', ')}.`,
|
|
395
|
+
)
|
|
292
396
|
}
|
|
293
397
|
}
|
|
294
398
|
|
|
@@ -312,9 +416,16 @@ export function syncDependabot(text, names, ecosystem) {
|
|
|
312
416
|
|
|
313
417
|
const indent = match[1]
|
|
314
418
|
const rules =
|
|
315
|
-
names.length === 0
|
|
316
|
-
|
|
317
|
-
|
|
419
|
+
names.length === 0
|
|
420
|
+
? []
|
|
421
|
+
: [
|
|
422
|
+
`${indent}ignore:`,
|
|
423
|
+
...names.map((name) => `${indent} - dependency-name: '${name}'`),
|
|
424
|
+
]
|
|
425
|
+
|
|
426
|
+
return text.replace(pattern, () =>
|
|
427
|
+
[`${indent}${begin}`, ...rules, `${indent}${end}`].join('\n'),
|
|
428
|
+
)
|
|
318
429
|
}
|
|
319
430
|
|
|
320
431
|
/**
|
|
@@ -328,7 +439,12 @@ export function fixedOverrides(overrides, vulnerabilities, versionsOf) {
|
|
|
328
439
|
const pinned = typeof pin === 'string' ? pin : pin?.['.']
|
|
329
440
|
const range = vulnerabilities?.[name]?.range
|
|
330
441
|
|
|
331
|
-
if (
|
|
442
|
+
if (
|
|
443
|
+
semver.valid(pinned) === null ||
|
|
444
|
+
typeof range !== 'string' ||
|
|
445
|
+
semver.validRange(range) === null
|
|
446
|
+
)
|
|
447
|
+
continue
|
|
332
448
|
if (!semver.satisfies(pinned, range)) continue
|
|
333
449
|
|
|
334
450
|
const candidates = (versionsOf(name) ?? []).filter(
|
|
@@ -353,7 +469,9 @@ export function fixedOverrides(overrides, vulnerabilities, versionsOf) {
|
|
|
353
469
|
export function refreshableNames(lock, scopes = TRUSTED_SCOPES) {
|
|
354
470
|
const names = Object.keys(lock?.packages ?? {})
|
|
355
471
|
.filter((path) => path !== '')
|
|
356
|
-
.map((path) =>
|
|
472
|
+
.map((path) =>
|
|
473
|
+
path.slice(path.lastIndexOf('node_modules/') + 'node_modules/'.length),
|
|
474
|
+
)
|
|
357
475
|
.filter((name) => !scopes.some((scope) => name.startsWith(scope)))
|
|
358
476
|
|
|
359
477
|
return [...new Set(names)].sort()
|
|
@@ -381,19 +499,30 @@ export function isInherited(lock, name, scope = GROUP_SCOPE) {
|
|
|
381
499
|
for (const [path, node] of Object.entries(packages)) {
|
|
382
500
|
if (path === '') continue
|
|
383
501
|
|
|
384
|
-
const key = path.slice(
|
|
502
|
+
const key = path.slice(
|
|
503
|
+
path.lastIndexOf('node_modules/') + 'node_modules/'.length,
|
|
504
|
+
)
|
|
385
505
|
const next = edges.get(key) ?? new Set()
|
|
386
506
|
|
|
387
|
-
for (const section of [
|
|
388
|
-
|
|
507
|
+
for (const section of [
|
|
508
|
+
'dependencies',
|
|
509
|
+
'optionalDependencies',
|
|
510
|
+
'peerDependencies',
|
|
511
|
+
]) {
|
|
512
|
+
Object.keys(node[section] ?? {}).forEach((dependency) =>
|
|
513
|
+
next.add(dependency),
|
|
514
|
+
)
|
|
389
515
|
}
|
|
390
516
|
edges.set(key, next)
|
|
391
517
|
}
|
|
392
518
|
|
|
393
519
|
const root = packages['']
|
|
394
|
-
const direct = [
|
|
395
|
-
|
|
396
|
-
|
|
520
|
+
const direct = [
|
|
521
|
+
'dependencies',
|
|
522
|
+
'devDependencies',
|
|
523
|
+
'optionalDependencies',
|
|
524
|
+
'peerDependencies',
|
|
525
|
+
].flatMap((section) => Object.keys(root?.[section] ?? {}))
|
|
397
526
|
const reaches = (starts) => {
|
|
398
527
|
const seen = new Set(starts)
|
|
399
528
|
const queue = [...starts]
|
|
@@ -410,15 +539,20 @@ export function isInherited(lock, name, scope = GROUP_SCOPE) {
|
|
|
410
539
|
return seen
|
|
411
540
|
}
|
|
412
541
|
|
|
413
|
-
const ours = reaches(
|
|
414
|
-
|
|
542
|
+
const ours = reaches(
|
|
543
|
+
direct.filter((dependency) => !dependency.startsWith(scope)),
|
|
544
|
+
)
|
|
545
|
+
const theirs = reaches(
|
|
546
|
+
direct.filter((dependency) => dependency.startsWith(scope)),
|
|
547
|
+
)
|
|
415
548
|
|
|
416
549
|
return theirs.has(name) && !ours.has(name)
|
|
417
550
|
}
|
|
418
551
|
|
|
419
552
|
/** The id of an advisory: the last part of its URL (GHSA-xxxx-xxxx-xxxx), else the number npm gives it. */
|
|
420
553
|
function advisoryId(via) {
|
|
421
|
-
const fromUrl =
|
|
554
|
+
const fromUrl =
|
|
555
|
+
typeof via.url === 'string' ? via.url.split('/').filter(Boolean).pop() : ''
|
|
422
556
|
|
|
423
557
|
return fromUrl || String(via.source ?? '')
|
|
424
558
|
}
|
|
@@ -445,7 +579,9 @@ export function parseNpmAudit(report) {
|
|
|
445
579
|
}
|
|
446
580
|
}
|
|
447
581
|
|
|
448
|
-
return [...rows.values()].sort((a, b) =>
|
|
582
|
+
return [...rows.values()].sort((a, b) =>
|
|
583
|
+
`${a.package}|${a.id}`.localeCompare(`${b.package}|${b.id}`),
|
|
584
|
+
)
|
|
449
585
|
}
|
|
450
586
|
|
|
451
587
|
/** The title of the issue of an advisory. It is also the key that keeps the issue from being opened twice. */
|
|
@@ -472,15 +608,23 @@ export function auditAlert(opened, project) {
|
|
|
472
608
|
if (notable.length === 0) return null
|
|
473
609
|
|
|
474
610
|
const silent = opened.length - notable.length
|
|
475
|
-
const lines = notable.map(
|
|
611
|
+
const lines = notable.map(
|
|
612
|
+
({ row, url }) =>
|
|
613
|
+
`${row.package} (${row.id}, ${row.severity || 'unknown'}): ${url}`,
|
|
614
|
+
)
|
|
476
615
|
if (silent > 0) lines.push(`${silent} more of a low severity, issue only.`)
|
|
477
616
|
|
|
478
|
-
return {
|
|
617
|
+
return {
|
|
618
|
+
title: `Security advisories: ${notable.length} new in ${project}`,
|
|
619
|
+
message: lines.join('\n'),
|
|
620
|
+
}
|
|
479
621
|
}
|
|
480
622
|
|
|
481
623
|
/** The labels of the issue of an advisory: the kind and the area, the priority from the severity, and Status::Ready. */
|
|
482
624
|
export function advisoryLabels(row) {
|
|
483
|
-
const priority =
|
|
625
|
+
const priority =
|
|
626
|
+
{ critical: 'Priority::Critical', high: 'Priority::High' }[row.severity] ??
|
|
627
|
+
'Priority::Low'
|
|
484
628
|
|
|
485
629
|
return [...ISSUE_LABELS, priority, 'Status::Ready'].join(',')
|
|
486
630
|
}
|
|
@@ -516,9 +660,18 @@ function secret(text) {
|
|
|
516
660
|
|
|
517
661
|
function run(command, args = [], options = {}) {
|
|
518
662
|
try {
|
|
519
|
-
return execFileSync(command, args, {
|
|
663
|
+
return execFileSync(command, args, {
|
|
664
|
+
encoding: 'utf8',
|
|
665
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
666
|
+
...options,
|
|
667
|
+
}).trim()
|
|
520
668
|
} catch (error) {
|
|
521
|
-
throw new Error(
|
|
669
|
+
throw new Error(
|
|
670
|
+
secret(
|
|
671
|
+
`${command} ${args.join(' ')} failed: ${error.stderr || error.message}`,
|
|
672
|
+
),
|
|
673
|
+
{ cause: error },
|
|
674
|
+
)
|
|
522
675
|
}
|
|
523
676
|
}
|
|
524
677
|
|
|
@@ -533,16 +686,27 @@ function capture(command, args = []) {
|
|
|
533
686
|
} catch (error) {
|
|
534
687
|
if (error.stdout) return error.stdout
|
|
535
688
|
|
|
536
|
-
throw new Error(
|
|
689
|
+
throw new Error(
|
|
690
|
+
secret(
|
|
691
|
+
`${command} ${args.join(' ')} failed: ${error.stderr || error.message}`,
|
|
692
|
+
),
|
|
693
|
+
{ cause: error },
|
|
694
|
+
)
|
|
537
695
|
}
|
|
538
696
|
}
|
|
539
697
|
|
|
540
698
|
async function api(method, path, body, token = process.env.GITLAB_TOKEN) {
|
|
541
|
-
const response = await fetch(
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
699
|
+
const response = await fetch(
|
|
700
|
+
`${process.env.CI_API_V4_URL ?? 'https://gitlab.com/api/v4'}${path}`,
|
|
701
|
+
{
|
|
702
|
+
method,
|
|
703
|
+
headers: {
|
|
704
|
+
'PRIVATE-TOKEN': token ?? '',
|
|
705
|
+
'Content-Type': 'application/json',
|
|
706
|
+
},
|
|
707
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
708
|
+
},
|
|
709
|
+
)
|
|
546
710
|
const text = await response.text()
|
|
547
711
|
|
|
548
712
|
return { status: response.status, ok: response.ok, data: parseBody(text) }
|
|
@@ -558,16 +722,24 @@ function parseBody(text) {
|
|
|
558
722
|
}
|
|
559
723
|
}
|
|
560
724
|
|
|
561
|
-
const sleep = (milliseconds) =>
|
|
725
|
+
const sleep = (milliseconds) =>
|
|
726
|
+
new Promise((resolve) => setTimeout(resolve, milliseconds))
|
|
562
727
|
|
|
563
728
|
function projectId() {
|
|
564
729
|
return process.env.CI_PROJECT_ID
|
|
565
730
|
}
|
|
566
731
|
|
|
567
732
|
function assertEnvironment() {
|
|
568
|
-
for (const name of [
|
|
733
|
+
for (const name of [
|
|
734
|
+
'GITLAB_TOKEN',
|
|
735
|
+
'CI_PROJECT_ID',
|
|
736
|
+
'CI_PROJECT_PATH',
|
|
737
|
+
'CI_SERVER_HOST',
|
|
738
|
+
]) {
|
|
569
739
|
if (!process.env[name])
|
|
570
|
-
throw new Error(
|
|
740
|
+
throw new Error(
|
|
741
|
+
`${name} is not set: run this in GitLab CI, with the group variable GITLAB_TOKEN.`,
|
|
742
|
+
)
|
|
571
743
|
}
|
|
572
744
|
|
|
573
745
|
// The runner clones the project with another user than the one of the job: Git refuses it otherwise.
|
|
@@ -600,7 +772,9 @@ function ensureGpg() {
|
|
|
600
772
|
if (available()) return
|
|
601
773
|
}
|
|
602
774
|
|
|
603
|
-
throw new Error(
|
|
775
|
+
throw new Error(
|
|
776
|
+
'gpg is not installed and could not be added: add gnupg to the image of the job.',
|
|
777
|
+
)
|
|
604
778
|
}
|
|
605
779
|
|
|
606
780
|
/** Import the signing key and make Git sign the commits and the tags with it. */
|
|
@@ -609,7 +783,10 @@ function configureSigning() {
|
|
|
609
783
|
|
|
610
784
|
ensureGpg()
|
|
611
785
|
|
|
612
|
-
run('gpg', ['--batch', '--import'], {
|
|
786
|
+
run('gpg', ['--batch', '--import'], {
|
|
787
|
+
input: signer.key,
|
|
788
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
789
|
+
})
|
|
613
790
|
const fingerprint = run('gpg', ['--list-secret-keys', '--with-colons'])
|
|
614
791
|
.split('\n')
|
|
615
792
|
.find((line) => line.startsWith('fpr'))
|
|
@@ -628,9 +805,13 @@ function remoteUrl() {
|
|
|
628
805
|
|
|
629
806
|
async function mergeRequestOptions() {
|
|
630
807
|
const user = await api('GET', '/user')
|
|
631
|
-
const milestones = await api(
|
|
808
|
+
const milestones = await api(
|
|
809
|
+
'GET',
|
|
810
|
+
`/projects/${projectId()}/milestones?state=active`,
|
|
811
|
+
)
|
|
632
812
|
const milestone = Array.isArray(milestones.data)
|
|
633
|
-
? (milestones.data.find((item) => item.title === 'Maintenance') ??
|
|
813
|
+
? (milestones.data.find((item) => item.title === 'Maintenance') ??
|
|
814
|
+
milestones.data[0])
|
|
634
815
|
: null
|
|
635
816
|
|
|
636
817
|
return {
|
|
@@ -644,10 +825,14 @@ async function mergeRequestOptions() {
|
|
|
644
825
|
/** Merge the request when its pipeline succeeds. GitLab needs a moment to create the pipeline of a new request. */
|
|
645
826
|
async function mergeWhenGreen(iid) {
|
|
646
827
|
for (let attempt = 0; attempt < 18; attempt++) {
|
|
647
|
-
const result = await api(
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
828
|
+
const result = await api(
|
|
829
|
+
'PUT',
|
|
830
|
+
`/projects/${projectId()}/merge_requests/${iid}/merge`,
|
|
831
|
+
{
|
|
832
|
+
merge_when_pipeline_succeeds: true,
|
|
833
|
+
should_remove_source_branch: true,
|
|
834
|
+
},
|
|
835
|
+
)
|
|
651
836
|
|
|
652
837
|
if (result.ok) {
|
|
653
838
|
log(`Merge request !${iid}: it is merged when its pipeline succeeds.`)
|
|
@@ -657,19 +842,31 @@ async function mergeWhenGreen(iid) {
|
|
|
657
842
|
await sleep(10_000)
|
|
658
843
|
}
|
|
659
844
|
|
|
660
|
-
log(
|
|
845
|
+
log(
|
|
846
|
+
`Merge request !${iid}: it could not be set to merge by itself, it stays open.`,
|
|
847
|
+
)
|
|
661
848
|
|
|
662
849
|
return false
|
|
663
850
|
}
|
|
664
851
|
|
|
665
852
|
async function openMergeRequests(query = '') {
|
|
666
|
-
const result = await api(
|
|
853
|
+
const result = await api(
|
|
854
|
+
'GET',
|
|
855
|
+
`/projects/${projectId()}/merge_requests?state=opened&per_page=100${query}`,
|
|
856
|
+
)
|
|
667
857
|
|
|
668
858
|
return Array.isArray(result.data) ? result.data : []
|
|
669
859
|
}
|
|
670
860
|
|
|
671
861
|
function ncu(args) {
|
|
672
|
-
return run('npx', [
|
|
862
|
+
return run('npx', [
|
|
863
|
+
'--yes',
|
|
864
|
+
'npm-check-updates',
|
|
865
|
+
'--target',
|
|
866
|
+
'minor',
|
|
867
|
+
...args,
|
|
868
|
+
'-u',
|
|
869
|
+
])
|
|
673
870
|
}
|
|
674
871
|
|
|
675
872
|
// ================
|
|
@@ -680,12 +877,16 @@ function raiseVulnerableOverrides() {
|
|
|
680
877
|
if (!manifest.overrides) return {}
|
|
681
878
|
|
|
682
879
|
const report = JSON.parse(capture('npm', ['audit', '--json']) || '{}')
|
|
683
|
-
const fixes = fixedOverrides(
|
|
684
|
-
|
|
880
|
+
const fixes = fixedOverrides(
|
|
881
|
+
manifest.overrides,
|
|
882
|
+
report.vulnerabilities,
|
|
883
|
+
(name) =>
|
|
884
|
+
JSON.parse(capture('npm', ['view', name, 'versions', '--json']) || '[]'),
|
|
685
885
|
)
|
|
686
886
|
|
|
687
887
|
for (const [name, version] of Object.entries(fixes)) {
|
|
688
|
-
if (typeof manifest.overrides[name] === 'string')
|
|
888
|
+
if (typeof manifest.overrides[name] === 'string')
|
|
889
|
+
manifest.overrides[name] = version
|
|
689
890
|
else manifest.overrides[name]['.'] = version
|
|
690
891
|
}
|
|
691
892
|
|
|
@@ -700,7 +901,11 @@ function raiseVulnerableOverrides() {
|
|
|
700
901
|
async function update(config) {
|
|
701
902
|
assertEnvironment()
|
|
702
903
|
|
|
703
|
-
if (
|
|
904
|
+
if (
|
|
905
|
+
(await openMergeRequests()).some((request) =>
|
|
906
|
+
request.source_branch.startsWith(BRANCH_PREFIX),
|
|
907
|
+
)
|
|
908
|
+
) {
|
|
704
909
|
log('An update is already open: nothing to do.')
|
|
705
910
|
return
|
|
706
911
|
}
|
|
@@ -725,7 +930,9 @@ async function update(config) {
|
|
|
725
930
|
// The lock file is refreshed inside the ranges, so that a fix of a package that this project only inherits arrives
|
|
726
931
|
// (overrides of a dependency do not apply here). The new versions wait for the cooling period, except for a fix.
|
|
727
932
|
const before = cooldownDate(config.cooldown)
|
|
728
|
-
const names = refreshableNames(
|
|
933
|
+
const names = refreshableNames(
|
|
934
|
+
JSON.parse(readFileSync('package-lock.json', 'utf8')),
|
|
935
|
+
)
|
|
729
936
|
|
|
730
937
|
// The refresh is a bonus: when npm cannot respect the cooling date (a range that starts at a version younger than
|
|
731
938
|
// the period, after an urgent update), it waits for the next run and the update goes on.
|
|
@@ -733,25 +940,38 @@ async function update(config) {
|
|
|
733
940
|
if (urgent || before === null) {
|
|
734
941
|
run('npm', ['update', '--no-audit', '--no-fund'])
|
|
735
942
|
} else if (names.length > 0) {
|
|
736
|
-
run('npm', [
|
|
943
|
+
run('npm', [
|
|
944
|
+
'update',
|
|
945
|
+
'--no-audit',
|
|
946
|
+
'--no-fund',
|
|
947
|
+
`--before=${before.toISOString()}`,
|
|
948
|
+
...names,
|
|
949
|
+
])
|
|
737
950
|
}
|
|
738
951
|
} catch (error) {
|
|
739
|
-
log(
|
|
952
|
+
log(
|
|
953
|
+
`The lock file is not refreshed this time: ${secret(error.message).split('\n')[0]}`,
|
|
954
|
+
)
|
|
740
955
|
}
|
|
741
956
|
|
|
742
957
|
if (urgent) {
|
|
743
958
|
try {
|
|
744
959
|
run('npm', ['audit', 'fix', '--no-fund'])
|
|
745
960
|
} catch (error) {
|
|
746
|
-
log(
|
|
961
|
+
log(
|
|
962
|
+
`npm audit fix did not apply: ${secret(error.message).split('\n')[0]}`,
|
|
963
|
+
)
|
|
747
964
|
}
|
|
748
965
|
|
|
749
966
|
// npm audit fix leaves the overrides alone: one that pins a vulnerable version is raised here.
|
|
750
967
|
try {
|
|
751
968
|
const raised = raiseVulnerableOverrides()
|
|
752
|
-
for (const [name, version] of Object.entries(raised))
|
|
969
|
+
for (const [name, version] of Object.entries(raised))
|
|
970
|
+
log(`The override of ${name} is raised to ${version}.`)
|
|
753
971
|
} catch (error) {
|
|
754
|
-
log(
|
|
972
|
+
log(
|
|
973
|
+
`The overrides are not raised this time: ${secret(error.message).split('\n')[0]}`,
|
|
974
|
+
)
|
|
755
975
|
}
|
|
756
976
|
}
|
|
757
977
|
}
|
|
@@ -760,10 +980,18 @@ async function update(config) {
|
|
|
760
980
|
const rows = diffDependencies(before, after, ecosystem.all)
|
|
761
981
|
|
|
762
982
|
// Only the manifest and the lock file: the postinstall of the tools can rewrite other files of the project in CI.
|
|
763
|
-
const files = [
|
|
983
|
+
const files = [
|
|
984
|
+
ecosystem.file,
|
|
985
|
+
...['package-lock.json', 'composer.lock'].filter((file) =>
|
|
986
|
+
existsSync(file),
|
|
987
|
+
),
|
|
988
|
+
]
|
|
764
989
|
|
|
765
990
|
// The one exception: the `ref:` of the shared pipeline follows the installed version of the tools.
|
|
766
|
-
if (
|
|
991
|
+
if (
|
|
992
|
+
existsSync(PIPELINE_FILE) &&
|
|
993
|
+
isRefOnlyChange(run('git', ['diff', '-U0', '--', PIPELINE_FILE]))
|
|
994
|
+
) {
|
|
767
995
|
files.push(PIPELINE_FILE)
|
|
768
996
|
}
|
|
769
997
|
|
|
@@ -792,7 +1020,10 @@ async function update(config) {
|
|
|
792
1020
|
...(await mergeRequestOptions()),
|
|
793
1021
|
})
|
|
794
1022
|
|
|
795
|
-
if (!request.ok)
|
|
1023
|
+
if (!request.ok)
|
|
1024
|
+
throw new Error(
|
|
1025
|
+
`The merge request could not be created: ${secret(JSON.stringify(request.data))}`,
|
|
1026
|
+
)
|
|
796
1027
|
|
|
797
1028
|
log(`Merge request !${request.data.iid} created.`)
|
|
798
1029
|
await mergeWhenGreen(request.data.iid)
|
|
@@ -803,9 +1034,14 @@ async function closeObsolete() {
|
|
|
803
1034
|
try {
|
|
804
1035
|
const found = parseNpmAudit(JSON.parse(capture('npm', ['audit', '--json'])))
|
|
805
1036
|
|
|
806
|
-
await closeResolved(
|
|
1037
|
+
await closeResolved(
|
|
1038
|
+
found.map(advisoryTitle),
|
|
1039
|
+
process.env.FRESHNESS_TOKEN || process.env.GITLAB_TOKEN,
|
|
1040
|
+
)
|
|
807
1041
|
} catch (error) {
|
|
808
|
-
log(
|
|
1042
|
+
log(
|
|
1043
|
+
`The obsolete issues could not be checked: ${secret(error.message).split('\n')[0]}`,
|
|
1044
|
+
)
|
|
809
1045
|
}
|
|
810
1046
|
}
|
|
811
1047
|
|
|
@@ -815,7 +1051,9 @@ async function release() {
|
|
|
815
1051
|
// The update that was just merged into develop may fix advisories: their issues have no reason to stay open.
|
|
816
1052
|
await closeObsolete()
|
|
817
1053
|
|
|
818
|
-
const existing = await openMergeRequests(
|
|
1054
|
+
const existing = await openMergeRequests(
|
|
1055
|
+
'&source_branch=develop&target_branch=main',
|
|
1056
|
+
)
|
|
819
1057
|
|
|
820
1058
|
if (existing.length > 0) {
|
|
821
1059
|
await mergeWhenGreen(existing[0].iid)
|
|
@@ -826,11 +1064,15 @@ async function release() {
|
|
|
826
1064
|
source_branch: 'develop',
|
|
827
1065
|
target_branch: 'main',
|
|
828
1066
|
title: COMMIT_SUBJECT,
|
|
829
|
-
description:
|
|
1067
|
+
description:
|
|
1068
|
+
'Brings the automatic update of the dependencies into main. It is merged when its pipeline succeeds.',
|
|
830
1069
|
...(await mergeRequestOptions()),
|
|
831
1070
|
})
|
|
832
1071
|
|
|
833
|
-
if (!request.ok)
|
|
1072
|
+
if (!request.ok)
|
|
1073
|
+
throw new Error(
|
|
1074
|
+
`The merge request could not be created: ${secret(JSON.stringify(request.data))}`,
|
|
1075
|
+
)
|
|
834
1076
|
|
|
835
1077
|
log(`Merge request !${request.data.iid} created.`)
|
|
836
1078
|
await mergeWhenGreen(request.data.iid)
|
|
@@ -843,15 +1085,29 @@ async function tag(config) {
|
|
|
843
1085
|
const last = latestTag(run('git', ['tag', '--list']).split('\n'))
|
|
844
1086
|
// git reads a basic regular expression: the parentheses of the subject are literal.
|
|
845
1087
|
const commits = last
|
|
846
|
-
? run('git', [
|
|
1088
|
+
? run('git', [
|
|
1089
|
+
'log',
|
|
1090
|
+
`${last}..HEAD`,
|
|
1091
|
+
'--format=%s',
|
|
1092
|
+
`--grep=^${COMMIT_SUBJECT}`,
|
|
1093
|
+
])
|
|
847
1094
|
.split('\n')
|
|
848
1095
|
.filter(Boolean)
|
|
849
1096
|
: []
|
|
850
1097
|
const ecosystem = ECOSYSTEMS[detectEcosystem()]
|
|
851
|
-
const before = last
|
|
1098
|
+
const before = last
|
|
1099
|
+
? JSON.parse(run('git', ['show', `${last}:${ecosystem.file}`]))
|
|
1100
|
+
: null
|
|
852
1101
|
const after = JSON.parse(readFileSync(ecosystem.file, 'utf8'))
|
|
853
1102
|
|
|
854
|
-
const plan = planTag({
|
|
1103
|
+
const plan = planTag({
|
|
1104
|
+
config,
|
|
1105
|
+
last,
|
|
1106
|
+
commits,
|
|
1107
|
+
before,
|
|
1108
|
+
after,
|
|
1109
|
+
sections: ecosystem.runtime,
|
|
1110
|
+
})
|
|
855
1111
|
|
|
856
1112
|
if (!plan.version) {
|
|
857
1113
|
log(`No tag: ${plan.reason}.`)
|
|
@@ -859,16 +1115,32 @@ async function tag(config) {
|
|
|
859
1115
|
}
|
|
860
1116
|
|
|
861
1117
|
configureSigning()
|
|
862
|
-
run('git', [
|
|
1118
|
+
run('git', [
|
|
1119
|
+
'tag',
|
|
1120
|
+
'-s',
|
|
1121
|
+
plan.version,
|
|
1122
|
+
'-m',
|
|
1123
|
+
plan.version,
|
|
1124
|
+
process.env.CI_COMMIT_SHA,
|
|
1125
|
+
])
|
|
863
1126
|
run('git', ['push', remoteUrl(), `refs/tags/${plan.version}`])
|
|
864
1127
|
|
|
865
|
-
log(
|
|
1128
|
+
log(
|
|
1129
|
+
`Tag ${plan.version} signed and pushed (last tag ${last}): ${plan.reason}. Its pipeline publishes the package.`,
|
|
1130
|
+
)
|
|
866
1131
|
}
|
|
867
1132
|
|
|
868
1133
|
/** True when the registry of the ecosystem (npm or Packagist) lists the version. */
|
|
869
1134
|
async function isAvailable(ecosystem, name, version) {
|
|
870
1135
|
if (ecosystem === 'npm') {
|
|
871
|
-
return
|
|
1136
|
+
return (
|
|
1137
|
+
run('npm', [
|
|
1138
|
+
'view',
|
|
1139
|
+
`${name}@${version}`,
|
|
1140
|
+
'version',
|
|
1141
|
+
'--prefer-online',
|
|
1142
|
+
]) === version
|
|
1143
|
+
)
|
|
872
1144
|
}
|
|
873
1145
|
|
|
874
1146
|
const response = await fetch(`https://repo.packagist.org/p2/${name}.json`)
|
|
@@ -877,7 +1149,9 @@ async function isAvailable(ecosystem, name, version) {
|
|
|
877
1149
|
|
|
878
1150
|
const data = await response.json()
|
|
879
1151
|
|
|
880
|
-
return (data.packages?.[name] ?? []).some(
|
|
1152
|
+
return (data.packages?.[name] ?? []).some(
|
|
1153
|
+
(release) => release.version === version,
|
|
1154
|
+
)
|
|
881
1155
|
}
|
|
882
1156
|
|
|
883
1157
|
async function cascade(config) {
|
|
@@ -907,7 +1181,10 @@ async function cascade(config) {
|
|
|
907
1181
|
if (!available) await sleep(30_000)
|
|
908
1182
|
}
|
|
909
1183
|
|
|
910
|
-
if (!available)
|
|
1184
|
+
if (!available)
|
|
1185
|
+
throw new Error(
|
|
1186
|
+
`${name}@${version} is not on the registry after 30 minutes.`,
|
|
1187
|
+
)
|
|
911
1188
|
|
|
912
1189
|
for (const group of config.cascade.groups ?? []) {
|
|
913
1190
|
const projects = await api(
|
|
@@ -934,7 +1211,9 @@ async function cascade(config) {
|
|
|
934
1211
|
variables: [{ key: 'UPDATE_DEPENDENCIES', value: 'true' }],
|
|
935
1212
|
})
|
|
936
1213
|
|
|
937
|
-
log(
|
|
1214
|
+
log(
|
|
1215
|
+
`${project.path_with_namespace}: ${started.ok ? 'update started' : `not started (${started.status})`}.`,
|
|
1216
|
+
)
|
|
938
1217
|
}
|
|
939
1218
|
}
|
|
940
1219
|
}
|
|
@@ -942,26 +1221,46 @@ async function cascade(config) {
|
|
|
942
1221
|
// ================
|
|
943
1222
|
|
|
944
1223
|
async function issueExists(title, token) {
|
|
945
|
-
const query = new URLSearchParams({
|
|
946
|
-
|
|
1224
|
+
const query = new URLSearchParams({
|
|
1225
|
+
state: 'opened',
|
|
1226
|
+
search: title,
|
|
1227
|
+
in: 'title',
|
|
1228
|
+
})
|
|
1229
|
+
const result = await api(
|
|
1230
|
+
'GET',
|
|
1231
|
+
`/projects/${projectId()}/issues?${query}`,
|
|
1232
|
+
undefined,
|
|
1233
|
+
token,
|
|
1234
|
+
)
|
|
947
1235
|
|
|
948
|
-
return (Array.isArray(result.data) ? result.data : []).some(
|
|
1236
|
+
return (Array.isArray(result.data) ? result.data : []).some(
|
|
1237
|
+
(issue) => issue.title === title,
|
|
1238
|
+
)
|
|
949
1239
|
}
|
|
950
1240
|
|
|
951
1241
|
async function createIssue(title, description, labels, token) {
|
|
952
1242
|
// The assignee is given by the variable ISSUE_ASSIGNEE_ID; without it, the user of the token.
|
|
953
1243
|
let assignee = Number(process.env.ISSUE_ASSIGNEE_ID) || 0
|
|
954
1244
|
|
|
955
|
-
if (assignee === 0)
|
|
1245
|
+
if (assignee === 0)
|
|
1246
|
+
assignee = (await api('GET', '/user', undefined, token)).data?.id ?? 0
|
|
956
1247
|
|
|
957
1248
|
const issue = await api(
|
|
958
1249
|
'POST',
|
|
959
1250
|
`/projects/${projectId()}/issues`,
|
|
960
|
-
{
|
|
1251
|
+
{
|
|
1252
|
+
title,
|
|
1253
|
+
description,
|
|
1254
|
+
labels,
|
|
1255
|
+
assignee_ids: assignee > 0 ? [assignee] : [],
|
|
1256
|
+
},
|
|
961
1257
|
token,
|
|
962
1258
|
)
|
|
963
1259
|
|
|
964
|
-
if (!issue.ok)
|
|
1260
|
+
if (!issue.ok)
|
|
1261
|
+
throw new Error(
|
|
1262
|
+
`The issue could not be created: ${secret(JSON.stringify(issue.data))}`,
|
|
1263
|
+
)
|
|
965
1264
|
|
|
966
1265
|
return issue.data.web_url
|
|
967
1266
|
}
|
|
@@ -975,20 +1274,35 @@ async function closeResolved(titles, token) {
|
|
|
975
1274
|
in: 'title',
|
|
976
1275
|
per_page: '100',
|
|
977
1276
|
})
|
|
978
|
-
const result = await api(
|
|
1277
|
+
const result = await api(
|
|
1278
|
+
'GET',
|
|
1279
|
+
`/projects/${projectId()}/issues?${query}`,
|
|
1280
|
+
undefined,
|
|
1281
|
+
token,
|
|
1282
|
+
)
|
|
979
1283
|
|
|
980
1284
|
for (const issue of Array.isArray(result.data) ? result.data : []) {
|
|
981
|
-
if (
|
|
1285
|
+
if (
|
|
1286
|
+
!issue.title.startsWith(ADVISORY_TITLE_PREFIX) ||
|
|
1287
|
+
titles.includes(issue.title)
|
|
1288
|
+
)
|
|
1289
|
+
continue
|
|
982
1290
|
|
|
983
1291
|
const path = `/projects/${projectId()}/issues/${issue.iid}`
|
|
984
1292
|
await api(
|
|
985
1293
|
'POST',
|
|
986
1294
|
`${path}/notes`,
|
|
987
|
-
{
|
|
1295
|
+
{
|
|
1296
|
+
body: `The audit of ${process.env.CI_PIPELINE_URL} no longer finds this advisory.`,
|
|
1297
|
+
},
|
|
988
1298
|
token,
|
|
989
1299
|
)
|
|
990
1300
|
const closed = await api('PUT', path, { state_event: 'close' }, token)
|
|
991
|
-
log(
|
|
1301
|
+
log(
|
|
1302
|
+
closed.ok
|
|
1303
|
+
? `Issue closed: ${issue.title}`
|
|
1304
|
+
: `Issue not closed (${closed.status}): ${issue.title}`,
|
|
1305
|
+
)
|
|
992
1306
|
}
|
|
993
1307
|
}
|
|
994
1308
|
|
|
@@ -1012,11 +1326,16 @@ async function notify(title, message) {
|
|
|
1012
1326
|
async function readPolicy(ecosystem) {
|
|
1013
1327
|
try {
|
|
1014
1328
|
const policy = await fetchPolicy(ecosystem)
|
|
1015
|
-
log(
|
|
1329
|
+
log(
|
|
1330
|
+
`Dependency policy: ${policy.entries.map((entry) => entry.package).join(', ') || 'nothing is held back'}.`,
|
|
1331
|
+
)
|
|
1016
1332
|
|
|
1017
1333
|
return policy
|
|
1018
1334
|
} catch (error) {
|
|
1019
|
-
await notify(
|
|
1335
|
+
await notify(
|
|
1336
|
+
'Dependency policy unreachable',
|
|
1337
|
+
`${process.env.CI_PROJECT_PATH}: ${secret(error.message)}`,
|
|
1338
|
+
)
|
|
1020
1339
|
throw error
|
|
1021
1340
|
}
|
|
1022
1341
|
}
|
|
@@ -1026,7 +1345,9 @@ async function audit() {
|
|
|
1026
1345
|
assertEnvironment()
|
|
1027
1346
|
|
|
1028
1347
|
const found = parseNpmAudit(JSON.parse(capture('npm', ['audit', '--json'])))
|
|
1029
|
-
const lock = existsSync('package-lock.json')
|
|
1348
|
+
const lock = existsSync('package-lock.json')
|
|
1349
|
+
? JSON.parse(readFileSync('package-lock.json', 'utf8'))
|
|
1350
|
+
: null
|
|
1030
1351
|
const auto = process.env.AUTO_FIX === 'true'
|
|
1031
1352
|
const token = process.env.FRESHNESS_TOKEN || process.env.GITLAB_TOKEN
|
|
1032
1353
|
const titles = []
|
|
@@ -1048,7 +1369,9 @@ async function audit() {
|
|
|
1048
1369
|
// Inherited from a package of the group: the issue is in the package that brings it, and the update of this project
|
|
1049
1370
|
// takes the fix when there is one.
|
|
1050
1371
|
if (isInherited(lock, row.package)) {
|
|
1051
|
-
log(
|
|
1372
|
+
log(
|
|
1373
|
+
`Inherited from the group, followed in the package that brings it: ${title}`,
|
|
1374
|
+
)
|
|
1052
1375
|
continue
|
|
1053
1376
|
}
|
|
1054
1377
|
|
|
@@ -1065,11 +1388,19 @@ async function audit() {
|
|
|
1065
1388
|
await closeResolved(titles, token)
|
|
1066
1389
|
|
|
1067
1390
|
if (!auto || !urgent) {
|
|
1068
|
-
log(
|
|
1391
|
+
log(
|
|
1392
|
+
auto
|
|
1393
|
+
? 'Nothing urgent: the weekly update takes the rest.'
|
|
1394
|
+
: 'AUTO_FIX is off: update by hand.',
|
|
1395
|
+
)
|
|
1069
1396
|
return
|
|
1070
1397
|
}
|
|
1071
1398
|
|
|
1072
|
-
if (
|
|
1399
|
+
if (
|
|
1400
|
+
(await openMergeRequests()).some((request) =>
|
|
1401
|
+
request.source_branch.startsWith(BRANCH_PREFIX),
|
|
1402
|
+
)
|
|
1403
|
+
) {
|
|
1073
1404
|
log('An update is already open: nothing to start.')
|
|
1074
1405
|
return
|
|
1075
1406
|
}
|
|
@@ -1086,13 +1417,21 @@ async function audit() {
|
|
|
1086
1417
|
},
|
|
1087
1418
|
token,
|
|
1088
1419
|
)
|
|
1089
|
-
log(
|
|
1420
|
+
log(
|
|
1421
|
+
started.ok
|
|
1422
|
+
? 'Update started.'
|
|
1423
|
+
: `The update could not be started (${started.status}).`,
|
|
1424
|
+
)
|
|
1090
1425
|
}
|
|
1091
1426
|
|
|
1092
1427
|
/** The peer range of `name` in the latest release of `peer`, or null. */
|
|
1093
1428
|
function peerRange(peer, name) {
|
|
1094
1429
|
try {
|
|
1095
|
-
return
|
|
1430
|
+
return (
|
|
1431
|
+
JSON.parse(
|
|
1432
|
+
run('npm', ['view', peer, 'peerDependencies', '--json']) || '{}',
|
|
1433
|
+
)?.[name] ?? null
|
|
1434
|
+
)
|
|
1096
1435
|
} catch {
|
|
1097
1436
|
return null
|
|
1098
1437
|
}
|
|
@@ -1127,12 +1466,15 @@ async function policyWatch() {
|
|
|
1127
1466
|
const file = POLICY_FILES.npm
|
|
1128
1467
|
const doc = JSON.parse(readFileSync(file, 'utf8'))
|
|
1129
1468
|
const errors = validatePolicy(doc)
|
|
1130
|
-
if (errors.length > 0)
|
|
1469
|
+
if (errors.length > 0)
|
|
1470
|
+
throw new Error(`${file} is not valid: ${errors.join(' ')}`)
|
|
1131
1471
|
|
|
1132
1472
|
const lifted = doc.entries.filter((entry) => {
|
|
1133
1473
|
if (!entry.unblock) return false
|
|
1134
1474
|
|
|
1135
|
-
const ranges = Object.fromEntries(
|
|
1475
|
+
const ranges = Object.fromEntries(
|
|
1476
|
+
entry.unblock.peers.map((peer) => [peer, peerRange(peer, entry.package)]),
|
|
1477
|
+
)
|
|
1136
1478
|
|
|
1137
1479
|
return isLifted(entry, ranges)
|
|
1138
1480
|
})
|
|
@@ -1142,7 +1484,11 @@ async function policyWatch() {
|
|
|
1142
1484
|
return
|
|
1143
1485
|
}
|
|
1144
1486
|
|
|
1145
|
-
if (
|
|
1487
|
+
if (
|
|
1488
|
+
(await openMergeRequests()).some((request) =>
|
|
1489
|
+
request.source_branch.startsWith(POLICY_BRANCH_PREFIX),
|
|
1490
|
+
)
|
|
1491
|
+
) {
|
|
1146
1492
|
log('A merge request that lifts an entry is already open.')
|
|
1147
1493
|
return
|
|
1148
1494
|
}
|
|
@@ -1158,7 +1504,13 @@ async function policyWatch() {
|
|
|
1158
1504
|
`${JSON.stringify({ ...doc, entries: doc.entries.filter((entry) => !lifted.includes(entry)) }, null, 2)}\n`,
|
|
1159
1505
|
)
|
|
1160
1506
|
run('git', ['add', file])
|
|
1161
|
-
run('git', [
|
|
1507
|
+
run('git', [
|
|
1508
|
+
'commit',
|
|
1509
|
+
'-m',
|
|
1510
|
+
subject,
|
|
1511
|
+
'-m',
|
|
1512
|
+
'The peer ranges of the packages that held it back now accept it.',
|
|
1513
|
+
])
|
|
1162
1514
|
run('git', ['push', remoteUrl(), `HEAD:refs/heads/${branch}`])
|
|
1163
1515
|
|
|
1164
1516
|
const request = await api('POST', `/projects/${projectId()}/merge_requests`, {
|
|
@@ -1170,18 +1522,27 @@ async function policyWatch() {
|
|
|
1170
1522
|
...(await mergeRequestOptions()),
|
|
1171
1523
|
})
|
|
1172
1524
|
|
|
1173
|
-
if (!request.ok)
|
|
1525
|
+
if (!request.ok)
|
|
1526
|
+
throw new Error(
|
|
1527
|
+
`The merge request could not be created: ${secret(JSON.stringify(request.data))}`,
|
|
1528
|
+
)
|
|
1174
1529
|
|
|
1175
1530
|
log(`Merge request !${request.data.iid} created.`)
|
|
1176
|
-
await notify(
|
|
1531
|
+
await notify(
|
|
1532
|
+
'Dependency policy: block lifted',
|
|
1533
|
+
`${names}: ${request.data.web_url}`,
|
|
1534
|
+
)
|
|
1177
1535
|
await mergeWhenGreen(request.data.iid)
|
|
1178
1536
|
}
|
|
1179
1537
|
|
|
1180
1538
|
/** Fail when the working tree updates a package of the policy compared with `base`: a range or a locked version. */
|
|
1181
1539
|
async function policyCheck(_config, base) {
|
|
1182
|
-
if (!base)
|
|
1540
|
+
if (!base)
|
|
1541
|
+
throw new Error('Usage: node update-dependencies.mjs policy-check <ref>')
|
|
1183
1542
|
|
|
1184
|
-
const blocked = (await readPolicy('npm')).entries.map(
|
|
1543
|
+
const blocked = (await readPolicy('npm')).entries.map(
|
|
1544
|
+
(entry) => entry.package,
|
|
1545
|
+
)
|
|
1185
1546
|
const atBase = (file) => {
|
|
1186
1547
|
try {
|
|
1187
1548
|
return JSON.parse(capture('git', ['show', `${base}:${file}`]))
|
|
@@ -1189,7 +1550,8 @@ async function policyCheck(_config, base) {
|
|
|
1189
1550
|
return null
|
|
1190
1551
|
}
|
|
1191
1552
|
}
|
|
1192
|
-
const now = (file) =>
|
|
1553
|
+
const now = (file) =>
|
|
1554
|
+
existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : null
|
|
1193
1555
|
|
|
1194
1556
|
const problems = policyViolations({
|
|
1195
1557
|
blocked,
|
|
@@ -1200,14 +1562,18 @@ async function policyCheck(_config, base) {
|
|
|
1200
1562
|
})
|
|
1201
1563
|
|
|
1202
1564
|
for (const problem of problems) console.error(problem)
|
|
1203
|
-
if (problems.length > 0)
|
|
1565
|
+
if (problems.length > 0)
|
|
1566
|
+
throw new Error(
|
|
1567
|
+
'The change updates a package that the dependency policy holds back.',
|
|
1568
|
+
)
|
|
1204
1569
|
|
|
1205
1570
|
log('No package of the dependency policy is updated.')
|
|
1206
1571
|
}
|
|
1207
1572
|
|
|
1208
1573
|
/** Rewrite the ignore rules of .github/dependabot.yml from the policy. The workflow of the repository commits the result. */
|
|
1209
1574
|
async function dependabotSync() {
|
|
1210
|
-
if (!existsSync(DEPENDABOT_FILE))
|
|
1575
|
+
if (!existsSync(DEPENDABOT_FILE))
|
|
1576
|
+
throw new Error(`${DEPENDABOT_FILE} does not exist.`)
|
|
1211
1577
|
|
|
1212
1578
|
const names = (await readPolicy('npm')).entries.map((entry) => entry.package)
|
|
1213
1579
|
const text = readFileSync(DEPENDABOT_FILE, 'utf8')
|
|
@@ -1225,7 +1591,9 @@ async function dependabotSync() {
|
|
|
1225
1591
|
}
|
|
1226
1592
|
|
|
1227
1593
|
writeFileSync(DEPENDABOT_FILE, synced)
|
|
1228
|
-
log(
|
|
1594
|
+
log(
|
|
1595
|
+
`The ignore rules of Dependabot are updated: ${names.join(', ') || 'none'}.`,
|
|
1596
|
+
)
|
|
1229
1597
|
}
|
|
1230
1598
|
|
|
1231
1599
|
const modes = {
|
|
@@ -1257,6 +1625,9 @@ export async function main(mode, argument) {
|
|
|
1257
1625
|
}
|
|
1258
1626
|
}
|
|
1259
1627
|
|
|
1260
|
-
if (
|
|
1628
|
+
if (
|
|
1629
|
+
process.argv[1] &&
|
|
1630
|
+
import.meta.url === pathToFileURL(process.argv[1]).href
|
|
1631
|
+
) {
|
|
1261
1632
|
await main(process.argv[2], process.argv[3])
|
|
1262
1633
|
}
|