# Auto-approve PRs from repo admins so auto-merge doesn't block on reviews.
# Uses the cogos-ci app token so the approval satisfies the ruleset's review requirement.
name: Auto-approve admin PRs

on:
  pull_request:
    types: [opened, reopened, synchronize, ready_for_review]

jobs:
  auto-approve:
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-latest
    steps:
      - name: Generate CI app token
        id: app-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.APP_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}

      - name: Check author permission
        id: check
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token }}
        run: |
          PERM=$(gh api repos/${{ github.repository }}/collaborators/${{ github.event.pull_request.user.login }}/permission --jq .permission)
          echo "permission=$PERM" >> "$GITHUB_OUTPUT"

      - name: Approve PR
        if: steps.check.outputs.permission == 'admin'
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token }}
        run: gh pr review ${{ github.event.pull_request.number }} --repo ${{ github.repository }} --approve
