name: Backport v2
on:
  pull_request:
    types: [closed, labeled] # runs when the pull request is closed/merged or labeled (to trigger a backport in hindsight)
permissions:
  contents: write # so it can comment
  pull-requests: write # so it can create pull requests
  actions: write
jobs:
  backport:
    name: Backport
    runs-on: ubuntu-latest
    if: github.event.pull_request.merged
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Create backport pull requests
        uses: korthout/backport-action@66065406958f46e82238fd59546f5a99e69e22aa # v4.5.2
        id: backport
        with:
          github_token: ${{ secrets.PAT }}
          pull_title: '[backport -> ${target_branch}] ${pull_title}'
          merge_commits: 'skip'
          copy_labels_pattern: ^(?!backport ).* # copies all labels except those starting with "backport "
          label_pattern: ^backport (release\/[^ ]+)$ # filters for labels starting with "backport " and extracts the branch name
          pull_description: |-
            Automated backport to `${target_branch}`, triggered by a label in #${pull_number}.

            ## Original description

            ${pull_description}
          copy_assignees: true
          copy_milestone: true
          copy_requested_reviewers: true
          experimental: >
            {
              "detect_merge_method": true
            }
      - name: add label
        if: steps.backport.outputs.was_successful == 'false'
        uses: Kong/action-add-labels@e9beb8480f1c0e4e1292df967adec60e388755fc
        with:
          labels: incomplete-backport
