Skip to content

Commit e1d9609

Browse files
Clean up stale enterprise org in BeforeAll and add assertions to New-GitHubOrganization test
1 parent f80b19d commit e1d9609

1 file changed

Lines changed: 32 additions & 6 deletions

File tree

tests/Organizations.Tests.ps1

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,38 @@ Describe 'Organizations' {
4141
$orgName = "$orgPrefix$id"
4242

4343
if ($AuthType -eq 'APP') {
44-
LogGroup 'Pre-test Cleanup - App Installations' {
45-
Get-GitHubAppInstallation -Context $context | Where-Object { $_.Target.Name -like "$orgName*" } |
46-
Uninstall-GitHubApp -Confirm:$false
47-
}
48-
4944
$installationContext = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent
5045
LogGroup 'Context - Installation' {
5146
Write-Host ($installationContext | Select-Object * | Out-String)
5247
}
48+
49+
if ($OwnerType -eq 'enterprise') {
50+
# Clean up a stale enterprise org from a previous run attempt with the same
51+
# GITHUB_RUN_ID. DELETE /orgs/{org} requires org-level administration:write,
52+
# so we install the app first to obtain an org-level IAT, then delete.
53+
LogGroup 'Pre-test Cleanup - Stale Enterprise Organization' {
54+
$staleOrg = Get-GitHubOrganization -Name $orgName -ErrorAction SilentlyContinue
55+
if ($staleOrg -and $staleOrg.Name) {
56+
Write-Host "Stale org [$orgName] found from previous run attempt. Removing..."
57+
try {
58+
$null = Install-GitHubApp -Enterprise $owner -Organization $orgName `
59+
-ClientID $installationContext.ClientID -RepositorySelection 'all' -ErrorAction Stop
60+
$cleanupOrgContext = Connect-GitHubApp -Organization $orgName -Context $context -PassThru -Silent
61+
Remove-GitHubOrganization -Name $orgName -Confirm:$false -Context $cleanupOrgContext
62+
Write-Host "Stale org [$orgName] removed."
63+
} catch {
64+
Write-Host "WARNING: Could not remove stale org [$orgName]: $($_.Exception.Message)"
65+
}
66+
} else {
67+
Write-Host "No stale org found for [$orgName]."
68+
}
69+
}
70+
}
71+
72+
LogGroup 'Pre-test Cleanup - App Installations' {
73+
Get-GitHubAppInstallation -Context $context | Where-Object { $_.Target.Name -like "$orgName*" } |
74+
Uninstall-GitHubApp -Confirm:$false
75+
}
5376
}
5477
}
5578

@@ -128,10 +151,13 @@ Describe 'Organizations' {
128151
Owner = 'MariusStorhaug'
129152
BillingEmail = 'post@msx.no'
130153
}
154+
$org = New-GitHubOrganization @orgParam
131155
LogGroup 'Organization' {
132-
$org = New-GitHubOrganization @orgParam
133156
Write-Host ($org | Select-Object * | Out-String)
134157
}
158+
$org | Should -Not -BeNullOrEmpty
159+
$org | Should -BeOfType 'GitHubOrganization'
160+
$org.Name | Should -Be $orgName
135161
}
136162

137163
It 'Update-GitHubOrganization - Updates the organization location using enterprise installation' -Skip:($OwnerType -ne 'enterprise') {

0 commit comments

Comments
 (0)