@@ -22,16 +22,17 @@ param()
2222BeforeAll {
2323 $testName = ' Organizations'
2424 $os = $env: RUNNER_OS
25- $id = $env: GITHUB_RUN_ID
26- if (-not $id ) {
25+ $runId = $env: GITHUB_RUN_ID
26+ if (-not $runId ) {
2727 throw ' GITHUB_RUN_ID is required to safely scope pre-test cleanup in Organizations.Tests.ps1.'
2828 }
2929 # GITHUB_RUN_ATTEMPT increments on each rerun (1, 2, 3...). Enterprise org names go on a
3030 # 90-day hold after deletion, so a rerun of the same GITHUB_RUN_ID would collide if we used
3131 # the run ID alone. Appending the attempt number makes each attempt produce a unique org name.
3232 $attempt = $env: GITHUB_RUN_ATTEMPT
33+ $id = $runId
3334 if ($attempt -and $attempt -ne ' 1' ) {
34- $id = " $id -$attempt "
35+ $id = " $runId -$attempt "
3536 }
3637}
3738
@@ -58,38 +59,55 @@ Describe 'Organizations' {
5859 # GITHUB_RUN_ID. DELETE /orgs/{org} requires org-level administration:write,
5960 # so we install the app first to obtain an org-level IAT, then delete.
6061 LogGroup ' Pre-test Cleanup - Stale Enterprise Organization' {
61- $staleOrg = Get-GitHubOrganization - Name $orgName - ErrorAction SilentlyContinue
62- if ($staleOrg -and $staleOrg.Name ) {
63- Write-Host " Stale org [$orgName ] found from previous run attempt. Removing..."
64- try {
65- # Retry Install-GitHubApp: the enterprise apps endpoint can return 404
66- # for a short time after the org was originally created.
67- $maxAttempts = 5
68- $retryDelay = 3
69- for ($retryAttempt = 1 ; $retryAttempt -le $maxAttempts ; $retryAttempt ++ ) {
70- try {
71- $null = Install-GitHubApp - Enterprise $owner - Organization $orgName `
72- - ClientID $installationContext.ClientID - RepositorySelection ' all' - ErrorAction Stop
73- break
74- } catch {
75- if ($retryAttempt -lt $maxAttempts ) {
76- Write-Host " Install-GitHubApp attempt $retryAttempt /$maxAttempts failed: $ ( $_.Exception.Message ) . Retrying in ${retryDelay} s..."
77- Start-Sleep - Seconds $retryDelay
78- } else {
79- throw
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+ $orgPrefix = " $testName -$os -$runId "
66+ Write-Host " Searching for stale orgs matching prefix: $orgPrefix *"
67+
68+ # Collect all orgs that match the base run prefix pattern
69+ $staleOrgs = Get-GitHubOrganization - ErrorAction SilentlyContinue |
70+ Where-Object { $_.Name -like " $orgPrefix *" -and $_.Name -ne $orgName }
71+
72+ # Also check for the current org name in case it exists from a failed attempt
73+ $currentOrg = Get-GitHubOrganization - Name $orgName - ErrorAction SilentlyContinue
74+ if ($currentOrg -and $currentOrg.Name ) {
75+ $staleOrgs += $currentOrg
76+ }
77+
78+ if ($staleOrgs.Count -gt 0 ) {
79+ foreach ($staleOrg in $staleOrgs ) {
80+ Write-Host " Stale org [$ ( $staleOrg.Name ) ] found from previous run. Removing..."
81+ try {
82+ # Retry Install-GitHubApp: the enterprise apps endpoint can return 404
83+ # for a short time after the org was originally created.
84+ $maxAttempts = 5
85+ $retryDelay = 3
86+ for ($retryAttempt = 1 ; $retryAttempt -le $maxAttempts ; $retryAttempt ++ ) {
87+ try {
88+ $null = Install-GitHubApp - Enterprise $owner - Organization $staleOrg.Name `
89+ - ClientID $installationContext.ClientID - RepositorySelection ' all' - ErrorAction Stop
90+ break
91+ } catch {
92+ if ($retryAttempt -lt $maxAttempts ) {
93+ Write-Host " Install-GitHubApp attempt $retryAttempt /$maxAttempts failed: $ ( $_.Exception.Message ) . Retrying in ${retryDelay} s..."
94+ Start-Sleep - Seconds $retryDelay
95+ } else {
96+ throw
97+ }
8098 }
8199 }
100+ $cleanupOrgContext = Connect-GitHubApp - Organization $staleOrg.Name - Context $context - PassThru - Silent
101+ Remove-GitHubOrganization - Name $staleOrg.Name - Confirm:$false - Context $cleanupOrgContext
102+ Write-Host " Stale org [$ ( $staleOrg.Name ) ] removed."
103+ } catch {
104+ # Rethrow — if the org exists but we can't remove it, New-GitHubOrganization
105+ # will fail anyway. Failing here gives a clearer root-cause message.
106+ throw " Could not remove stale org [$ ( $staleOrg.Name ) ]: $ ( $_.Exception.Message ) "
82107 }
83- $cleanupOrgContext = Connect-GitHubApp - Organization $orgName - Context $context - PassThru - Silent
84- Remove-GitHubOrganization - Name $orgName - Confirm:$false - Context $cleanupOrgContext
85- Write-Host " Stale org [$orgName ] removed."
86- } catch {
87- # Rethrow — if the org exists but we can't remove it, New-GitHubOrganization
88- # will fail anyway. Failing here gives a clearer root-cause message.
89- throw " Could not remove stale org [$orgName ]: $ ( $_.Exception.Message ) "
90108 }
91109 } else {
92- Write-Host " No stale org found for [ $orgName ]. "
110+ Write-Host " No stale orgs found matching prefix: $orgPrefix * "
93111 }
94112 }
95113 }
@@ -189,10 +207,6 @@ Describe 'Organizations' {
189207 { Update-GitHubOrganization - Name $orgName - Location ' New Location' } | Should - Throw
190208 }
191209
192- It ' Remove-GitHubOrganization - Removes an organization using enterprise installation' - Skip:($OwnerType -ne ' enterprise' ) {
193- { Remove-GitHubOrganization - Name $orgName - Confirm:$false } | Should - Throw
194- }
195-
196210 It ' Install-GitHubApp - Installs a GitHub App to an organization' - Skip:($OwnerType -ne ' enterprise' ) {
197211 # Retry: the enterprise apps endpoint can return 404 transiently right after
198212 # New-GitHubOrganization, before the new org has propagated.
@@ -233,6 +247,10 @@ Describe 'Organizations' {
233247 Update-GitHubOrganization - Name $orgName - Location ' New Location' - Context $orgContext
234248 }
235249
250+ It ' Remove-GitHubOrganization - Removes an organization using enterprise installation' - Skip:($OwnerType -ne ' enterprise' ) {
251+ { Remove-GitHubOrganization - Name $orgName - Confirm:$false } | Should - Throw
252+ }
253+
236254 It ' Remove-GitHubOrganization - Removes an organization using organization installation' - Skip:($OwnerType -ne ' enterprise' ) {
237255 $orgContext = Connect-GitHubApp - Organization $orgName - Context $context - PassThru - Silent
238256 Remove-GitHubOrganization - Name $orgName - Confirm:$false - Context $orgContext
0 commit comments