diff --git a/.github/workflows/cleanup-closed-pr-caches.yml b/.github/workflows/cleanup-closed-pr-caches.yml new file mode 100644 index 0000000000..436fa96899 --- /dev/null +++ b/.github/workflows/cleanup-closed-pr-caches.yml @@ -0,0 +1,70 @@ +name: Reclaim closed PR caches + +on: + workflow_run: + workflows: [Retire closed PR verification] + types: [completed] + +permissions: {} + +jobs: + cleanup: + if: >- + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion == 'success' && + startsWith(github.event.workflow_run.display_title, 'Retire PR ') && + github.event.workflow_run.path == '.github/workflows/retire-merged-pr.yml' && + github.event.workflow_run.repository.id == github.event.repository.id && + github.ref == format('refs/heads/{0}', github.event.repository.default_branch) + runs-on: ubuntu-24.04 + timeout-minutes: 10 + permissions: + actions: write + pull-requests: read + steps: + - name: Reclaim the closed pull request's cache IDs + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + repo="repos/$GITHUB_REPOSITORY" + read -r number closed < <(jq -er ' + .workflow_run.display_title | + capture("^Retire PR (?[1-9][0-9]*) closed (?[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)$") | + [.number, .closed] | @tsv' "$GITHUB_EVENT_PATH") + still_closed() { + jq -e --argjson number "$number" --arg closed "$closed" --slurpfile event "$GITHUB_EVENT_PATH" ' + $event[0].workflow_run as $run | + .number == $number and .state == "closed" and .closed_at == $closed and + (.base.ref == "main" or .base.ref == "develop") and .base.repo.id == $run.repository.id and + (.head.repo.id | type == "number") and .head.repo.id == $run.head_repository.id and + .head.ref == $run.head_branch and .head.sha == $run.head_sha + ' >/dev/null + } + pull=$(gh api "$repo/pulls/$number") + if ! still_closed <<< "$pull"; then + echo "PR $number no longer matches the recorded closure" + exit 0 + fi + ref="refs/pull/$number/merge" + gh api --method GET --paginate --slurp "$repo/actions/caches" -f ref="$ref" -F per_page=100 > "$RUNNER_TEMP/pr-caches.json" + jq -r --arg ref "$ref" --arg closed "$closed" ' + .[] | .actions_caches[] | select(.ref == $ref and + (.created_at | sub("Z$"; "")) <= ($closed | sub("Z$"; ".000000000"))) | .id + ' "$RUNNER_TEMP/pr-caches.json" > "$RUNNER_TEMP/pr-cache-ids" + while read -r id; do + [[ "$id" =~ ^[1-9][0-9]*$ ]] + pull=$(gh api "$repo/pulls/$number") + if ! still_closed <<< "$pull"; then + echo "PR $number changed before cache deletion" + break + fi + status=$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \ + --request DELETE --header "Authorization: Bearer $GH_TOKEN" \ + "$GITHUB_API_URL/$repo/actions/caches/$id") + case "$status" in + 204) echo "Reclaimed cache $id for closed PR $number" ;; + 404) echo "Cache $id for closed PR $number was already absent" ;; + *) echo "::error::Could not reclaim cache $id for PR $number (HTTP $status)"; exit 1 ;; + esac + done < "$RUNNER_TEMP/pr-cache-ids" diff --git a/.github/workflows/retire-merged-pr-android.yml b/.github/workflows/retire-merged-pr-android.yml index b9bc8ba169..c976e907eb 100644 --- a/.github/workflows/retire-merged-pr-android.yml +++ b/.github/workflows/retire-merged-pr-android.yml @@ -1,7 +1,7 @@ name: Retire merged PR Android verification on: - pull_request_target: + pull_request: types: [closed] branches: [main, develop] diff --git a/.github/workflows/retire-merged-pr-promotion.yml b/.github/workflows/retire-merged-pr-promotion.yml index 83c4c59990..96c5f9e0bf 100644 --- a/.github/workflows/retire-merged-pr-promotion.yml +++ b/.github/workflows/retire-merged-pr-promotion.yml @@ -1,7 +1,7 @@ name: Retire merged PR promotion checks on: - pull_request_target: + pull_request: types: [closed] branches: [main] diff --git a/.github/workflows/retire-merged-pr.yml b/.github/workflows/retire-merged-pr.yml index 6dd8b8030c..9162891798 100644 --- a/.github/workflows/retire-merged-pr.yml +++ b/.github/workflows/retire-merged-pr.yml @@ -1,29 +1,19 @@ name: Retire closed PR verification +run-name: Retire PR ${{ github.event.pull_request.number }} closed ${{ github.event.pull_request.closed_at }} on: - pull_request_target: + pull_request: types: [closed] branches: [main, develop] -permissions: - actions: write +permissions: {} concurrency: group: ci-pr-${{ github.event.pull_request.number }} cancel-in-progress: true jobs: - cleanup-pr-caches: - name: Reclaim closed PR caches - runs-on: ubuntu-latest - timeout-minutes: 5 + closed-pr-retired: + runs-on: ubuntu-24.04 steps: - - name: Delete GitHub Actions caches for closed PR - env: - GH_TOKEN: ${{ github.token }} - PR_REF: refs/pull/${{ github.event.pull_request.number }}/merge - run: | - ids=$(gh api --paginate "repos/${GITHUB_REPOSITORY}/actions/caches?ref=${PR_REF}&per_page=100" --jq '.actions_caches[].id' || true) - for id in $ids; do - gh api -X DELETE "repos/${GITHUB_REPOSITORY}/actions/caches/${id}" || true - done + - run: echo "Closed PR verification retired"