name: Approve DROP statements

on:
  issue_comment:
    types: [created]

permissions:
  statuses: write
  pull-requests: write

jobs:
  approve-drops:
    name: Approve DROP statements
    if: >-
      github.event.issue.pull_request &&
      contains(github.event.comment.body, '/proceed-with-drop-statements')
    runs-on: ubuntu-latest
    steps:
      - name: Check commenter permissions
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          PERMISSION=$(gh api "repos/${{ github.repository }}/collaborators/${{ github.event.comment.user.login }}/permission" --jq '.permission')
          if [ "$PERMISSION" != "admin" ] && [ "$PERMISSION" != "write" ]; then
            echo "::error::User ${{ github.event.comment.user.login }} does not have write access (permission: $PERMISSION)"
            exit 1
          fi
          echo "User ${{ github.event.comment.user.login }} has $PERMISSION access."

      - name: Get PR head SHA
        id: pr
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          SHA=$(gh api "repos/${{ github.repository }}/pulls/${{ github.event.issue.number }}" --jq '.head.sha')
          echo "sha=$SHA" >> "$GITHUB_OUTPUT"

      - name: React to comment with eyes
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh api "repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions" \
            -f content="eyes"

      - name: Set drop-statement-review status to success
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh api "repos/${{ github.repository }}/statuses/${{ steps.pr.outputs.sha }}" \
            -f state="success" \
            -f description="DROP statements approved by @${{ github.event.comment.user.login }}" \
            -f context="drop-statement-review"
