From 247f32e6ef00860f5276a8b77195d1a392bb3c34 Mon Sep 17 00:00:00 2001 From: tobigr Date: Sun, 28 Dec 2025 12:12:34 +0100 Subject: [PATCH 1/2] Add workflow to backport PRs to another branch The workflow can be triggered by creating a comment on a closed PR: /backport The backport can only be triggered by people with write access to the repository. --- .github/workflows/backport-pr.yml | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 .github/workflows/backport-pr.yml diff --git a/.github/workflows/backport-pr.yml b/.github/workflows/backport-pr.yml new file mode 100644 index 00000000000..e5955ea9c3f --- /dev/null +++ b/.github/workflows/backport-pr.yml @@ -0,0 +1,50 @@ +name: Backport merged pull request +on: + issue_comment: + types: [created] +permissions: + contents: write # for comment creation on original PR + pull-requests: write +jobs: + backport: + name: Backport pull request + runs-on: ubuntu-latest + + # Only run when the comment starts with the `/backport` command on a PR and + # the commenter has write access to the repository. We do not want to allow + # everybody to trigger backports and create branches in our repository. + if: > + github.event.issue.pull_request && + startsWith(github.event.comment.body, '/backport') && + ( + github.event.comment.author_association == 'OWNER' || + github.event.comment.author_association == 'COLLABORATOR' || + github.event.comment.author_association == 'MEMBER' + ) + steps: + - uses: actions/checkout@v4 + - name: Get backport metadata + # the target branch is the first argument after `/backport` + run: | + set -euo pipefail + body="${{ github.event.comment.body }}" + labels_json='${{ toJSON(github.event.pull_request.labels || github.event.issue.labels) }}' + target=$(echo "$body" | sed -n 's/.*\/backport[[:space:]]\+\([^[:space:]]\+\).*/\1/p') + + if [ -z "$target" ]; then + echo "No backport target found in comment." >&2 + exit 1 + fi + echo "BACKPORT_TARGET=$target" >> $GITHUB_ENV + + labels=$(echo "$labels_json" | jq -r 'if . == null then "" else map(.name) | join(",") end') + if [ -z "$labels" ]; then + echo "BACKPORT_PR_LABELS=backport" >> $GITHUB_ENV + else + echo "BACKPORT_PR_LABELS=backport,$labels" >> $GITHUB_ENV + fi + - name: Create backport pull request + uses: korthout/backport-action@v4 + with: + add_labels: ${{ env.BACKPORT_PR_LABELS }} + target_branches: ${{ env.BACKPORT_TARGET }} \ No newline at end of file From 05a2e5194a58c8e0beb71de7b485dd95a2d1ef47 Mon Sep 17 00:00:00 2001 From: tobigr Date: Wed, 7 Jan 2026 12:41:58 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Use proper arguments to apply labels from original PR to backport PR. Improve target branch detection. Co-authored-by: AbsurdlyLongUsername <22662897+absurdlylongusername@users.noreply.github.com> --- .github/workflows/backport-pr.yml | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/.github/workflows/backport-pr.yml b/.github/workflows/backport-pr.yml index e5955ea9c3f..c7bcb117e91 100644 --- a/.github/workflows/backport-pr.yml +++ b/.github/workflows/backport-pr.yml @@ -15,7 +15,7 @@ jobs: # everybody to trigger backports and create branches in our repository. if: > github.event.issue.pull_request && - startsWith(github.event.comment.body, '/backport') && + startsWith(github.event.comment.body, '/backport ') && ( github.event.comment.author_association == 'OWNER' || github.event.comment.author_association == 'COLLABORATOR' || @@ -28,23 +28,19 @@ jobs: run: | set -euo pipefail body="${{ github.event.comment.body }}" - labels_json='${{ toJSON(github.event.pull_request.labels || github.event.issue.labels) }}' - target=$(echo "$body" | sed -n 's/.*\/backport[[:space:]]\+\([^[:space:]]\+\).*/\1/p') - - if [ -z "$target" ]; then - echo "No backport target found in comment." >&2 + + line=${body%%$'\n'*} # Get the first line + if [[ $line =~ ^/backport[[:space:]]+([^[:space:]]+) ]]; then + echo "BACKPORT_TARGET=${BASH_REMATCH[1]}" >> "$GITHUB_ENV" + else + echo "Usage: /backport " >&2 exit 1 fi - echo "BACKPORT_TARGET=$target" >> $GITHUB_ENV - labels=$(echo "$labels_json" | jq -r 'if . == null then "" else map(.name) | join(",") end') - if [ -z "$labels" ]; then - echo "BACKPORT_PR_LABELS=backport" >> $GITHUB_ENV - else - echo "BACKPORT_PR_LABELS=backport,$labels" >> $GITHUB_ENV - fi - name: Create backport pull request uses: korthout/backport-action@v4 with: - add_labels: ${{ env.BACKPORT_PR_LABELS }} + add_labels: 'backport' + copy_labels_pattern: '.*' + label_pattern: '' target_branches: ${{ env.BACKPORT_TARGET }} \ No newline at end of file