name: Backport 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-24.04

    # Only run when pull request is merged
    # or when a comment starting with `/backport` is created by someone other than the
    # https://github.com/backport-action bot user (user id: 97796249). Note that if you use your
    # own PAT as `github_token`, that you should replace this id with yours.
    if: >
        (
          github.event_name == 'pull_request_target' &&
          github.event.pull_request.merged
        ) || (
          github.event_name == 'issue_comment' &&
          github.event.issue.pull_request &&
          github.event.comment.user.id != 97796249 &&
          startsWith(github.event.comment.body, '/backport')
        )
    steps:
        - uses: actions/checkout@v4
        - name: Create backport pull requests
          uses: korthout/backport-action@v3
          with:
            label_pattern: '^port/(.+)$'
