From c6dbdc008cb1164716f5bee25b4fe0a388e6ce9a Mon Sep 17 00:00:00 2001 From: Constrat <56174894+Constrat@users.noreply.github.com> Date: Wed, 31 Dec 2025 19:13:12 +0100 Subject: [PATCH] ci: fixes to cache removal scripts --- .github/pr_cache_remover.ps1 | 62 +++++++++++++++++++-------------- .github/stale_cache_cleanup.ps1 | 3 +- .github/stale_cache_dev.ps1 | 3 +- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/.github/pr_cache_remover.ps1 b/.github/pr_cache_remover.ps1 index 9a2b338d62..8f035346ba 100644 --- a/.github/pr_cache_remover.ps1 +++ b/.github/pr_cache_remover.ps1 @@ -1,38 +1,46 @@ $totalClearedSize = 0 $cacheList = gh cache list --json id,ref,sizeInBytes | ConvertFrom-Json -$prCaches = $cacheList | Where-Object { $_.ref -like "*merge*" } - -if (-not $prCaches) { - Write-Output "No PR caches found." - exit +$branchSet = @{} +git ls-remote --heads origin | ForEach-Object { + if ($_ -match 'refs/heads/(.+)$') { $branchSet[$matches[1]] = $true } } -foreach ($cache in $prCaches) { - $cacheId = $cache.id - $cacheRef = $cache.ref - $cacheSizeInBytes = $cache.sizeInBytes +foreach ($cache in $cacheList) { + $shouldDelete = $false - if ($cacheRef -match "refs/pull/(\d+)/merge") { + if ($cache.ref -match "refs/pull/(\d+)/merge") { $prNumber = $matches[1] - Write-Host "PR #${prNumber}: " -NoNewline - - $prStatus = gh pr view $prNumber --json state | ConvertFrom-Json - - Write-Host $prStatus.state -NoNewline - - if ($prStatus.state -eq "MERGED" -or $prStatus.state -eq "CLOSED") { - Write-Host " -> DELETING" - - gh cache delete $cacheId + + try { + $prStatus = (gh pr view $prNumber --json state | ConvertFrom-Json).state + Write-Host "$prStatus" -NoNewline + $shouldDelete = $prStatus -in @("MERGED", "CLOSED") + } + catch { + Write-Host "NOT FOUND" -NoNewline + $shouldDelete = $true + } + } + elseif ($cache.ref -match "refs/heads/(.+)" -and $matches[1] -ne "dev") { + $branchName = $matches[1] + $exists = $branchSet.ContainsKey($branchName) + + Write-Host "Branch '$branchName': $(if ($exists) { 'EXISTS' } else { 'DELETED' })" -NoNewline + $shouldDelete = -not $exists + } + else { + continue + } - $totalClearedSize += $cacheSizeInBytes - } - else { - Write-Host "" - } + if ($shouldDelete) { + Write-Host " -> DELETING" + gh cache delete $cache.id + $totalClearedSize += $cache.sizeInBytes + } + else { + Write-Host "" } } -$formattedSize = "{0:N2}" -f ($totalClearedSize / 1MB) -Write-Output "Cleared size: $formattedSize MB" +Write-Output "Cleared size: $("{0:N2}" -f ($totalClearedSize / 1MB)) MB" diff --git a/.github/stale_cache_cleanup.ps1 b/.github/stale_cache_cleanup.ps1 index 96354c7445..1acf8600fc 100644 --- a/.github/stale_cache_cleanup.ps1 +++ b/.github/stale_cache_cleanup.ps1 @@ -11,7 +11,8 @@ $keyPatterns = @( "macOS-arm64-maadeps", "Linux-x64-maadeps", "Linux-arm64-maadeps", - "Smoke-testing" + "Smoke-testing", + "prek-v1|Linux|X64" ) # Filter caches matching any of our key patterns diff --git a/.github/stale_cache_dev.ps1 b/.github/stale_cache_dev.ps1 index 2fefc043a4..eee62daed4 100644 --- a/.github/stale_cache_dev.ps1 +++ b/.github/stale_cache_dev.ps1 @@ -19,7 +19,8 @@ $keyPatterns = @( "macOS-arm64-maadeps", "Linux-x64-maadeps", "Linux-arm64-maadeps", - "Smoke-testing" + "Smoke-testing", + "prek-v1|Linux|X64" ) foreach ($pattern in $keyPatterns) {