Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for statically setting labels on backport PRs #432

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ inputs:
Note that the pull request's headref is excluded automatically.
Can be used in addition to backport labels.
By default, only backport labels are used to specify the target branches.
labels:
description: >
A space separted list of labels to add to the pull request.

outputs:
created_pull_numbers:
Expand Down
20 changes: 12 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Backport {
}
const target_branches = this.findTargetBranches(mainpr, this.config);
if (target_branches.length === 0) {
console.log(`Nothing to backport: no 'target_branches' specified and none of the labels match the backport pattern '${(_c = this.config.labels.pattern) === null || _c === void 0 ? void 0 : _c.source}'`);
console.log(`Nothing to backport: no 'target_branches' specified and none of the labels match the backport pattern '${(_c = this.config.source_labels_pattern) === null || _c === void 0 ? void 0 : _c.source}'`);
return; // nothing left to do here
}
console.log(`Fetching all the commits from the pull request: ${mainpr.commits + 1}`);
Expand Down Expand Up @@ -181,8 +181,8 @@ class Backport {
labelsToCopy = mainpr.labels
.map((label) => label.name)
.filter((label) => label.match(copyLabelsPattern) &&
(this.config.labels.pattern === undefined ||
!label.match(this.config.labels.pattern)));
(this.config.source_labels_pattern === undefined ||
!label.match(this.config.source_labels_pattern)));
}
console.log(`Will copy labels matching ${this.config.copy_labels_pattern}. Found matching labels: ${labelsToCopy}`);
if (this.shouldUseDownstreamRepo()) {
Expand Down Expand Up @@ -324,8 +324,10 @@ class Backport {
}
}
}
if (labelsToCopy.length > 0) {
const label_response = yield this.github.labelPR(new_pr.number, labelsToCopy, {
// Combine the labels to be copied with the static labels and deduplicate them using a Set
const labels = [...new Set([...labelsToCopy, ...this.config.labels])];
if (labels.length > 0) {
const label_response = yield this.github.labelPR(new_pr.number, labels, {
owner,
repo,
});
Expand Down Expand Up @@ -467,7 +469,7 @@ class Backport {
const suggestionToResolve = this.composeMessageToResolveCommittedConflicts(target, branchname, commitShasToCherryPick, conflictResolution);
return (0, dedent_1.default) `Created backport PR for \`${target}\`:
- ${downstream}#${pr_number} with remaining conflicts!

${suggestionToResolve}`;
}
createOutput(successByTarget, createdPullRequestNumbers) {
Expand Down Expand Up @@ -497,7 +499,7 @@ function findTargetBranches(config, labels, headref) {
}
exports.findTargetBranches = findTargetBranches;
function findTargetBranchesFromLabels(labels, config) {
const pattern = config.labels.pattern;
const pattern = config.source_labels_pattern;
if (pattern === undefined) {
return [];
}
Expand Down Expand Up @@ -1070,6 +1072,7 @@ function run() {
const description = core.getInput("pull_description");
const title = core.getInput("pull_title");
const branch_name = core.getInput("branch_name");
const labels = core.getInput("labels");
const copy_labels_pattern = core.getInput("copy_labels_pattern");
const target_branches = core.getInput("target_branches");
const cherry_picking = core.getInput("cherry_picking");
Expand Down Expand Up @@ -1115,9 +1118,10 @@ function run() {
const git = new git_1.Git(execa_1.execa);
const config = {
pwd,
labels: { pattern: pattern === "" ? undefined : new RegExp(pattern) },
source_labels_pattern: pattern === "" ? undefined : new RegExp(pattern),
pull: { description, title, branch_name },
copy_labels_pattern: copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
labels: labels === "" ? [] : labels.split(/[ ]/),
target_branches: target_branches === "" ? undefined : target_branches,
commits: { cherry_picking, merge_commits },
copy_assignees: copy_assignees === "true",
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ type PRContent = {

export type Config = {
pwd: string;
labels: {
pattern?: RegExp;
};
source_labels_pattern?: RegExp;
pull: {
description: string;
title: string;
branch_name: string;
};
copy_labels_pattern?: RegExp;
labels: string[];
target_branches?: string;
commits: {
cherry_picking: "auto" | "pull_request_head";
Expand Down Expand Up @@ -121,7 +120,7 @@ export class Backport {
const target_branches = this.findTargetBranches(mainpr, this.config);
if (target_branches.length === 0) {
console.log(
`Nothing to backport: no 'target_branches' specified and none of the labels match the backport pattern '${this.config.labels.pattern?.source}'`,
`Nothing to backport: no 'target_branches' specified and none of the labels match the backport pattern '${this.config.source_labels_pattern?.source}'`,
);
return; // nothing left to do here
}
Expand Down Expand Up @@ -240,8 +239,8 @@ export class Backport {
.filter(
(label) =>
label.match(copyLabelsPattern) &&
(this.config.labels.pattern === undefined ||
!label.match(this.config.labels.pattern)),
(this.config.source_labels_pattern === undefined ||
!label.match(this.config.source_labels_pattern)),
);
}
console.log(
Expand Down Expand Up @@ -434,10 +433,12 @@ export class Backport {
}
}

if (labelsToCopy.length > 0) {
// Combine the labels to be copied with the static labels and deduplicate them using a Set
const labels = [...new Set([...labelsToCopy, ...this.config.labels])];
if (labels.length > 0) {
const label_response = await this.github.labelPR(
new_pr.number,
labelsToCopy,
labels,
{
owner,
repo,
Expand Down Expand Up @@ -677,7 +678,7 @@ export class Backport {
);
return dedent`Created backport PR for \`${target}\`:
- ${downstream}#${pr_number} with remaining conflicts!

${suggestionToResolve}`;
}

Expand All @@ -702,7 +703,7 @@ export class Backport {
}

export function findTargetBranches(
config: Pick<Config, "labels" | "target_branches">,
config: Pick<Config, "source_labels_pattern" | "target_branches">,
labels: string[],
headref: string,
) {
Expand Down Expand Up @@ -736,9 +737,9 @@ export function findTargetBranches(

function findTargetBranchesFromLabels(
labels: string[],
config: Pick<Config, "labels">,
config: Pick<Config, "source_labels_pattern">,
) {
const pattern = config.labels.pattern;
const pattern = config.source_labels_pattern;
if (pattern === undefined) {
return [];
}
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async function run(): Promise<void> {
const description = core.getInput("pull_description");
const title = core.getInput("pull_title");
const branch_name = core.getInput("branch_name");
const labels = core.getInput("labels");
const copy_labels_pattern = core.getInput("copy_labels_pattern");
const target_branches = core.getInput("target_branches");
const cherry_picking = core.getInput("cherry_picking");
Expand Down Expand Up @@ -75,10 +76,11 @@ async function run(): Promise<void> {
const git = new Git(execa);
const config: Config = {
pwd,
labels: { pattern: pattern === "" ? undefined : new RegExp(pattern) },
source_labels_pattern: pattern === "" ? undefined : new RegExp(pattern),
pull: { description, title, branch_name },
copy_labels_pattern:
copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
labels: labels === "" ? [] : labels.split(/[ ]/),
target_branches: target_branches === "" ? undefined : target_branches,
commits: { cherry_picking, merge_commits },
copy_assignees: copy_assignees === "true",
Expand Down
28 changes: 14 additions & 14 deletions src/test/backport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("find target branches", () => {
it("when labels is an empty list", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
[],
"feature/one",
),
Expand All @@ -17,7 +17,7 @@ describe("find target branches", () => {
it("when none of the labels match the pattern", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
["a label", "another-label", "a/third/label"],
"feature/one",
),
Expand All @@ -27,7 +27,7 @@ describe("find target branches", () => {
it("when a label matches the pattern but doesn't capture a target branch", () => {
expect(
findTargetBranches(
{ labels: { pattern: /^no capture group$/ } },
{ source_labels_pattern: /^no capture group$/ },
["no capture group"],
"feature/one",
),
Expand All @@ -37,7 +37,7 @@ describe("find target branches", () => {
it("when the label pattern is an empty string", () => {
expect(
findTargetBranches(
{ labels: { pattern: undefined } },
{ source_labels_pattern: undefined },
["an empty string"],
"feature/one",
),
Expand All @@ -47,7 +47,7 @@ describe("find target branches", () => {
it("when target_branches is an empty string", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern }, target_branches: "" },
{ source_labels_pattern: default_pattern, target_branches: "" },
["a label"],
"feature/one",
),
Expand All @@ -57,7 +57,7 @@ describe("find target branches", () => {
it("when the label pattern only matches the headref", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
["backport feature/one"],
"feature/one",
),
Expand All @@ -67,7 +67,7 @@ describe("find target branches", () => {
it("when target_branches only contains the headref", () => {
expect(
findTargetBranches(
{ labels: {}, target_branches: "feature/one" },
{ target_branches: "feature/one" },
[],
"feature/one",
),
Expand All @@ -79,7 +79,7 @@ describe("find target branches", () => {
it("when a label matches the pattern and captures a target branch", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
["backport release-1"],
"feature/one",
),
Expand All @@ -89,7 +89,7 @@ describe("find target branches", () => {
it("when several labels match the pattern and capture a target branch", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
["backport release-1", "backport another/target/branch"],
"feature/one",
),
Expand All @@ -100,7 +100,7 @@ describe("find target branches", () => {
expect(
findTargetBranches(
{
labels: { pattern: default_pattern },
source_labels_pattern: default_pattern,
target_branches: "release-1",
},
[],
Expand All @@ -113,7 +113,7 @@ describe("find target branches", () => {
expect(
findTargetBranches(
{
labels: { pattern: default_pattern },
source_labels_pattern: default_pattern,
target_branches: "release-1 another/target/branch",
},
[],
Expand All @@ -126,7 +126,7 @@ describe("find target branches", () => {
expect(
findTargetBranches(
{
labels: { pattern: default_pattern },
source_labels_pattern: default_pattern,
target_branches: "release-1",
},
["backport release-1"],
Expand All @@ -138,7 +138,7 @@ describe("find target branches", () => {
it("when several labels match the pattern the headref is excluded", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
["backport feature/one", "backport feature/two"],
"feature/one",
),
Expand All @@ -148,7 +148,7 @@ describe("find target branches", () => {
it("when several target branches are specified the headref is excluded", () => {
expect(
findTargetBranches(
{ labels: {}, target_branches: "feature/one feature/two" },
{ target_branches: "feature/one feature/two" },
[],
"feature/one",
),
Expand Down