name: Backport
on:
  pull_request_target:
    types:
      - closed
      - labeled

permissions:
  contents: read

jobs:
  backport:
    name: Backport
    runs-on: ubuntu-latest
    environment:
      name: Backport
    # Only react to merged PRs for security reasons.
    # See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target.
    if: >
      github.event.pull_request.merged && (
        github.event.action == 'closed'
        || (
          github.event.action == 'labeled'
          && contains(github.event.label.name, 'backport')
        )
      )
    steps:
      - uses: actions/create-github-app-token@67018539274d69449ef7c02e8e71183d1719ab42 # v2.1.4
        id: app-token
        with:
          app-id: ${{ vars.BACKPORT_APP_ID }}
          private-key: ${{ secrets.PRIVATE_KEY }}
          permission-contents: write # push branch to Github
          permission-pull-requests: write # create PR / add comment for manual backport
          permission-workflows: write # modify files in .github/workflows
      - uses: pylint-dev/backport@94367840595495e101f9a31415897c05da1f08d9 # v2.1.1
        with:
          github_token: ${{ steps.app-token.outputs.token }}
          user_name: ${{ vars.BACKPORT_USER_NAME }}
          user_email: ${{ vars.BACKPORT_USER_EMAIL }}
