# SPDX-FileCopyrightText: 2015-2025 Alexey Rochev
#
# SPDX-License-Identifier: CC0-1.0

name: Cleanup cache

on:
  pull_request:
    types: [closed]

jobs:
  cleanup-cache:
    runs-on: ubuntu-latest
    steps:
    - name: Check out sources
      uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3
      with:
        submodules: 'recursive'

    - name: Delete caches for closed pull requet
      run: |
        ref="refs/pull/${{ github.event.number }}/merge"
        ids=( $(gh cache list --json id --jq '.[].id' --ref "$ref") )
        echo "Cache ids for ref $ref: ${ids[@]}"
        for id in "${ids[@]}"; do
          echo "Deleting cache with id $id"
          gh cache delete "$id"
        done
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
