name: Trigger branch cleanup for WAI-APG site

on:
  delete:
  pull_request_target:
    branches-ignore:
      - "dependabot/**"
    types:
      - closed

jobs:
  cleanup-branch-wai:
    if: ${{ github.event_name == 'delete' && github.event.ref_type == 'branch' }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          cache: npm

      - name: Install @octokit/rest
        run: npm install @octokit/rest

      - name: Run wai-trigger-cleanup script
        run: |
          node scripts/wai-trigger-cleanup.js
        env:
          GH_TOKEN: ${{ secrets.W3CGRUNTBOT_TOKEN }}
          OWNER: ${{ github.repository_owner }}
          # reference github.event.ref for branch deletion
          APG_BRANCH: ${{ github.event.ref }}

  cleanup-pr-wai:
    if: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          cache: npm

      - name: Install @octokit/rest
        run: npm install @octokit/rest

      - name: Run wai-trigger-cleanup script
        run: |
          node scripts/wai-trigger-cleanup.js
        env:
          GH_TOKEN: ${{ secrets.W3CGRUNTBOT_TOKEN }}
          OWNER: ${{ github.repository_owner }}
          # reference github.head_ref for pull request
          APG_BRANCH: ${{ github.head_ref }}