|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Fixes branch rulesets by disabling existing ones and recreating with the correct configuration. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + This script inspects the existing branch rulesets for a repository, disables all of them, |
| 7 | + and renames any ruleset named "Protect main branch" to "Protect main branch (old)" so that |
| 8 | + Setup-BranchRuleset.ps1 can create a fresh ruleset without conflicts. |
| 9 | +
|
| 10 | + The script presents a plan of all changes before executing and prompts for confirmation. |
| 11 | +
|
| 12 | +.PARAMETER Repository |
| 13 | + The repository in owner/repo format. If not provided, uses the current repository. |
| 14 | +
|
| 15 | +.EXAMPLE |
| 16 | + .\Fix-BranchRuleset.ps1 |
| 17 | + Inspects and fixes rulesets for the current repository |
| 18 | +
|
| 19 | +.EXAMPLE |
| 20 | + .\Fix-BranchRuleset.ps1 -Repository "Chris-Wolfgang/my-repo" |
| 21 | + Inspects and fixes rulesets for a specific repository |
| 22 | +
|
| 23 | +.NOTES |
| 24 | + Requires: GitHub CLI (gh) authenticated with admin permissions |
| 25 | + Install gh: https://cli.github.com/ |
| 26 | +#> |
| 27 | + |
| 28 | +[CmdletBinding()] |
| 29 | +param( |
| 30 | + [Parameter()] |
| 31 | + [string]$Repository = "Chris-Wolfgang/ETL-FixedWidth" |
| 32 | +) |
| 33 | + |
| 34 | +# Check if gh CLI is installed |
| 35 | +try { |
| 36 | + $null = gh --version |
| 37 | +} catch { |
| 38 | + Write-Error "GitHub CLI (gh) is not installed or not in PATH." |
| 39 | + Write-Host "Install from: https://cli.github.com/" -ForegroundColor Yellow |
| 40 | + exit 1 |
| 41 | +} |
| 42 | + |
| 43 | +# Check if authenticated |
| 44 | +try { |
| 45 | + $null = gh auth status 2>&1 |
| 46 | + if ($LASTEXITCODE -ne 0) { |
| 47 | + Write-Error "Not authenticated with GitHub CLI." |
| 48 | + Write-Host "Run: gh auth login" -ForegroundColor Yellow |
| 49 | + exit 1 |
| 50 | + } |
| 51 | +} catch { |
| 52 | + Write-Error "Failed to check GitHub CLI authentication status." |
| 53 | + exit 1 |
| 54 | +} |
| 55 | + |
| 56 | +# Determine repository |
| 57 | +if ($Repository -eq "Chris-Wolfgang/ETL-FixedWidth" -or -not $Repository) { |
| 58 | + Write-Host "Detecting current repository..." -ForegroundColor Cyan |
| 59 | + try { |
| 60 | + $repoInfo = gh repo view --json nameWithOwner | ConvertFrom-Json |
| 61 | + $Repository = $repoInfo.nameWithOwner |
| 62 | + Write-Host "Using repository: $Repository" -ForegroundColor Green |
| 63 | + } catch { |
| 64 | + if ($Repository -eq "Chris-Wolfgang/ETL-FixedWidth") { |
| 65 | + Write-Error "Could not detect repository. Please run the setup script first to replace placeholders, or specify -Repository parameter." |
| 66 | + } else { |
| 67 | + Write-Error "Could not detect repository. Please run from within a git repository or specify -Repository parameter." |
| 68 | + } |
| 69 | + exit 1 |
| 70 | + } |
| 71 | +} else { |
| 72 | + Write-Host "Using specified repository: $Repository" -ForegroundColor Green |
| 73 | +} |
| 74 | + |
| 75 | +# Fetch all rulesets |
| 76 | +Write-Host "`nFetching existing rulesets..." -ForegroundColor Cyan |
| 77 | + |
| 78 | +try { |
| 79 | + $rulesetsJson = gh api ` |
| 80 | + -H "Accept: application/vnd.github+json" ` |
| 81 | + -H "X-GitHub-Api-Version: 2022-11-28" ` |
| 82 | + "/repos/$Repository/rulesets" ` |
| 83 | + --paginate 2>&1 |
| 84 | + |
| 85 | + if ($LASTEXITCODE -ne 0) { |
| 86 | + Write-Error "Failed to fetch rulesets: $rulesetsJson" |
| 87 | + exit 1 |
| 88 | + } |
| 89 | + |
| 90 | + $rulesets = $rulesetsJson | ConvertFrom-Json |
| 91 | +} catch { |
| 92 | + Write-Error "Failed to fetch rulesets: $($_.Exception.Message)" |
| 93 | + exit 1 |
| 94 | +} |
| 95 | + |
| 96 | +if (-not $rulesets -or $rulesets.Count -eq 0) { |
| 97 | + Write-Host "No rulesets found for $Repository. Nothing to fix." -ForegroundColor Green |
| 98 | + exit 0 |
| 99 | +} |
| 100 | + |
| 101 | +# Build the plan |
| 102 | +$plan = @() |
| 103 | +$targetRulesetName = "Protect main branch" |
| 104 | + |
| 105 | +Write-Host "`nFound $($rulesets.Count) ruleset(s):" -ForegroundColor Cyan |
| 106 | +Write-Host "" |
| 107 | + |
| 108 | +foreach ($ruleset in $rulesets) { |
| 109 | + $status = if ($ruleset.enforcement -eq "disabled") { "disabled" } else { $ruleset.enforcement } |
| 110 | + Write-Host " [$($ruleset.id)] $($ruleset.name) (enforcement: $status)" -ForegroundColor Gray |
| 111 | + |
| 112 | + $actions = @() |
| 113 | + |
| 114 | + # If this is the target name, rename it |
| 115 | + if ($ruleset.name -eq $targetRulesetName) { |
| 116 | + $actions += @{ |
| 117 | + type = "rename" |
| 118 | + description = "Rename '$($ruleset.name)' -> '$($ruleset.name) (old)'" |
| 119 | + newName = "$($ruleset.name) (old)" |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + # If not already disabled, disable it |
| 124 | + if ($ruleset.enforcement -ne "disabled") { |
| 125 | + $actions += @{ |
| 126 | + type = "disable" |
| 127 | + description = "Disable '$($ruleset.name)' (currently: $status)" |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if ($actions.Count -gt 0) { |
| 132 | + $plan += @{ |
| 133 | + ruleset = $ruleset |
| 134 | + actions = $actions |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +Write-Host "" |
| 140 | + |
| 141 | +# Present the plan |
| 142 | +if ($plan.Count -eq 0) { |
| 143 | + Write-Host "All rulesets are already disabled and none need renaming. Nothing to do." -ForegroundColor Green |
| 144 | + exit 0 |
| 145 | +} |
| 146 | + |
| 147 | +Write-Host "Planned changes:" -ForegroundColor Yellow |
| 148 | +Write-Host "" |
| 149 | + |
| 150 | +$stepNumber = 1 |
| 151 | +foreach ($item in $plan) { |
| 152 | + foreach ($action in $item.actions) { |
| 153 | + Write-Host " $stepNumber. $($action.description)" -ForegroundColor White |
| 154 | + $stepNumber++ |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +Write-Host "" |
| 159 | + |
| 160 | +# Prompt for confirmation |
| 161 | +$response = Read-Host "Proceed with these changes? (y/N)" |
| 162 | +if ($response -ne 'y' -and $response -ne 'Y') { |
| 163 | + Write-Host "Cancelled. No changes were made." -ForegroundColor Yellow |
| 164 | + exit 0 |
| 165 | +} |
| 166 | + |
| 167 | +Write-Host "" |
| 168 | + |
| 169 | +# Execute the plan |
| 170 | +$errors = 0 |
| 171 | + |
| 172 | +foreach ($item in $plan) { |
| 173 | + $ruleset = $item.ruleset |
| 174 | + $rulesetId = $ruleset.id |
| 175 | + |
| 176 | + # Build the update payload — apply rename and disable together in one API call |
| 177 | + $updatePayload = @{} |
| 178 | + |
| 179 | + foreach ($action in $item.actions) { |
| 180 | + switch ($action.type) { |
| 181 | + "rename" { |
| 182 | + $updatePayload["name"] = $action.newName |
| 183 | + } |
| 184 | + "disable" { |
| 185 | + $updatePayload["enforcement"] = "disabled" |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if ($updatePayload.Count -gt 0) { |
| 191 | + $descriptions = ($item.actions | ForEach-Object { $_.description }) -join " + " |
| 192 | + Write-Host " Updating ruleset [$rulesetId]: $descriptions..." -ForegroundColor Cyan |
| 193 | + |
| 194 | + $jsonPayload = $updatePayload | ConvertTo-Json -Depth 5 |
| 195 | + $tempFile = [System.IO.Path]::GetTempFileName() |
| 196 | + $jsonPayload | Out-File -FilePath $tempFile -Encoding utf8NoBOM |
| 197 | + |
| 198 | + try { |
| 199 | + $result = gh api ` |
| 200 | + --method PUT ` |
| 201 | + -H "Accept: application/vnd.github+json" ` |
| 202 | + -H "X-GitHub-Api-Version: 2022-11-28" ` |
| 203 | + "/repos/$Repository/rulesets/$rulesetId" ` |
| 204 | + --input $tempFile 2>&1 |
| 205 | + |
| 206 | + if ($LASTEXITCODE -eq 0) { |
| 207 | + Write-Host " Done." -ForegroundColor Green |
| 208 | + } else { |
| 209 | + Write-Host " Failed: $result" -ForegroundColor Red |
| 210 | + $errors++ |
| 211 | + } |
| 212 | + } catch { |
| 213 | + Write-Host " Error: $($_.Exception.Message)" -ForegroundColor Red |
| 214 | + $errors++ |
| 215 | + } finally { |
| 216 | + if (Test-Path $tempFile) { |
| 217 | + Remove-Item $tempFile -Force |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +Write-Host "" |
| 224 | + |
| 225 | +if ($errors -gt 0) { |
| 226 | + Write-Host "$errors action(s) failed. Review the errors above." -ForegroundColor Red |
| 227 | + exit 1 |
| 228 | +} else { |
| 229 | + Write-Host "All changes applied successfully." -ForegroundColor Green |
| 230 | + Write-Host "" |
| 231 | + Write-Host "Next step: Run .\Setup-BranchRuleset.ps1 to create a fresh ruleset." -ForegroundColor Cyan |
| 232 | + Write-Host "View rulesets at: https://github.com/$Repository/settings/rules" -ForegroundColor Cyan |
| 233 | +} |
0 commit comments