|
| 1 | +[CmdletBinding()] |
| 2 | +param() |
| 3 | + |
| 4 | +LogGroup 'BeforeAll - Global Test Setup' { |
| 5 | + $authCases = . "$PSScriptRoot/Data/AuthCases.ps1" |
| 6 | + $id = $env:GITHUB_RUN_ID |
| 7 | + if (-not $id) { |
| 8 | + throw 'GITHUB_RUN_ID environment variable is not set. Refusing to create or clean up test repositories with a non-deterministic name.' |
| 9 | + } |
| 10 | + if (-not $env:Settings) { |
| 11 | + throw 'Settings environment variable is not set. Process-PSModule must populate it with the test suite configuration.' |
| 12 | + } |
| 13 | + |
| 14 | + # Derive the list of OS names from the Settings JSON provided by Process-PSModule. |
| 15 | + try { |
| 16 | + $settings = $env:Settings | ConvertFrom-Json |
| 17 | + } catch { |
| 18 | + throw "Settings environment variable does not contain valid JSON. Process-PSModule must populate it with a valid test suite configuration. $_" |
| 19 | + } |
| 20 | + |
| 21 | + $osNames = @($settings.TestSuites.Module.OSName | Sort-Object -Unique) |
| 22 | + if (-not $osNames) { |
| 23 | + throw 'Settings JSON must contain TestSuites.Module.OSName with at least one OS name.' |
| 24 | + } |
| 25 | + $invalidOsNames = @($osNames | Where-Object { -not $_ -or -not $_.ToString().Trim() }) |
| 26 | + if ($invalidOsNames.Count -gt 0) { |
| 27 | + throw 'Settings JSON contains one or more null or empty values in TestSuites.Module.OSName.' |
| 28 | + } |
| 29 | + Write-Host "Creating test repositories for OSes: $($osNames -join ', ')" |
| 30 | + |
| 31 | + foreach ($authCase in $authCases) { |
| 32 | + $authCase.GetEnumerator() | ForEach-Object { Set-Variable -Name $_.Key -Value $_.Value } |
| 33 | + |
| 34 | + if ($TokenType -eq 'GITHUB_TOKEN') { |
| 35 | + Write-Host "Skipping setup for $AuthType-$TokenType (uses existing repository)" |
| 36 | + continue |
| 37 | + } |
| 38 | + |
| 39 | + $context = Connect-GitHubAccount @connectParams -PassThru -Silent |
| 40 | + if ($AuthType -eq 'APP') { |
| 41 | + $context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent |
| 42 | + } |
| 43 | + Write-Host ($context | Format-List | Out-String) |
| 44 | + |
| 45 | + foreach ($os in $osNames) { |
| 46 | + $repoPrefix = "Test-$os-$TokenType" |
| 47 | + $repoName = "$repoPrefix-$id" |
| 48 | + |
| 49 | + LogGroup "Repository setup - $AuthType-$TokenType - $os" { |
| 50 | + # Clean up repos from a previous attempt of the same run (re-runs). |
| 51 | + # Use deterministic name lookups instead of listing all repos to reduce API calls. |
| 52 | + $cleanupRepoNames = @($repoName) |
| 53 | + if ($OwnerType -eq 'organization') { |
| 54 | + $cleanupRepoNames += "$repoName-2", "$repoName-3" |
| 55 | + } |
| 56 | + |
| 57 | + foreach ($cleanupRepoName in $cleanupRepoNames) { |
| 58 | + switch ($OwnerType) { |
| 59 | + 'user' { |
| 60 | + Get-GitHubRepository -Name $cleanupRepoName -ErrorAction SilentlyContinue | |
| 61 | + Remove-GitHubRepository -Confirm:$false |
| 62 | + } |
| 63 | + 'organization' { |
| 64 | + Get-GitHubRepository -Owner $Owner -Name $cleanupRepoName -ErrorAction SilentlyContinue | |
| 65 | + Remove-GitHubRepository -Confirm:$false |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + # Create the primary shared repository (with readme, license, gitignore for release tests). |
| 71 | + $repoParams = @{ |
| 72 | + Name = $repoName |
| 73 | + AddReadme = $true |
| 74 | + License = 'mit' |
| 75 | + Gitignore = 'VisualStudio' |
| 76 | + } |
| 77 | + switch ($OwnerType) { |
| 78 | + 'user' { |
| 79 | + New-GitHubRepository @repoParams |
| 80 | + } |
| 81 | + 'organization' { |
| 82 | + New-GitHubRepository @repoParams -Organization $Owner |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + # Create extra repositories needed by Secrets/Variables SelectedRepository tests. |
| 87 | + # Only organization owners need them — those tests are skipped for user owners. |
| 88 | + if ($OwnerType -eq 'organization') { |
| 89 | + foreach ($suffix in 2, 3) { |
| 90 | + $extraName = "$repoName-$suffix" |
| 91 | + New-GitHubRepository -Organization $Owner -Name $extraName |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent |
| 98 | + } |
| 99 | +} |
0 commit comments