From 353e6ee026af9d501399dd85b9105ac9785907fa Mon Sep 17 00:00:00 2001 From: lalalune <18633264+lalalune@users.noreply.github.com> Date: Fri, 4 Dec 2026 16:47:52 -0500 Subject: [PATCH 1/3] Retire closed PR runs without privileges and reclaim validated PR caches on the default branch --- .../workflows/cleanup-closed-pr-caches.yml | 66 +++++++++++++++++++ .../workflows/retire-merged-pr-android.yml | 2 +- .../workflows/retire-merged-pr-promotion.yml | 2 +- .github/workflows/retire-merged-pr.yml | 20 ++---- 4 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/cleanup-closed-pr-caches.yml diff --git a/.github/workflows/cleanup-closed-pr-caches.yml b/.github/workflows/cleanup-closed-pr-caches.yml new file mode 100644 index 0000000000..2fa3628d54 --- /dev/null +++ b/.github/workflows/cleanup-closed-pr-caches.yml @@ -0,0 +1,66 @@ +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.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" + head=$(jq -er '.workflow_run | [.head_repository.owner.login, .head_branch] | select(all(.[]; type == "string" and length > 0)) | join(":")' "$GITHUB_EVENT_PATH") + gh api --method GET --paginate --slurp "$repo/pulls" -f state=closed -f head="$head" -F per_page=100 > "$RUNNER_TEMP/closed-pulls.json" + jq -r --slurpfile event "$GITHUB_EVENT_PATH" ' + $event[0].workflow_run as $run | + .[][] | select(.state == "closed" and (.base.ref == "main" or .base.ref == "develop") and + .base.repo.id == $run.repository.id and .head.repo.id == $run.head_repository.id and + .head.ref == $run.head_branch and .head.sha == $run.head_sha and + .closed_at != null and .closed_at <= $run.created_at) | + [.number, .closed_at] | @tsv + ' "$RUNNER_TEMP/closed-pulls.json" > "$RUNNER_TEMP/pr-closures.tsv" + while IFS=$'\t' read -r number closed; do + [[ "$number" =~ ^[1-9][0-9]*$ ]] + 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 ! jq -e --arg closed "$closed" --slurpfile event "$GITHUB_EVENT_PATH" ' + .state == "closed" and .closed_at == $closed and + (.base.ref == "main" or .base.ref == "develop") and + .base.repo.id == $event[0].repository.id and .head.sha == $event[0].workflow_run.head_sha + ' <<< "$pull" >/dev/null; then + 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" + done < "$RUNNER_TEMP/pr-closures.tsv" 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..9a533292ba 100644 --- a/.github/workflows/retire-merged-pr.yml +++ b/.github/workflows/retire-merged-pr.yml @@ -1,29 +1,19 @@ name: Retire closed PR verification 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 + closed-pr-retired: + if: github.event.action != 'closed' runs-on: ubuntu-latest - timeout-minutes: 5 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: exit 1 From 313ca3fd5dd2ca97c7d8f5fdc6dfe193d5601eef Mon Sep 17 00:00:00 2001 From: lalalune <18633264+lalalune@users.noreply.github.com> Date: Fri, 4 Dec 2026 16:48:45 -0500 Subject: [PATCH 2/3] Bind protected cleanup to the exact recorded pull request closure --- .../workflows/cleanup-closed-pr-caches.yml | 79 ++++++++++--------- .github/workflows/retire-merged-pr.yml | 6 +- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/.github/workflows/cleanup-closed-pr-caches.yml b/.github/workflows/cleanup-closed-pr-caches.yml index 2fa3628d54..336719e9f5 100644 --- a/.github/workflows/cleanup-closed-pr-caches.yml +++ b/.github/workflows/cleanup-closed-pr-caches.yml @@ -11,6 +11,8 @@ 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) @@ -26,41 +28,42 @@ jobs: run: | set -euo pipefail repo="repos/$GITHUB_REPOSITORY" - head=$(jq -er '.workflow_run | [.head_repository.owner.login, .head_branch] | select(all(.[]; type == "string" and length > 0)) | join(":")' "$GITHUB_EVENT_PATH") - gh api --method GET --paginate --slurp "$repo/pulls" -f state=closed -f head="$head" -F per_page=100 > "$RUNNER_TEMP/closed-pulls.json" - jq -r --slurpfile event "$GITHUB_EVENT_PATH" ' - $event[0].workflow_run as $run | - .[][] | select(.state == "closed" and (.base.ref == "main" or .base.ref == "develop") and - .base.repo.id == $run.repository.id and .head.repo.id == $run.head_repository.id and - .head.ref == $run.head_branch and .head.sha == $run.head_sha and - .closed_at != null and .closed_at <= $run.created_at) | - [.number, .closed_at] | @tsv - ' "$RUNNER_TEMP/closed-pulls.json" > "$RUNNER_TEMP/pr-closures.tsv" - while IFS=$'\t' read -r number closed; do - [[ "$number" =~ ^[1-9][0-9]*$ ]] - 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 ! jq -e --arg closed "$closed" --slurpfile event "$GITHUB_EVENT_PATH" ' - .state == "closed" and .closed_at == $closed and - (.base.ref == "main" or .base.ref == "develop") and - .base.repo.id == $event[0].repository.id and .head.sha == $event[0].workflow_run.head_sha - ' <<< "$pull" >/dev/null; then - 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" - done < "$RUNNER_TEMP/pr-closures.tsv" + 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 == $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.yml b/.github/workflows/retire-merged-pr.yml index 9a533292ba..9162891798 100644 --- a/.github/workflows/retire-merged-pr.yml +++ b/.github/workflows/retire-merged-pr.yml @@ -1,4 +1,5 @@ name: Retire closed PR verification +run-name: Retire PR ${{ github.event.pull_request.number }} closed ${{ github.event.pull_request.closed_at }} on: pull_request: @@ -13,7 +14,6 @@ concurrency: jobs: closed-pr-retired: - if: github.event.action != 'closed' - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - - run: exit 1 + - run: echo "Closed PR verification retired" From fea71ac6215c3811373986bd5d0b51363d4a3fc7 Mon Sep 17 00:00:00 2001 From: lalalune <18633264+lalalune@users.noreply.github.com> Date: Fri, 4 Dec 2026 16:50:40 -0500 Subject: [PATCH 3/3] Require an observed source repository ID on every protected cleanup --- .github/workflows/cleanup-closed-pr-caches.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cleanup-closed-pr-caches.yml b/.github/workflows/cleanup-closed-pr-caches.yml index 336719e9f5..436fa96899 100644 --- a/.github/workflows/cleanup-closed-pr-caches.yml +++ b/.github/workflows/cleanup-closed-pr-caches.yml @@ -37,7 +37,8 @@ jobs: $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 == $run.head_repository.id and .head.ref == $run.head_branch and .head.sha == $run.head_sha + (.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")