Skip to content

Commit

Permalink
Add workflow for backporting pull requests
Browse files Browse the repository at this point in the history
Fixes #8181

This commit includes the configuration file for an action that:

* When a PR that has a label like "Backport <branch>" is merged,
  it will trigger a backport action
* If the PR commits merge cleanly into the target branch, a new PR will
  be created against it, assigned to the same user as the merged one.
  The usual checks will be run on the new PR
* If the commits don't merge cleanly, a comment will be posted on the
  orginal PR with the manual commands to fix the conflicts, and the PR
  will be labelled with "Backport failed"
* Additionally, Tech Team members can trigger a backport on open or
  already closed PRs adding a comment starting with `/backport` (and
  adding the relevant label)

Some more details:
* The action is run using a token from the @ckanbot account, this needs
  to be stored in the repository secrets (`BACKPORT_ACTION_PAT`)
* There are also two public variables (`TECH_TEAM_USER_IDS` and
  `CKANBOT_USER_ID`) that need to be added to the repository variables.
* The action used source code code can be found at
  https://github.com/korthout/backport-action
  • Loading branch information
amercader committed Apr 17, 2024
1 parent 4da81ee commit 31d87e7
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/backports.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Backport merged pull request
on:
pull_request_target:
types: [closed]
issue_comment:
types: [created]
permissions:
contents: write # so it can comment
pull-requests: write # so it can create pull requests
jobs:
backport:
name: Backport pull request
runs-on: ubuntu-latest
# Only run when a pull request is merged
# or when a comment starting with `/backport` is created by a Tech Team member
if: |-
${{
(
github.event_name == 'pull_request_target' &&
github.event.pull_request.merged
) || (
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
contains(fromJSON(vars.TECH_TEAM_USER_IDS), github.event.comment.user.id) &&
startsWith(github.event.comment.body, '/backport')
)
}}
steps:
- uses: actions/checkout@v4
- name: Create backport pull requests
uses: korthout/backport-action@v2
with:
# Token to authenticate requests to GitHub. This is a Personal Access Token
# from the ckanbot user
github_token: ${{ secrets.BACKPORT_ACTION_PAT }}
# Run when there is one or more "Backport <branch>" labels,
# excluding "Backport pending"
label_pattern: "Backport (?!pending)([^ ]+)$"
merge_commits: skip
copy_assignees: true
pull_description: |-
This is an automated backport pull request 🏗️.
### Details
| | |
| --- | --- |
| Original pull request | #${pull_number} (${pull_title}) |
| Original author | @${pull_author} |
| Target branch | **${target_branch}** |
Please make sure that all relevant checks pass before merging it.
backport-labels:
name: Backport failed labels
runs-on: ubuntu-latest
if: |-
${{
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
github.event.comment.user.id == vars.CKANBOT_USER_ID &&
startsWith(github.event.comment.body, 'Backport failed for')
}}
steps:
- name: Add Backport failed label to PR
uses: actions/github-script@v7
with:
github-token: ${{ secrets.BACKPORT_ACTION_PAT }}
script: |
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ["Backport failed"]
});

0 comments on commit 31d87e7

Please sign in to comment.