|
| 1 | +[CmdletBinding()] |
| 2 | +param( |
| 3 | + [string]$StudentRepository = '', |
| 4 | + |
| 5 | + [string]$ClassroomOrg = 'Community-Access-Classroom', |
| 6 | + |
| 7 | + [string]$TemplateRef = 'main', |
| 8 | + |
| 9 | + [ValidateSet('core-day1', 'core-day2', 'automation-core', 'all-core', 'custom')] |
| 10 | + [string]$Profile = 'all-core', |
| 11 | + |
| 12 | + [string[]]$RepoPrefixes = @('you-belong-here-', 'you-can-build-this-'), |
| 13 | + |
| 14 | + [switch]$OpenPullRequest, |
| 15 | + |
| 16 | + [switch]$AutoMerge, |
| 17 | + |
| 18 | + [switch]$AdminMerge, |
| 19 | + |
| 20 | + [switch]$DryRun, |
| 21 | + |
| 22 | + [switch]$Force |
| 23 | +) |
| 24 | + |
| 25 | +$ErrorActionPreference = 'Stop' |
| 26 | + |
| 27 | +function Invoke-CheckedCommand { |
| 28 | + param([string]$FilePath, [string[]]$Arguments) |
| 29 | + |
| 30 | + & $FilePath @Arguments |
| 31 | + if ($LASTEXITCODE -ne 0) { |
| 32 | + throw "Command failed: $FilePath $($Arguments -join ' ')" |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function Invoke-GhJson { |
| 37 | + param([string[]]$Arguments) |
| 38 | + |
| 39 | + $json = & gh @Arguments |
| 40 | + if ($LASTEXITCODE -ne 0) { |
| 41 | + throw "Command failed: gh $($Arguments -join ' ')" |
| 42 | + } |
| 43 | + |
| 44 | + if (-not $json) { |
| 45 | + return $null |
| 46 | + } |
| 47 | + |
| 48 | + return $json | ConvertFrom-Json |
| 49 | +} |
| 50 | + |
| 51 | +function Test-RepositoryHasActivity { |
| 52 | + param([string]$Repository) |
| 53 | + |
| 54 | + $activity = [ordered]@{ |
| 55 | + HasNonMainBranch = $false |
| 56 | + HasOpenPR = $false |
| 57 | + } |
| 58 | + |
| 59 | + $branches = Invoke-GhJson -Arguments @('api', "repos/$Repository/branches?per_page=100") |
| 60 | + if ($branches) { |
| 61 | + $nonMain = @($branches | Where-Object { $_.name -ne 'main' }) |
| 62 | + if ($nonMain.Count -gt 0) { |
| 63 | + $activity.HasNonMainBranch = $true |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + $openPrs = Invoke-GhJson -Arguments @('pr', 'list', '-R', $Repository, '--state', 'open', '--json', 'number') |
| 68 | + if ($openPrs) { |
| 69 | + if (@($openPrs).Count -gt 0) { |
| 70 | + $activity.HasOpenPR = $true |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return [PSCustomObject]$activity |
| 75 | +} |
| 76 | + |
| 77 | +function Get-TargetRepositories { |
| 78 | + param( |
| 79 | + [string]$SingleRepository, |
| 80 | + [string]$Organization, |
| 81 | + [string[]]$Prefixes |
| 82 | + ) |
| 83 | + |
| 84 | + if ($SingleRepository) { |
| 85 | + return @($SingleRepository) |
| 86 | + } |
| 87 | + |
| 88 | + $repos = Invoke-GhJson -Arguments @('repo', 'list', $Organization, '--limit', '500', '--json', 'name,nameWithOwner,isPrivate') |
| 89 | + if (-not $repos) { |
| 90 | + return @() |
| 91 | + } |
| 92 | + |
| 93 | + $targets = @() |
| 94 | + foreach ($repo in $repos) { |
| 95 | + if (-not $repo.isPrivate) { |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + foreach ($prefix in $Prefixes) { |
| 100 | + if ($repo.name.StartsWith($prefix, [System.StringComparison]::OrdinalIgnoreCase)) { |
| 101 | + $targets += $repo.nameWithOwner |
| 102 | + break |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return $targets |
| 108 | +} |
| 109 | + |
| 110 | +Write-Host 'Checking GitHub CLI authentication...' |
| 111 | +Invoke-CheckedCommand gh @('auth', 'status', '-h', 'github.com') |
| 112 | + |
| 113 | +$scriptDir = if ($PSScriptRoot) { $PSScriptRoot } else { Split-Path -Parent $MyInvocation.MyCommand.Path } |
| 114 | +$restoreScript = Join-Path $scriptDir 'Restore-LearningRoomFiles.ps1' |
| 115 | +if (-not (Test-Path -LiteralPath $restoreScript -PathType Leaf)) { |
| 116 | + throw "Required script not found: $restoreScript" |
| 117 | +} |
| 118 | + |
| 119 | +$targets = Get-TargetRepositories -SingleRepository $StudentRepository -Organization $ClassroomOrg -Prefixes $RepoPrefixes |
| 120 | +if (-not $targets -or $targets.Count -eq 0) { |
| 121 | + Write-Host 'No target repositories found. Nothing to do.' |
| 122 | + return |
| 123 | +} |
| 124 | + |
| 125 | +Write-Host "Found $($targets.Count) target repository/repositories." |
| 126 | + |
| 127 | +$summary = @() |
| 128 | + |
| 129 | +foreach ($repo in $targets) { |
| 130 | + Write-Host '' |
| 131 | + Write-Host "Processing $repo" |
| 132 | + |
| 133 | + $row = [ordered]@{ |
| 134 | + Repository = $repo |
| 135 | + Status = 'unknown' |
| 136 | + Details = '' |
| 137 | + } |
| 138 | + |
| 139 | + try { |
| 140 | + if (-not $Force) { |
| 141 | + $activity = Test-RepositoryHasActivity -Repository $repo |
| 142 | + if ($activity.HasNonMainBranch -or $activity.HasOpenPR) { |
| 143 | + $reasons = @() |
| 144 | + if ($activity.HasNonMainBranch) { $reasons += 'non-main branches exist' } |
| 145 | + if ($activity.HasOpenPR) { $reasons += 'open PRs exist' } |
| 146 | + |
| 147 | + $row.Status = 'skipped' |
| 148 | + $row.Details = ($reasons -join '; ') |
| 149 | + Write-Host "Skipping due to activity: $($row.Details)" |
| 150 | + $summary += [PSCustomObject]$row |
| 151 | + continue |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + $stamp = Get-Date -Format 'yyyyMMddHHmmss' |
| 156 | + $branchName = "recovery/template-sync-$stamp" |
| 157 | + |
| 158 | + $restoreParams = @{ |
| 159 | + StudentRepository = $repo |
| 160 | + Profile = $Profile |
| 161 | + TemplateRef = $TemplateRef |
| 162 | + BranchName = $branchName |
| 163 | + CommitMessage = 'chore: sync learning-room template baseline' |
| 164 | + } |
| 165 | + |
| 166 | + if ($OpenPullRequest -or $AutoMerge) { |
| 167 | + $restoreParams.OpenPullRequest = $true |
| 168 | + } |
| 169 | + |
| 170 | + if ($DryRun) { |
| 171 | + $row.Status = 'dry-run' |
| 172 | + $row.Details = "Would run restore on branch $branchName" |
| 173 | + Write-Host $row.Details |
| 174 | + $summary += [PSCustomObject]$row |
| 175 | + continue |
| 176 | + } |
| 177 | + |
| 178 | + & $restoreScript @restoreParams |
| 179 | + if ($LASTEXITCODE -ne 0) { |
| 180 | + throw "Restore script failed for $repo" |
| 181 | + } |
| 182 | + |
| 183 | + if ($AutoMerge) { |
| 184 | + $prNumber = & gh pr list -R $repo --head $branchName --state open --json number --jq '.[0].number' |
| 185 | + if ($LASTEXITCODE -ne 0) { |
| 186 | + throw "Could not query PR number for $repo" |
| 187 | + } |
| 188 | + |
| 189 | + if (-not $prNumber) { |
| 190 | + $row.Status = 'updated' |
| 191 | + $row.Details = 'No open recovery PR found after restore (possibly no diff)' |
| 192 | + Write-Host $row.Details |
| 193 | + $summary += [PSCustomObject]$row |
| 194 | + continue |
| 195 | + } |
| 196 | + |
| 197 | + $mergeArgs = @('pr', 'merge', $prNumber, '-R', $repo, '--squash', '--delete-branch') |
| 198 | + if ($AdminMerge) { |
| 199 | + $mergeArgs += '--admin' |
| 200 | + } |
| 201 | + |
| 202 | + Invoke-CheckedCommand gh $mergeArgs |
| 203 | + $row.Status = 'merged' |
| 204 | + $row.Details = "Merged PR #$prNumber" |
| 205 | + Write-Host $row.Details |
| 206 | + } |
| 207 | + else { |
| 208 | + $row.Status = 'updated' |
| 209 | + $row.Details = 'Restore completed' |
| 210 | + Write-Host $row.Details |
| 211 | + } |
| 212 | + } |
| 213 | + catch { |
| 214 | + $row.Status = 'failed' |
| 215 | + $row.Details = $_.Exception.Message |
| 216 | + Write-Warning $row.Details |
| 217 | + } |
| 218 | + |
| 219 | + $summary += [PSCustomObject]$row |
| 220 | +} |
| 221 | + |
| 222 | +Write-Host '' |
| 223 | +Write-Host 'Summary' |
| 224 | +$summary | Format-Table -AutoSize |
0 commit comments