name: Auto Merge PRs

on:
  pull_request_target:
    types:
      - opened
      - reopened
      - synchronize
      - ready_for_review

permissions:
  contents: write
  pull-requests: write

concurrency:
  group: automerge-${{ github.event.pull_request.number }}
  cancel-in-progress: true

jobs:
  enable-automerge:
    if: >
      github.event.pull_request.draft == false &&
      github.event.pull_request.state == 'open' &&
      github.event.pull_request.base.ref == 'main'
    runs-on: ubuntu-latest

    steps:
      - name: Enable squash auto-merge
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_URL: ${{ github.event.pull_request.html_url }}
        run: gh pr merge --auto --squash --delete-branch "$PR_URL"
