From 1f49d59e6b98a52cd4d502cc6d4d587e47096839 Mon Sep 17 00:00:00 2001
From: yell0wsuit <5692900+yell0wsuit@users.noreply.github.com>
Date: Fri, 5 Jul 2024 13:56:43 +0700
Subject: [PATCH] add cache deletion for closed PRs

---
 .github/workflows/checks.yml | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml
index 2bef2d8e00..ab45b6d5fa 100644
--- a/.github/workflows/checks.yml
+++ b/.github/workflows/checks.yml
@@ -11,6 +11,10 @@ on:
       - '!*.md'
       - '.github/workflows/*.yml'
 
+  pull_request_target:
+    branches: [ master ]
+    types: [closed]
+
 permissions:
   pull-requests: write
   checks: write
@@ -72,3 +76,29 @@ jobs:
     uses: ./.github/workflows/build.yml
     needs: format
     secrets: inherit
+
+  cleanup:
+    if: github.event.pull_request.merged == false
+    runs-on: ubuntu-latest
+    steps:
+      - name: Setup gh extension
+        run: |
+          gh extension install actions/gh-actions-cache
+
+      - name: Cleanup
+        run: |
+          echo "Fetching list of cache keys"
+          cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH -L 100 | cut -f 1 )
+
+          ## Setting this to not fail the workflow while deleting cache keys.
+          set +e
+          echo "Deleting caches..."
+          for cacheKey in $cacheKeysForPR
+          do
+              gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
+          done
+          echo "Done"
+        env:
+          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+          REPO: ${{ github.repository }}
+          BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge