From fa43ffc1dc5572a06309c28e93ee6ab5fba14780 Mon Sep 17 00:00:00 2001 From: Andrea Lamparelli Date: Thu, 27 Jul 2023 15:35:23 +0200 Subject: [PATCH] fix: preserve new lines in body and comments (#72) --- .gitignore | 3 +- action.yml | 8 +++--- dist/cli/index.js | 7 +++-- dist/gha/index.js | 7 +++-- .../configs/pullrequest/pr-configs-parser.ts | 9 +++--- test/service/runner/cli-github-runner.test.ts | 28 ++++++++++--------- test/service/runner/gha-github-runner.test.ts | 20 ++++++------- 7 files changed, 44 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index f3c41a9..115c77e 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ build/ .npmrc # temporary files created during tests -*test*.json \ No newline at end of file +*test*.json +bp diff --git a/action.yml b/action.yml index 5deca80..cb4fd89 100644 --- a/action.yml +++ b/action.yml @@ -29,11 +29,11 @@ inputs: title: description: "Backporting PR title. Default is the original PR title prefixed by the target branch" required: false - body: - description: "Backporting PR body. Default is the original PR body prefixed by `backport: `" - required: false body-prefix: - description: "Backporting PR body prefix. Default is `backport: `" + description: "Backporting PR body prefix. Default is `Backport: `" + required: false + body: + description: "Backporting PR body. Default is the original PR body" required: false bp-branch-name: description: "Backporting PR branch name. Default is auto-generated from commit" diff --git a/dist/cli/index.js b/dist/cli/index.js index 87e057f..234f055 100755 --- a/dist/cli/index.js +++ b/dist/cli/index.js @@ -340,7 +340,7 @@ class PullRequestConfigsParser extends configs_parser_1.default { } } 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 ?? []; if (args.inheritLabels) { labels.push(...originalPullRequest.labels); @@ -361,11 +361,12 @@ class PullRequestConfigsParser extends configs_parser_1.default { head: backportBranch, base: args.targetBranch, 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)], assignees: [...new Set(args.assignees)], labels: [...new Set(labels)], - comments: args.comments ?? [], + comments: args.comments?.map(c => c.replace(/\\n/g, "\n").replace(/\\r/g, "\r")) ?? [], }; } } diff --git a/dist/gha/index.js b/dist/gha/index.js index a4d41a2..c7464fa 100755 --- a/dist/gha/index.js +++ b/dist/gha/index.js @@ -310,7 +310,7 @@ class PullRequestConfigsParser extends configs_parser_1.default { } } 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 ?? []; if (args.inheritLabels) { labels.push(...originalPullRequest.labels); @@ -331,11 +331,12 @@ class PullRequestConfigsParser extends configs_parser_1.default { head: backportBranch, base: args.targetBranch, 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)], assignees: [...new Set(args.assignees)], labels: [...new Set(labels)], - comments: args.comments ?? [], + comments: args.comments?.map(c => c.replace(/\\n/g, "\n").replace(/\\r/g, "\r")) ?? [], }; } } diff --git a/src/service/configs/pullrequest/pr-configs-parser.ts b/src/service/configs/pullrequest/pr-configs-parser.ts index 3cc81ce..39d39e7 100644 --- a/src/service/configs/pullrequest/pr-configs-parser.ts +++ b/src/service/configs/pullrequest/pr-configs-parser.ts @@ -63,7 +63,7 @@ export default class PullRequestConfigsParser extends ConfigsParser { } 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 ?? []; if (args.inheritLabels) { @@ -87,12 +87,13 @@ export default class PullRequestConfigsParser extends ConfigsParser { repo: originalPullRequest.targetRepo.project, head: backportBranch, base: args.targetBranch, - title: args.title ?? `[${args.targetBranch}] ${originalPullRequest.title}`, - body: `${bodyPrefix}${body}`, + title: args.title ?? `[${args.targetBranch}] ${originalPullRequest.title}`, + // preserve new line chars + body: body.replace(/\\n/g, "\n").replace(/\\r/g, "\r"), reviewers: [...new Set(reviewers)], assignees: [...new Set(args.assignees)], labels: [...new Set(labels)], - comments: args.comments ?? [], + comments: args.comments?.map(c => c.replace(/\\n/g, "\n").replace(/\\r/g, "\r")) ?? [], }; } } \ No newline at end of file diff --git a/test/service/runner/cli-github-runner.test.ts b/test/service/runner/cli-github-runner.test.ts index 27c78aa..f5ebe36 100644 --- a/test/service/runner/cli-github-runner.test.ts +++ b/test/service/runner/cli-github-runner.test.ts @@ -229,7 +229,7 @@ describe("cli runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: [], @@ -274,7 +274,7 @@ describe("cli runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: [], @@ -331,7 +331,7 @@ describe("cli runner", () => { head: "bp-target-9174896", base: "target", 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"], assignees: [], labels: [], @@ -351,7 +351,7 @@ describe("cli runner", () => { "--body", "New Body", "--body-prefix", - "New Body Prefix - ", + "New Body Prefix\\r\\n\\r\\n", "--bp-branch-name", "bp_branch_name", "--reviewers", @@ -389,7 +389,7 @@ describe("cli runner", () => { head: "bp_branch_name", base: "target", title: "New Title", - body: "New Body Prefix - New Body", + body: "New Body Prefix\r\n\r\nNew Body", reviewers: ["user1", "user2"], assignees: ["user3", "user4"], labels: [], @@ -495,7 +495,7 @@ describe("cli runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: ["cherry-pick :cherries:", "original-label"], @@ -543,7 +543,7 @@ describe("cli runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: ["first-label", "second-label"], @@ -634,7 +634,7 @@ describe("cli runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: [], @@ -681,7 +681,7 @@ describe("cli runner", () => { head: "bp-target-0404fb9-11da4e3", base: "target", 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"], assignees: [], labels: [], @@ -734,7 +734,7 @@ describe("cli runner", () => { head: truncatedBranch, base: "target", 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"], assignees: [], labels: [], @@ -785,7 +785,7 @@ describe("cli runner", () => { head: "bp-target-0404fb9-11da4e3", base: "target", 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"], assignees: [], labels: [], @@ -801,7 +801,9 @@ describe("cli runner", () => { "-pr", "https://github.com/owner/reponame/pull/8632", "--comments", - "first comment; second comment" + "first comment; second comment", + "--body", + "New body" ]); await runner.execute(); @@ -832,7 +834,7 @@ describe("cli runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: [], diff --git a/test/service/runner/gha-github-runner.test.ts b/test/service/runner/gha-github-runner.test.ts index 44e11a3..44f098b 100644 --- a/test/service/runner/gha-github-runner.test.ts +++ b/test/service/runner/gha-github-runner.test.ts @@ -121,7 +121,7 @@ describe("gha runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: [], @@ -174,7 +174,7 @@ describe("gha runner", () => { head: "bp-target-9174896", base: "target", 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"], assignees: [], labels: [], @@ -189,7 +189,7 @@ describe("gha runner", () => { "pull-request": "https://github.com/owner/reponame/pull/2368", "title": "New Title", "body": "New Body", - "body-prefix": "New Body Prefix - ", + "body-prefix": "New Body Prefix\\r\\n\\r\\n", "bp-branch-name": "bp_branch_name", "reviewers": "user1, user2", "assignees": "user3, user4", @@ -224,7 +224,7 @@ describe("gha runner", () => { head: "bp_branch_name", base: "target", title: "New Title", - body: "New Body Prefix - New Body", + body: "New Body Prefix\r\n\r\nNew Body", reviewers: ["user1", "user2"], assignees: ["user3", "user4"], labels: [], @@ -321,7 +321,7 @@ describe("gha runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: ["cherry-pick :cherries:", "another-label", "original-label"], @@ -367,7 +367,7 @@ describe("gha runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: ["cherry-pick :cherries:", "another-label"], @@ -455,7 +455,7 @@ describe("gha runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: [], @@ -500,7 +500,7 @@ describe("gha runner", () => { head: "bp-target-0404fb9-11da4e3", base: "target", 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"], assignees: [], labels: [], @@ -546,7 +546,7 @@ describe("gha runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: [], @@ -591,7 +591,7 @@ describe("gha runner", () => { head: "bp-target-28f63db", base: "target", 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"], assignees: [], labels: [],