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

[Backport release-v1.3] Exclude headref from selected branches #345

Merged
merged 4 commits into from
Jun 8, 2023
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Must contain a capture group for the target branch.
Label matching can be disabled entirely using an empty string `''` as pattern.

The action will backport the pull request to each matched target branch.
Note that the pull request's headref is excluded automatically.
See [How it works](#how-it-works).

### `pull_description`
Expand Down Expand Up @@ -168,6 +169,7 @@ Please refer to this action's README for all available [placeholders](#placehold
Default: `''` (disabled)

The action will backport the pull request to each specified target branch (space-delimited).
Note that the pull request's headref is excluded automatically.
See [How it works](#how-it-works).

Can be used in addition to backport labels.
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ inputs:
Regex pattern to match the backport labels on the merged pull request.
Must contain a capture group for the target branch.
The action will backport the pull request to each matched target branch.
Note that the pull request's headref is excluded automatically.
default: ^backport ([^ ]+)$
pull_description:
description: >
Expand All @@ -43,6 +44,7 @@ inputs:
target_branches:
description: >
The action will backport the pull request to each specified target branch (space-delimited).
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.
outputs:
Expand Down
16 changes: 12 additions & 4 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class Backport {

private findTargetBranches(mainpr: PullRequest, config: Config): string[] {
const labels = mainpr.labels.map((label) => label.name);
return findTargetBranches(config, labels);
return findTargetBranches(config, labels, mainpr.head.ref);
}

private composePRContent(target: string, main: PullRequest): PRContent {
Expand Down Expand Up @@ -360,7 +360,8 @@ export class Backport {

export function findTargetBranches(
config: Pick<Config, "labels" | "target_branches">,
labels: string[]
labels: string[],
headref: string
) {
console.log("Determining target branches...");

Expand All @@ -377,10 +378,17 @@ export function findTargetBranches(
console.log(
`Found target branches in \`target_branches\` input: ${configuredTargetBranches}`
);
console.log(
`Exclude pull request's headref from target branches: ${headref}`
);

return [
const targetBranches = [
...new Set([...targetBranchesFromLabels, ...configuredTargetBranches]),
];
].filter((t) => t !== headref);

console.log(`Determined target branches: ${targetBranches}`);

return targetBranches;
}

function findTargetBranchesFromLabels(
Expand Down
1 change: 1 addition & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export type PullRequest = {
body: string | null;
head: {
sha: string;
ref: string;
};
base: {
sha: string;
Expand Down
101 changes: 78 additions & 23 deletions src/test/backport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,70 @@ describe("find target branches", () => {
describe("returns an empty list", () => {
it("when labels is an empty list", () => {
expect(
findTargetBranches({ labels: { pattern: default_pattern } }, [])
findTargetBranches(
{ labels: { pattern: default_pattern } },
[],
"feature/one"
)
).toEqual([]);
});

it("when none of the labels match the pattern", () => {
expect(
findTargetBranches({ labels: { pattern: default_pattern } }, [
"a label",
"another-label",
"a/third/label",
])
findTargetBranches(
{ labels: { pattern: default_pattern } },
["a label", "another-label", "a/third/label"],
"feature/one"
)
).toEqual([]);
});

it("when a label matches the pattern but doesn't capture a target branch", () => {
expect(
findTargetBranches({ labels: { pattern: /^no capture group$/ } }, [
"no capture group",
])
findTargetBranches(
{ labels: { pattern: /^no capture group$/ } },
["no capture group"],
"feature/one"
)
).toEqual([]);
});

it("when the label pattern is an empty string", () => {
expect(
findTargetBranches({ labels: { pattern: undefined } }, [
"an empty string",
])
findTargetBranches(
{ labels: { pattern: undefined } },
["an empty string"],
"feature/one"
)
).toEqual([]);
});

it("when target_branches is an empty string", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern }, target_branches: "" },
["a label"]
["a label"],
"feature/one"
)
).toEqual([]);
});

it("when the label pattern only matches the headref", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
["backport feature/one"],
"feature/one"
)
).toEqual([]);
});

it("when target_branches only contains the headref", () => {
expect(
findTargetBranches(
{ labels: {}, target_branches: "feature/one" },
[],
"feature/one"
)
).toEqual([]);
});
Expand All @@ -53,18 +82,21 @@ describe("find target branches", () => {
describe("returns selected branches", () => {
it("when a label matches the pattern and captures a target branch", () => {
expect(
findTargetBranches({ labels: { pattern: default_pattern } }, [
"backport release-1",
])
findTargetBranches(
{ labels: { pattern: default_pattern } },
["backport release-1"],
"feature/one"
)
).toEqual(["release-1"]);
});

it("when several labels match the pattern and capture a target branch", () => {
expect(
findTargetBranches({ labels: { pattern: default_pattern } }, [
"backport release-1",
"backport another/target/branch",
])
findTargetBranches(
{ labels: { pattern: default_pattern } },
["backport release-1", "backport another/target/branch"],
"feature/one"
)
).toEqual(["release-1", "another/target/branch"]);
});

Expand All @@ -75,7 +107,8 @@ describe("find target branches", () => {
labels: { pattern: default_pattern },
target_branches: "release-1",
},
[]
[],
"feature/one"
)
).toEqual(["release-1"]);
});
Expand All @@ -87,7 +120,8 @@ describe("find target branches", () => {
labels: { pattern: default_pattern },
target_branches: "release-1 another/target/branch",
},
[]
[],
"feature/one"
)
).toEqual(["release-1", "another/target/branch"]);
});
Expand All @@ -99,9 +133,30 @@ describe("find target branches", () => {
labels: { pattern: default_pattern },
target_branches: "release-1",
},
["backport release-1"]
["backport release-1"],
"feature/one"
)
).toEqual(["release-1"]);
});

it("when several labels match the pattern the headref is excluded", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
["backport feature/one", "backport feature/two"],
"feature/one"
)
).toEqual(["feature/two"]);
});

it("when several target branches are specified the headref is excluded", () => {
expect(
findTargetBranches(
{ labels: {}, target_branches: "feature/one feature/two" },
[],
"feature/one"
)
).toEqual(["feature/two"]);
});
});
});