Skip to content

Commit

Permalink
feat(backport): exclude headref from found target branches
Browse files Browse the repository at this point in the history
This case is relevant where the target_branches input is used to select
the target branches, and the original pull request's headref is part of
it.

For example, consider selecting all feature branches `feature/*` as
target_branches. Glob patterns are not supported yet, but users can find
these with an additional step in their workflow:

```
run: |
  branches=$(git branch --list --all | grep 'origin/feature/' | cut -c 18- )
  space_delimited=${branches//$'\n'/ }
  echo "BRANCHES=${space_delimited}" >> $GITHUB_OUTPUT
```

Note that such a step requires a deep checkout `depth=0`.

But when users do this, they might encounter the feature branch they
just merged, the merged branch that triggered the backport-action.

By excluding it now, we can better support this case. In addition, it
will help support glob patterns in the target_branches input in the
future.

(cherry picked from commit 151736d)
  • Loading branch information
korthout authored and github-actions[bot] committed Jun 8, 2023
1 parent 665ab08 commit 43df180
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

Expand Down
40 changes: 40 additions & 0 deletions src/test/backport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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"]);
});
});
});

0 comments on commit 43df180

Please sign in to comment.