name: Auto-enable Auto-merge
on:
  pull_request_target:
    types: [opened, synchronize, reopened, ready_for_review]
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]

permissions:
  pull-requests: write
  contents: write

jobs:
  enable-auto-merge:
    # When a fork PR is opened, both pull_request_target and pull_request fire.
    # Skip the pull_request run for fork PRs to avoid a duplicate (failing) job.
    if: >-
      github.event_name == 'pull_request_target' ||
      (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository)
    runs-on: ubuntu-latest
    steps:
      - name: Enable Auto-Merge
        env:
          GH_TOKEN: ${{ github.token }}

        run: gh pr merge --auto --squash "${{ github.event.pull_request.html_url }}"
