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

Exclude headref from selected branches #344

Merged
merged 4 commits into from
Jun 8, 2023

Conversation

korthout
Copy link
Owner

@korthout korthout commented Jun 8, 2023

This pull request enables users to select target branches more leniently.

When the action is triggered for a pull request, its headref should not be selected as target branch because it already contains the commits. There is no use case to cherry-pick the changes to that same branch again. However, it was possible to select the headref both through labels matching the labels_pattern input, as well as through the target_branches input. This pull request excludes the headref from any of the selected target branches automatically.

One example where this could be useful was highlighted in #343. While glob patterns are not supported in the target_branches input yet, it is possible to expand a glob pattern in a separate step, like:

- id: branches
  shell: bash
  # list all banches, filtering for 'feature/*', and cutting off remotes/origin/
  # then finally substitute newlines for spaces
  run: |
    branches=$(git branch --list --all | grep 'origin/feature/' | cut -c 18- )
    space_delimited=${branches//$'\n'/ }
    echo "BRANCHES=${space_delimited}" >> $GITHUB_OUTPUT

These branches can then be used as target_branches input. However, if the headref is among the selected branches, the action would unsuccessfully attempt to cherry-pick the changes of the pull request onto that same pull request's head. Git would rightfully complain about this. Now, that no longer occurs.

closes #343

korthout added 4 commits June 8, 2023 14:37
This is helpful to users to better understand which target branches have
been determined to backport to.
If we want to exclude the headref from the possible target branches, we
need to provide it as a parameter to the findTargetBranches function.

I'm not so happy that this affects all previously written unit tests for
this function, but it is acceptable because headref has now become
important for this function.
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.
The headref is excluded automatically. Since this is an explicit
difference in behavior, it should be clearly documented to avoid
surprises for users.

As far as I know, there is no use case to backport a pull request to
its headref. The cherry-pick would be interrupted because there are no
changes which would result in empty commits. Typically, the git user
get's to decide to skip these commits, or to abort the cherry-pick. This
means that would not be any new commits, and we cannot open an empty
pull request. So all of this doesn't work. The only thing left to do, is
exclude the headref from the determined target branches.
@korthout korthout added the backport release-v1.3 Pull requests that should be backported to v1.3 label Jun 8, 2023
@korthout korthout merged commit ad9f947 into main Jun 8, 2023
@korthout korthout deleted the 343-exclude-headref-from-target-branches branch June 8, 2023 13:29
@korthout
Copy link
Owner Author

korthout commented Jun 8, 2023

Backport action workflow was still disabled for #312, but that issue has been resolved already. I've re-enabled the workflow now.

/backport

@github-actions
Copy link
Contributor

github-actions bot commented Jun 8, 2023

Successfully created backport PR for release-v1.3:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport release-v1.3 Pull requests that should be backported to v1.3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request : backport to all open feature branches
1 participant