@node-core/utils 6.3.1 → 6.3.2

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.
@@ -306,25 +306,14 @@ export default class LandingSession extends Session {
306
306
  await this.tryCompleteLanding(commitInfo);
307
307
  }
308
308
 
309
- async amend() {
310
- const { cli } = this;
311
- if (!this.readyToAmend()) {
312
- cli.warn('Not yet ready to amend, run `git node land --abort`');
313
- return;
314
- }
309
+ async generateAmendedMessage(original) {
310
+ const { cli, metadata } = this;
315
311
 
316
312
  const BACKPORT_RE = /^BACKPORT-PR-URL\s*:\s*(\S+)$/i;
317
313
  const PR_RE = /^PR-URL\s*:\s*(\S+)$/i;
318
314
  const REVIEW_RE = /^Reviewed-By\s*:\s*(\S+)$/i;
319
315
  const CVE_RE = /^CVE-ID\s*:\s*(\S+)$/im;
320
316
 
321
- this.startAmending();
322
-
323
- const rev = this.getCurrentRev();
324
- const original = runSync('git', [
325
- 'show', 'HEAD', '-s', '--format=%B'
326
- ]).trim();
327
-
328
317
  // git has very specific rules about what is a trailer and what is not.
329
318
  // Instead of trying to implement those ourselves, let git parse the
330
319
  // original commit message and see if it outputs any trailers.
@@ -332,22 +321,35 @@ export default class LandingSession extends Session {
332
321
  'interpret-trailers', '--parse', '--no-divider'
333
322
  ], {
334
323
  input: `${original}\n`
335
- });
324
+ }).split('\n').map(trailer => {
325
+ const separatorIndex = trailer.indexOf(':');
326
+ return [trailer.slice(0, separatorIndex), trailer.slice(separatorIndex + 2)];
327
+ }).slice(0, -1); // Remove the last line return entry
328
+
336
329
  const containCVETrailer = CVE_RE.test(originalTrailers);
337
330
 
338
- const metadata = this.metadata.trim().split('\n');
339
- const amended = original.slice(0, -originalTrailers.length).trim().split('\n');
331
+ const filterTrailer = (line) => ([key]) =>
332
+ line.startsWith(key) &&
333
+ new RegExp(`^${RegExp.escape?.(key) ?? key}\\s*:`).test(line);
334
+ const amended = original.trim().split('\n')
335
+ .filter(line =>
336
+ !line.includes(':') ||
337
+ !originalTrailers.some(filterTrailer(line)));
338
+ for (let i = amended.length - 1; amended[i] === ''; i--) {
339
+ amended.pop();
340
+ }
340
341
 
341
342
  // Only keep existing trailers that we won't add ourselves
342
- const keptTrailers = originalTrailers.split('\n').filter(line =>
343
- !!line &&
344
- (!PR_RE.test(line) || this.backport) &&
345
- !BACKPORT_RE.test(line) &&
346
- !REVIEW_RE.test(line));
347
- amended.push('', ...keptTrailers);
348
-
349
- for (const line of metadata) {
350
- if (keptTrailers.includes(line)) {
343
+ const trailersToFilterOut = ['BACKPORT-PR-URL', 'REVIEWED-BY'];
344
+ if (!this.backport) trailersToFilterOut.push('PR-URL');
345
+ const keptTrailers = originalTrailers.filter(
346
+ ([key]) => !trailersToFilterOut.includes(key.toUpperCase())
347
+ );
348
+ amended.push('', ...keptTrailers.map(([key, value]) => `${key}: ${value}`));
349
+
350
+ for (const line of metadata.trim().split('\n')) {
351
+ const foundMatchingTrailer = keptTrailers.find(filterTrailer(line));
352
+ if (foundMatchingTrailer && line === foundMatchingTrailer.join(': ')) {
351
353
  cli.warn(`Found ${line}, skipping..`);
352
354
  continue;
353
355
  }
@@ -399,8 +401,24 @@ export default class LandingSession extends Session {
399
401
  amended.push('CVE-ID: ' + cveID);
400
402
  }
401
403
  }
404
+ return amended.join('\n');
405
+ }
406
+
407
+ async amend() {
408
+ const { cli } = this;
409
+ if (!this.readyToAmend()) {
410
+ cli.warn('Not yet ready to amend, run `git node land --abort`');
411
+ return;
412
+ }
413
+
414
+ this.startAmending();
415
+
416
+ const rev = this.getCurrentRev();
417
+ const original = runSync('git', [
418
+ 'show', 'HEAD', '-s', '--format=%B'
419
+ ]).trim();
402
420
 
403
- const message = amended.join('\n');
421
+ const message = await this.generateAmendedMessage(original);
404
422
  const messageFile = this.saveMessage(rev, message);
405
423
  cli.separator('New Message');
406
424
  cli.log(message.trim());
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@node-core/utils",
3
- "version": "6.3.1",
3
+ "version": "6.3.2",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@node-core/utils",
9
- "version": "6.3.1",
9
+ "version": "6.3.2",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@inquirer/prompts": "^7.4.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node-core/utils",
3
- "version": "6.3.1",
3
+ "version": "6.3.2",
4
4
  "description": "Utilities for Node.js core collaborators",
5
5
  "type": "module",
6
6
  "engines": {