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

Use target_branches to select target branches #340

Merged
merged 11 commits into from
May 29, 2023
Merged

Conversation

korthout
Copy link
Owner

@korthout korthout commented May 8, 2023

Until now, this action could only backport pull requests by selecting the target branches using labels. This pull request introduces a new input target_branches that can be used instead of the labels (or in addition to).

For example, in your repo you want every pull request targeting a release/* branch to be forward-ported to your default branch main. With this pull request, this is now possible:

name: Forward port to main
on:
  pull_request_target:
    types: [closed]
    branches:
      - 'release/*'
jobs:
  backport:
    name: Backport
    runs-on: ubuntu-latest
    # Don't run on closed unmerged pull requests
    if: github.event.pull_request.merged
    steps:
      - uses: actions/checkout@v3
      - uses: korthout/backport-action@292-target-branches-input
        with:
          label_pattern: '' # disable label matching
          target_branches: main

resolves #292

korthout added 3 commits May 7, 2023 10:01
As we continue to expand the number of available inputs, it makes sense
to order the inputs alphabetically. This allows users to more easily
find the documentation for the input they're looking for.

To help out with top-to-bottom readabability, we can use links in the
descriptions. For example, in the how it works section we can link
directly to the input's heading.
This note contained incorrect information.

It is true that a workflow that runs in the `pull_request` context for a
pull request from a forked repository does not have write access. It is
also true that a PAT can be used to specify permissions. But, a PAT
should be stored as a secret and secrets are not available to workflows
running in the `pull_request` context for pull requests from a forked
repository. So the proposed solution cannot work.

We can simply remove the proposed alternative, since it is fine to run
this workflow in the `pull_request_target`.
If we want to add an input that specifies target branches, then the
structure of the code needs to be slightly different. Previously, the
code would loop over each label and determine if a backport should
happen for that label. Now, we want to loop over all target branches.

This means that we have to find the target branches first, and then loop
over them.

This refactoring extracts this to a separate function, so it can be
easily called and tested.

It also determines the target branches before fetching all the commits
from the pull request. This allows us to remove the check whether any of
the labels matches the label pattern, because this allowed us to avoid
unnecessarily fetching the commits if there were no targets found. We
can now simply stop when there are no target branches found.
@korthout korthout force-pushed the 292-target-branches-input branch 2 times, most recently from d3e6e2b to e271ac1 Compare May 29, 2023 18:10
korthout added 7 commits May 29, 2023 20:13
New input that allows specifying the target branches explicitly.
The method to find target branches required large objects as inputs, and
it is part of the large Backport class. We can extract the core parts
into a separate function and test that function.
This allows users to disable label matching explicitly by using an empty
string as label_pattern. This is useful when users want to switch
entirely to the target_branches input.
In order to only use the `target_branches` input, it can be useful to
completely disable the `label_pattern` input.

This adds the ability to completely disable determining the target
branches based on labels. Simply set the `label_pattern` input to an
empty string `''`.
Originally, I went for comma-separated values in the target_branches
input. However, commas are a valid char in git branchnames.

I think it makes more sense to delimit the values on spaces. Spaces are
not allowed in branchnames.
@korthout korthout force-pushed the 292-target-branches-input branch from e271ac1 to 9b43050 Compare May 29, 2023 18:14
@korthout korthout marked this pull request as ready for review May 29, 2023 18:24
@korthout
Copy link
Owner Author

korthout commented May 29, 2023

✅ Tested successfully here, using this workflow with 254871b.

@korthout korthout merged commit e319d32 into main May 29, 2023
@korthout korthout deleted the 292-target-branches-input branch May 29, 2023 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically backport a closed PR to specific branches
1 participant