diff --git a/src/backport.ts b/src/backport.ts index 8a6fee9..e65928d 100644 --- a/src/backport.ts +++ b/src/backport.ts @@ -378,10 +378,13 @@ export function findTargetBranches( console.log( `Found target branches in \`target_branches\` input: ${configuredTargetBranches}` ); + console.log( + `Exclude pull request's headref from target branches: ${headref}` + ); const targetBranches = [ ...new Set([...targetBranchesFromLabels, ...configuredTargetBranches]), - ]; + ].filter((t) => t !== headref); console.log(`Determined target branches: ${targetBranches}`); diff --git a/src/test/backport.test.ts b/src/test/backport.test.ts index f2ee36a..e39f266 100644 --- a/src/test/backport.test.ts +++ b/src/test/backport.test.ts @@ -57,6 +57,26 @@ describe("find target branches", () => { ) ).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([]); + }); }); describe("returns selected branches", () => { @@ -118,5 +138,25 @@ describe("find target branches", () => { ) ).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"]); + }); }); });