Skip to content

Commit 87addee

Browse files
Optimize stale-org cleanup: use deterministic lookups instead of enumerating all enterprise orgs
Replace inefficient enumeration of all enterprise organizations (which can burn API quota on large enterprises) with deterministic lookups of specific candidate org names. The cleanup now checks only the expected org names from the current run and previous attempts, reducing API calls and improving performance without functional change.
1 parent 916f0b0 commit 87addee

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

tests/Organizations.Tests.ps1

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,30 @@ Describe 'Organizations' {
5959
# GITHUB_RUN_ID. DELETE /orgs/{org} requires org-level administration:write,
6060
# so we install the app first to obtain an org-level IAT, then delete.
6161
LogGroup 'Pre-test Cleanup - Stale Enterprise Organization' {
62-
# On reruns, clean up any orgs matching the base run prefix (e.g., ...-1234,
63-
# ...-1234-2, etc.) before creating a new one. This prevents orphaned orgs
64-
# from failed previous attempts.
65-
$orgRunPrefix = "$testName-$os-$runId"
66-
Write-Host "Searching for stale orgs matching prefix: $orgRunPrefix*"
62+
# On reruns, clean up any orgs from the current and previous attempts. Deleted
63+
# GitHub organizations are unavailable for 90 days, so rerun attempts must use
64+
# unique org names to avoid collisions. Deterministically check for stale orgs
65+
# from the base run (attempt 1) and any previous rerun attempts (2, 3, etc.).
66+
# Use direct lookups by name instead of enumerating all enterprise orgs to avoid
67+
# API quota burn on enterprises with many organizations.
6768

68-
# Collect all orgs that match the base run prefix pattern (scoped to this enterprise)
69-
$staleOrgs = Get-GitHubOrganization -Enterprise $owner -ErrorAction SilentlyContinue |
70-
Where-Object { $_.Name -like "$orgRunPrefix*" -and $_.Name -ne $orgName }
69+
# Build deterministic list of org names to check: base run + previous attempts
70+
$orgNamesToCheck = @("$testName-$os-$runId") # Attempt 1
71+
if ($attempt -and $attempt -ne '1') {
72+
for ($attemptNum = 2; $attemptNum -le [int]$attempt; $attemptNum++) {
73+
$orgNamesToCheck += "$testName-$os-$runId-$attemptNum"
74+
}
75+
}
7176

72-
# Also check for the current org name in case it exists from a failed attempt
73-
$currentOrg = Get-GitHubOrganization -Enterprise $owner -Name $orgName -ErrorAction SilentlyContinue
74-
if ($currentOrg -and $currentOrg.Name) {
75-
$staleOrgs += $currentOrg
77+
# Check each expected org name; collect any that exist and differ from current org
78+
$staleOrgs = @()
79+
foreach ($candidateName in $orgNamesToCheck) {
80+
if ($candidateName -ne $orgName) { # Skip the current org we're about to create
81+
$candidateOrg = Get-GitHubOrganization -Enterprise $owner -Name $candidateName -ErrorAction SilentlyContinue
82+
if ($candidateOrg -and $candidateOrg.Name) {
83+
$staleOrgs += $candidateOrg
84+
}
85+
}
7686
}
7787

7888
if ($staleOrgs.Count -gt 0) {

0 commit comments

Comments
 (0)