Skip to content

Commit ca131b7

Browse files
committed
fixup! fix(git-node): handle multi-line trailers
1 parent 1e929e2 commit ca131b7

2 files changed

Lines changed: 21 additions & 20 deletions

File tree

lib/landing_session.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -326,24 +326,25 @@ export default class LandingSession extends Session {
326326
const originalTrailers = interpretTrailers(original);
327327
const containCVETrailer = CVE_RE.test(originalTrailers);
328328

329-
const filterTrailer = (line) => ([key]) =>
330-
line.startsWith(key) &&
331-
new RegExp(`^${RegExp.escape?.(key) ?? key}\\s*:`).test(line);
332329
const amended = original.trim().split('\n');
333-
const stillInTrailers = () => {
334-
const result = interpretTrailers(amended.join('\n'));
335-
return result.length && originalTrailers.startsWith(result.trim());
336-
};
337-
for (let i = amended.length - 1; amended[i] === '' || stillInTrailers(); i--) {
338-
// Remove last line until git no longer detects any trailers
339-
amended.pop();
340-
}
330+
let keptTrailers = [];
331+
if (originalTrailers) {
332+
const stillInTrailers = () => {
333+
const result = interpretTrailers(amended.join('\n'));
334+
return result.length && originalTrailers.startsWith(result.trim());
335+
};
336+
// Remove all lines until we find an empty string, which should get of all
337+
// trailers in most cases.
338+
amended.splice(amended.lastIndexOf(''), Infinity);
339+
for (let i = amended.length - 1; amended[i] === '' || stillInTrailers(); i--) {
340+
// Remove last line until git no longer detects any trailers
341+
amended.pop();
342+
}
341343

342-
// Only keep existing trailers that we won't add ourselves
343-
const trailersToFilterOut = ['BACKPORT-PR-URL', 'REVIEWED-BY'];
344-
if (!this.backport) trailersToFilterOut.push('PR-URL');
345-
const keptTrailers =
346-
originalTrailers
344+
// Only keep existing trailers that we won't add ourselves
345+
const trailersToFilterOut = ['BACKPORT-PR-URL', 'REVIEWED-BY'];
346+
if (!this.backport) trailersToFilterOut.push('PR-URL');
347+
keptTrailers = originalTrailers
347348
.split('\n')
348349
.map(trailer => {
349350
const separatorIndex = trailer.indexOf(':');
@@ -353,11 +354,11 @@ export default class LandingSession extends Session {
353354
.filter(
354355
([key]) => !trailersToFilterOut.includes(key.toUpperCase())
355356
);
357+
}
356358
amended.push('', ...keptTrailers.map(([key, value]) => `${key}: ${value}`));
357359

358360
for (const line of metadata.trim().split('\n')) {
359-
const foundMatchingTrailer = keptTrailers.find(filterTrailer(line));
360-
if (foundMatchingTrailer && line === foundMatchingTrailer.join(': ')) {
361+
if (keptTrailers.some((trailer) => line.toUpperCase() === trailer.join(': ').toUpperCase())) {
361362
cli.warn(`Found ${line}, skipping..`);
362363
continue;
363364
}

test/unit/amend.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ describe('LandingSession.prototype.generateAmendedMessage', () => {
105105
t.assert.strictEqual(result, 'subsystem: foobar\n\nTrailer: Value\nPR-URL: http://example.com/123\nReviewed-By: user1 <[email protected]>');
106106
});
107107

108-
it('should handle empty message', async(t) => {
108+
it('should handle empty message (although not supported)', async(t) => {
109109
const session = createSession();
110110
const result = await session.generateAmendedMessage('');
111111

112-
t.assert.strictEqual(result, '\nPR-URL: http://example.com/123\nReviewed-By: user1 <[email protected]>');
112+
t.assert.strictEqual(result, '\n\nPR-URL: http://example.com/123\nReviewed-By: user1 <[email protected]>');
113113
});
114114

115115
it('should handle multi-line trailers', async(t) => {

0 commit comments

Comments
 (0)