name: Cleanup caches for closed PRs

on:
  # Run twice every day to remove the cache so that the caches from the closed prs
  # are removed.
  schedule:
    - cron: "0 17 * * *"
    - cron: "30 18 * * *"
  workflow_dispatch:

jobs:
  cleanup:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Cleanup
        run: |
          gh extension install actions/gh-actions-cache

          export REPO="${{ github.repository }}"

          gh pr list --state closed -L 20 --json number --jq '.[]|.number' | (
              while IFS='$\n' read -r closed_pr; do
                  BRANCH="refs/pull/${closed_pr}/merge" ./cleanup-cache.sh
              done
          )
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}