fix: preserve new lines in body and comments (#72)

This commit is contained in:
Andrea Lamparelli 2023-07-27 15:35:23 +02:00 committed by GitHub
parent a402fa4525
commit fa43ffc1dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 44 additions and 38 deletions

1
.gitignore vendored
View file

@ -14,3 +14,4 @@ build/
# temporary files created during tests # temporary files created during tests
*test*.json *test*.json
bp

View file

@ -29,11 +29,11 @@ inputs:
title: title:
description: "Backporting PR title. Default is the original PR title prefixed by the target branch" description: "Backporting PR title. Default is the original PR title prefixed by the target branch"
required: false required: false
body:
description: "Backporting PR body. Default is the original PR body prefixed by `backport: <original-pr-link>`"
required: false
body-prefix: body-prefix:
description: "Backporting PR body prefix. Default is `backport: <original-pr-link>`" description: "Backporting PR body prefix. Default is `Backport: <original-pr-link>`"
required: false
body:
description: "Backporting PR body. Default is the original PR body"
required: false required: false
bp-branch-name: bp-branch-name:
description: "Backporting PR branch name. Default is auto-generated from commit" description: "Backporting PR branch name. Default is auto-generated from commit"

7
dist/cli/index.js vendored
View file

@ -340,7 +340,7 @@ class PullRequestConfigsParser extends configs_parser_1.default {
} }
} }
const bodyPrefix = args.bodyPrefix ?? `**Backport:** ${originalPullRequest.htmlUrl}\r\n\r\n`; const bodyPrefix = args.bodyPrefix ?? `**Backport:** ${originalPullRequest.htmlUrl}\r\n\r\n`;
const body = args.body ?? `${originalPullRequest.body}`; const body = bodyPrefix + (args.body ?? `${originalPullRequest.body}`);
const labels = args.labels ?? []; const labels = args.labels ?? [];
if (args.inheritLabels) { if (args.inheritLabels) {
labels.push(...originalPullRequest.labels); labels.push(...originalPullRequest.labels);
@ -361,11 +361,12 @@ class PullRequestConfigsParser extends configs_parser_1.default {
head: backportBranch, head: backportBranch,
base: args.targetBranch, base: args.targetBranch,
title: args.title ?? `[${args.targetBranch}] ${originalPullRequest.title}`, title: args.title ?? `[${args.targetBranch}] ${originalPullRequest.title}`,
body: `${bodyPrefix}${body}`, // preserve new line chars
body: body.replace(/\\n/g, "\n").replace(/\\r/g, "\r"),
reviewers: [...new Set(reviewers)], reviewers: [...new Set(reviewers)],
assignees: [...new Set(args.assignees)], assignees: [...new Set(args.assignees)],
labels: [...new Set(labels)], labels: [...new Set(labels)],
comments: args.comments ?? [], comments: args.comments?.map(c => c.replace(/\\n/g, "\n").replace(/\\r/g, "\r")) ?? [],
}; };
} }
} }

7
dist/gha/index.js vendored
View file

@ -310,7 +310,7 @@ class PullRequestConfigsParser extends configs_parser_1.default {
} }
} }
const bodyPrefix = args.bodyPrefix ?? `**Backport:** ${originalPullRequest.htmlUrl}\r\n\r\n`; const bodyPrefix = args.bodyPrefix ?? `**Backport:** ${originalPullRequest.htmlUrl}\r\n\r\n`;
const body = args.body ?? `${originalPullRequest.body}`; const body = bodyPrefix + (args.body ?? `${originalPullRequest.body}`);
const labels = args.labels ?? []; const labels = args.labels ?? [];
if (args.inheritLabels) { if (args.inheritLabels) {
labels.push(...originalPullRequest.labels); labels.push(...originalPullRequest.labels);
@ -331,11 +331,12 @@ class PullRequestConfigsParser extends configs_parser_1.default {
head: backportBranch, head: backportBranch,
base: args.targetBranch, base: args.targetBranch,
title: args.title ?? `[${args.targetBranch}] ${originalPullRequest.title}`, title: args.title ?? `[${args.targetBranch}] ${originalPullRequest.title}`,
body: `${bodyPrefix}${body}`, // preserve new line chars
body: body.replace(/\\n/g, "\n").replace(/\\r/g, "\r"),
reviewers: [...new Set(reviewers)], reviewers: [...new Set(reviewers)],
assignees: [...new Set(args.assignees)], assignees: [...new Set(args.assignees)],
labels: [...new Set(labels)], labels: [...new Set(labels)],
comments: args.comments ?? [], comments: args.comments?.map(c => c.replace(/\\n/g, "\n").replace(/\\r/g, "\r")) ?? [],
}; };
} }
} }

View file

@ -63,7 +63,7 @@ export default class PullRequestConfigsParser extends ConfigsParser {
} }
const bodyPrefix = args.bodyPrefix ?? `**Backport:** ${originalPullRequest.htmlUrl}\r\n\r\n`; const bodyPrefix = args.bodyPrefix ?? `**Backport:** ${originalPullRequest.htmlUrl}\r\n\r\n`;
const body = args.body ?? `${originalPullRequest.body}`; const body = bodyPrefix + (args.body ?? `${originalPullRequest.body}`);
const labels = args.labels ?? []; const labels = args.labels ?? [];
if (args.inheritLabels) { if (args.inheritLabels) {
@ -88,11 +88,12 @@ export default class PullRequestConfigsParser extends ConfigsParser {
head: backportBranch, head: backportBranch,
base: args.targetBranch, base: args.targetBranch,
title: args.title ?? `[${args.targetBranch}] ${originalPullRequest.title}`, title: args.title ?? `[${args.targetBranch}] ${originalPullRequest.title}`,
body: `${bodyPrefix}${body}`, // preserve new line chars
body: body.replace(/\\n/g, "\n").replace(/\\r/g, "\r"),
reviewers: [...new Set(reviewers)], reviewers: [...new Set(reviewers)],
assignees: [...new Set(args.assignees)], assignees: [...new Set(args.assignees)],
labels: [...new Set(labels)], labels: [...new Set(labels)],
comments: args.comments ?? [], comments: args.comments?.map(c => c.replace(/\\n/g, "\n").replace(/\\r/g, "\r")) ?? [],
}; };
} }
} }

View file

@ -229,7 +229,7 @@ describe("cli runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"), body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -274,7 +274,7 @@ describe("cli runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/8632"), body: "**Backport:** https://github.com/owner/reponame/pull/8632\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -331,7 +331,7 @@ describe("cli runner", () => {
head: "bp-target-9174896", head: "bp-target-9174896",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/4444"), body: "**Backport:** https://github.com/owner/reponame/pull/4444\r\n\r\nPlease review and merge",
reviewers: ["gh-user"], reviewers: ["gh-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -351,7 +351,7 @@ describe("cli runner", () => {
"--body", "--body",
"New Body", "New Body",
"--body-prefix", "--body-prefix",
"New Body Prefix - ", "New Body Prefix\\r\\n\\r\\n",
"--bp-branch-name", "--bp-branch-name",
"bp_branch_name", "bp_branch_name",
"--reviewers", "--reviewers",
@ -389,7 +389,7 @@ describe("cli runner", () => {
head: "bp_branch_name", head: "bp_branch_name",
base: "target", base: "target",
title: "New Title", title: "New Title",
body: "New Body Prefix - New Body", body: "New Body Prefix\r\n\r\nNew Body",
reviewers: ["user1", "user2"], reviewers: ["user1", "user2"],
assignees: ["user3", "user4"], assignees: ["user3", "user4"],
labels: [], labels: [],
@ -495,7 +495,7 @@ describe("cli runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"), body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: ["cherry-pick :cherries:", "original-label"], labels: ["cherry-pick :cherries:", "original-label"],
@ -543,7 +543,7 @@ describe("cli runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"), body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: ["first-label", "second-label"], labels: ["first-label", "second-label"],
@ -634,7 +634,7 @@ describe("cli runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"), body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -681,7 +681,7 @@ describe("cli runner", () => {
head: "bp-target-0404fb9-11da4e3", head: "bp-target-0404fb9-11da4e3",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/8632"), body: "**Backport:** https://github.com/owner/reponame/pull/8632\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -734,7 +734,7 @@ describe("cli runner", () => {
head: truncatedBranch, head: truncatedBranch,
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"), body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -785,7 +785,7 @@ describe("cli runner", () => {
head: "bp-target-0404fb9-11da4e3", head: "bp-target-0404fb9-11da4e3",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/8632"), body: "**Backport:** https://github.com/owner/reponame/pull/8632\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -801,7 +801,9 @@ describe("cli runner", () => {
"-pr", "-pr",
"https://github.com/owner/reponame/pull/8632", "https://github.com/owner/reponame/pull/8632",
"--comments", "--comments",
"first comment; second comment" "first comment; second comment",
"--body",
"New body"
]); ]);
await runner.execute(); await runner.execute();
@ -832,7 +834,7 @@ describe("cli runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/8632"), body: "**Backport:** https://github.com/owner/reponame/pull/8632\r\n\r\nNew body",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],

View file

@ -121,7 +121,7 @@ describe("gha runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"), body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -174,7 +174,7 @@ describe("gha runner", () => {
head: "bp-target-9174896", head: "bp-target-9174896",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/4444"), body: "**Backport:** https://github.com/owner/reponame/pull/4444\r\n\r\nPlease review and merge",
reviewers: ["gh-user"], reviewers: ["gh-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -189,7 +189,7 @@ describe("gha runner", () => {
"pull-request": "https://github.com/owner/reponame/pull/2368", "pull-request": "https://github.com/owner/reponame/pull/2368",
"title": "New Title", "title": "New Title",
"body": "New Body", "body": "New Body",
"body-prefix": "New Body Prefix - ", "body-prefix": "New Body Prefix\\r\\n\\r\\n",
"bp-branch-name": "bp_branch_name", "bp-branch-name": "bp_branch_name",
"reviewers": "user1, user2", "reviewers": "user1, user2",
"assignees": "user3, user4", "assignees": "user3, user4",
@ -224,7 +224,7 @@ describe("gha runner", () => {
head: "bp_branch_name", head: "bp_branch_name",
base: "target", base: "target",
title: "New Title", title: "New Title",
body: "New Body Prefix - New Body", body: "New Body Prefix\r\n\r\nNew Body",
reviewers: ["user1", "user2"], reviewers: ["user1", "user2"],
assignees: ["user3", "user4"], assignees: ["user3", "user4"],
labels: [], labels: [],
@ -321,7 +321,7 @@ describe("gha runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"), body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: ["cherry-pick :cherries:", "another-label", "original-label"], labels: ["cherry-pick :cherries:", "another-label", "original-label"],
@ -367,7 +367,7 @@ describe("gha runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"), body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: ["cherry-pick :cherries:", "another-label"], labels: ["cherry-pick :cherries:", "another-label"],
@ -455,7 +455,7 @@ describe("gha runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"), body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -500,7 +500,7 @@ describe("gha runner", () => {
head: "bp-target-0404fb9-11da4e3", head: "bp-target-0404fb9-11da4e3",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/8632"), body: "**Backport:** https://github.com/owner/reponame/pull/8632\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -546,7 +546,7 @@ describe("gha runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"), body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],
@ -591,7 +591,7 @@ describe("gha runner", () => {
head: "bp-target-28f63db", head: "bp-target-28f63db",
base: "target", base: "target",
title: "[target] PR Title", title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"), body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"], reviewers: ["gh-user", "that-s-a-user"],
assignees: [], assignees: [],
labels: [], labels: [],