Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: preserve new lines in body and comments
Browse files Browse the repository at this point in the history
lampajr committed Jul 27, 2023

Verified

This commit was signed with the committer’s verified signature.
folkertdev Folkert de Vries
1 parent a402fa4 commit 461a2e4
Showing 7 changed files with 44 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -13,4 +13,5 @@ build/
.npmrc

# temporary files created during tests
*test*.json
*test*.json
bp
8 changes: 4 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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: <original-pr-link>`"
required: false
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
bp-branch-name:
description: "Backporting PR branch name. Default is auto-generated from commit"
7 changes: 4 additions & 3 deletions dist/cli/index.js
Original file line number Diff line number Diff line change
@@ -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")) ?? [],
};
}
}
7 changes: 4 additions & 3 deletions dist/gha/index.js
Original file line number Diff line number Diff line change
@@ -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")) ?? [],
};
}
}
9 changes: 5 additions & 4 deletions src/service/configs/pullrequest/pr-configs-parser.ts
Original file line number Diff line number Diff line change
@@ -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")) ?? [],
};
}
}
28 changes: 15 additions & 13 deletions test/service/runner/cli-github-runner.test.ts
Original file line number Diff line number Diff line change
@@ -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: [],
20 changes: 10 additions & 10 deletions test/service/runner/gha-github-runner.test.ts
Original file line number Diff line number Diff line change
@@ -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: [],

0 comments on commit 461a2e4

Please sign in to comment.