diff --git a/lib/landing_session.js b/lib/landing_session.js index 424e5c97..c93d3e6c 100644 --- a/lib/landing_session.js +++ b/lib/landing_session.js @@ -316,7 +316,7 @@ export default class LandingSession extends Session { const BACKPORT_RE = /^BACKPORT-PR-URL\s*:\s*(\S+)$/i; const PR_RE = /^PR-URL\s*:\s*(\S+)$/i; const REVIEW_RE = /^Reviewed-By\s*:\s*(\S+)$/i; - const CVE_RE = /^CVE-ID\s*:\s*(\S+)$/i; + const CVE_RE = /^CVE-ID\s*:\s*(\S+)$/im; this.startAmending(); @@ -328,41 +328,30 @@ export default class LandingSession extends Session { // git has very specific rules about what is a trailer and what is not. // Instead of trying to implement those ourselves, let git parse the // original commit message and see if it outputs any trailers. - const originalHasTrailers = runSync('git', [ + const originalTrailers = runSync('git', [ 'interpret-trailers', '--parse', '--no-divider' ], { input: `${original}\n` - }).trim().length !== 0; + }); + const containCVETrailer = CVE_RE.test(originalTrailers); const metadata = this.metadata.trim().split('\n'); - // Filtering out metadata that we will add ourselves: - const isFiltered = line => + const amended = original.slice(0, -originalTrailers.length).trim().split('\n'); + + // Only keep existing trailers that we won't add ourselves + const keptTrailers = originalTrailers.split('\n').filter(line => + !!line && (!PR_RE.test(line) || this.backport) && !BACKPORT_RE.test(line) && - !REVIEW_RE.test(line); - const amended = original.split('\n').filter(isFiltered); - - // If the original commit message already contains trailers (such as - // "Co-authored-by"), we simply add our own metadata after those. Otherwise, - // we have to add an empty line so that git recognizes our own metadata as - // trailers in the amended commit message. - if (!originalHasTrailers) { - amended.push(''); - } + !REVIEW_RE.test(line)); + amended.push('', ...keptTrailers); - let containCVETrailer = false; for (const line of metadata) { - if (line.length !== 0 && original.includes(line) && !isFiltered(line)) { - containCVETrailer ||= CVE_RE.test(line); - if (originalHasTrailers) { - cli.warn(`Found ${line}, skipping..`); - continue; - } else { - cli.error('Git found no trailers in the original commit message, ' + - `but '${line}' is present and should be a trailer.`); - process.exit(1); // make it work with git rebase -x - } + if (keptTrailers.includes(line)) { + cli.warn(`Found ${line}, skipping..`); + continue; } + if (BACKPORT_RE.test(line)) { const prIndex = (amended.findIndex(datum => PR_RE.test(datum)) + 1) || diff --git a/lib/session.js b/lib/session.js index 088d168b..0f130108 100644 --- a/lib/session.js +++ b/lib/session.js @@ -127,6 +127,7 @@ export default class Session { writeJson(this.sessionPath, { state: STARTED, prid: this.prid, + backport: this.backport, // Filter out getters, those are likely encrypted data we don't need on the session. config: Object.entries(Object.getOwnPropertyDescriptors(this.config)) .reduce((pv, [key, desc]) => { @@ -271,6 +272,7 @@ export default class Session { const sess = this.session; if (sess.prid) { this.prid = sess.prid; + this.backport = sess.backport; this.config = sess.config; } return this;