|
1 | 1 | # |
2 | | -# Setup script for new CodingWithCalvin repositories |
3 | | -# Run after creating a repo from a template |
| 2 | +# Setup script for CodingWithCalvin repositories |
| 3 | +# Configures repository settings and branch rulesets |
4 | 4 | # |
5 | | -# Usage: .\setup-repo.ps1 <repo-name> |
6 | | -# Example: .\setup-repo.ps1 VS-MyNewExtension |
| 5 | +# Usage: |
| 6 | +# .\setup-repo.ps1 <repo-name> # Configure a single repo |
| 7 | +# .\setup-repo.ps1 -All # Configure all repos in the org |
| 8 | +# |
| 9 | +# Examples: |
| 10 | +# .\setup-repo.ps1 VS-MyNewExtension |
| 11 | +# .\setup-repo.ps1 -All |
7 | 12 | # |
8 | 13 |
|
9 | 14 | param( |
10 | | - [Parameter(Mandatory=$true, Position=0)] |
11 | | - [string]$RepoName |
| 15 | + [Parameter(Position=0)] |
| 16 | + [string]$RepoName, |
| 17 | + |
| 18 | + [Parameter()] |
| 19 | + [switch]$All |
12 | 20 | ) |
13 | 21 |
|
14 | 22 | $ErrorActionPreference = "Stop" |
15 | | - |
16 | 23 | $Org = "CodingWithCalvin" |
17 | | -$FullRepo = "$Org/$RepoName" |
18 | 24 |
|
19 | | -Write-Host "Setting up repository: $FullRepo" -ForegroundColor Cyan |
20 | | -Write-Host "" |
| 25 | +function Setup-Repository { |
| 26 | + param( |
| 27 | + [string]$FullRepo |
| 28 | + ) |
21 | 29 |
|
22 | | -# Check if repo exists |
23 | | -$repoCheck = gh repo view $FullRepo 2>&1 |
24 | | -if ($LASTEXITCODE -ne 0) { |
25 | | - Write-Host "Error: Repository $FullRepo not found" -ForegroundColor Red |
26 | | - exit 1 |
27 | | -} |
| 30 | + Write-Host "Setting up repository: $FullRepo" -ForegroundColor Cyan |
| 31 | + Write-Host "" |
| 32 | + |
| 33 | + Write-Host "1. Configuring repository settings..." -ForegroundColor Yellow |
| 34 | + gh api "repos/$FullRepo" -X PATCH ` |
| 35 | + -F has_issues=true ` |
| 36 | + -F has_projects=false ` |
| 37 | + -F has_wiki=false ` |
| 38 | + -F has_discussions=true ` |
| 39 | + -F has_sponsorships=true ` |
| 40 | + -F allow_squash_merge=true ` |
| 41 | + -F allow_merge_commit=false ` |
| 42 | + -F allow_rebase_merge=false ` |
| 43 | + -F delete_branch_on_merge=true ` |
| 44 | + -F allow_update_branch=true ` |
| 45 | + -F allow_auto_merge=false ` |
| 46 | + -F web_commit_signoff_required=false ` |
| 47 | + --silent |
| 48 | + |
| 49 | + Write-Host " - Issues: enabled" -ForegroundColor Green |
| 50 | + Write-Host " - Projects: disabled" -ForegroundColor Green |
| 51 | + Write-Host " - Wiki: disabled" -ForegroundColor Green |
| 52 | + Write-Host " - Discussions: enabled" -ForegroundColor Green |
| 53 | + Write-Host " - Sponsorships: enabled" -ForegroundColor Green |
| 54 | + Write-Host " - Merge commits: disabled" -ForegroundColor Green |
| 55 | + Write-Host " - Rebase merge: disabled" -ForegroundColor Green |
| 56 | + Write-Host " - Squash merge: enabled" -ForegroundColor Green |
| 57 | + Write-Host " - Delete branch on merge: enabled" -ForegroundColor Green |
| 58 | + Write-Host " - Suggest updating PR branches: enabled" -ForegroundColor Green |
| 59 | + Write-Host "" |
| 60 | + |
| 61 | + Write-Host "2. Creating branch ruleset..." -ForegroundColor Yellow |
28 | 62 |
|
29 | | -Write-Host "1. Configuring repository settings..." -ForegroundColor Yellow |
30 | | -gh api "repos/$FullRepo" -X PATCH ` |
31 | | - -F has_issues=true ` |
32 | | - -F has_projects=false ` |
33 | | - -F has_wiki=false ` |
34 | | - -F has_discussions=true ` |
35 | | - -F allow_squash_merge=true ` |
36 | | - -F allow_merge_commit=false ` |
37 | | - -F allow_rebase_merge=false ` |
38 | | - -F delete_branch_on_merge=true ` |
39 | | - -F allow_update_branch=true ` |
40 | | - -F allow_auto_merge=false ` |
41 | | - -F web_commit_signoff_required=false ` |
42 | | - --silent |
43 | | - |
44 | | -Write-Host " - Issues: enabled" -ForegroundColor Green |
45 | | -Write-Host " - Projects: disabled" -ForegroundColor Green |
46 | | -Write-Host " - Wiki: disabled" -ForegroundColor Green |
47 | | -Write-Host " - Discussions: enabled" -ForegroundColor Green |
48 | | -Write-Host " - Merge commits: disabled" -ForegroundColor Green |
49 | | -Write-Host " - Rebase merge: disabled" -ForegroundColor Green |
50 | | -Write-Host " - Squash merge: enabled" -ForegroundColor Green |
51 | | -Write-Host " - Delete branch on merge: enabled" -ForegroundColor Green |
52 | | -Write-Host " - Suggest updating PR branches: enabled" -ForegroundColor Green |
53 | | -Write-Host "" |
54 | | - |
55 | | -Write-Host "2. Creating branch ruleset..." -ForegroundColor Yellow |
56 | | - |
57 | | -$rulesetJson = @' |
| 63 | + # Check if ruleset already exists |
| 64 | + $existingRulesets = gh api "repos/$FullRepo/rulesets" 2>$null | ConvertFrom-Json |
| 65 | + $existingRuleset = $existingRulesets | Where-Object { $_.name -eq "PRs to Main" } |
| 66 | + |
| 67 | + if ($existingRuleset) { |
| 68 | + Write-Host " - Ruleset 'PRs to Main' already exists, skipping..." -ForegroundColor Yellow |
| 69 | + } else { |
| 70 | + $rulesetJson = @' |
58 | 71 | { |
59 | 72 | "name": "PRs to Main", |
60 | 73 | "target": "branch", |
@@ -89,18 +102,89 @@ $rulesetJson = @' |
89 | 102 | } |
90 | 103 | '@ |
91 | 104 |
|
92 | | -$rulesetJson | gh api "repos/$FullRepo/rulesets" -X POST --input - --silent |
93 | | - |
94 | | -Write-Host " - Bypass: Organization Admins" -ForegroundColor Green |
95 | | -Write-Host " - Prevent deletion: enabled" -ForegroundColor Green |
96 | | -Write-Host " - Prevent force push: enabled" -ForegroundColor Green |
97 | | -Write-Host " - Require PR: enabled" -ForegroundColor Green |
98 | | -Write-Host " - Required approvals: 1" -ForegroundColor Green |
99 | | -Write-Host " - Dismiss stale reviews: enabled" -ForegroundColor Green |
100 | | -Write-Host " - Allowed merge: squash only" -ForegroundColor Green |
101 | | -Write-Host "" |
102 | | - |
103 | | -Write-Host "Done! Repository $FullRepo is configured." -ForegroundColor Cyan |
104 | | -Write-Host "" |
105 | | -Write-Host "View settings: https://github.com/$FullRepo/settings" |
106 | | -Write-Host "View ruleset: https://github.com/$FullRepo/settings/rules" |
| 105 | + $rulesetJson | gh api "repos/$FullRepo/rulesets" -X POST --input - --silent |
| 106 | + |
| 107 | + Write-Host " - Bypass: Organization Admins" -ForegroundColor Green |
| 108 | + Write-Host " - Prevent deletion: enabled" -ForegroundColor Green |
| 109 | + Write-Host " - Prevent force push: enabled" -ForegroundColor Green |
| 110 | + Write-Host " - Require PR: enabled" -ForegroundColor Green |
| 111 | + Write-Host " - Required approvals: 1" -ForegroundColor Green |
| 112 | + Write-Host " - Dismiss stale reviews: enabled" -ForegroundColor Green |
| 113 | + Write-Host " - Allowed merge: squash only" -ForegroundColor Green |
| 114 | + } |
| 115 | + Write-Host "" |
| 116 | + |
| 117 | + Write-Host "3. Disabling CodeQL..." -ForegroundColor Yellow |
| 118 | + $codeqlResult = gh api "repos/$FullRepo/code-scanning/default-setup" -X PATCH -F state=not-configured 2>&1 |
| 119 | + if ($LASTEXITCODE -eq 0) { |
| 120 | + Write-Host " - CodeQL default setup: disabled" -ForegroundColor Green |
| 121 | + } else { |
| 122 | + Write-Host " - CodeQL: not configured or already disabled" -ForegroundColor Yellow |
| 123 | + } |
| 124 | + Write-Host "" |
| 125 | + |
| 126 | + Write-Host "4. Disabling Dependabot..." -ForegroundColor Yellow |
| 127 | + gh api "repos/$FullRepo/vulnerability-alerts" -X DELETE 2>$null |
| 128 | + Write-Host " - Vulnerability alerts: disabled" -ForegroundColor Green |
| 129 | + gh api "repos/$FullRepo/automated-security-fixes" -X DELETE 2>$null |
| 130 | + Write-Host " - Automated security fixes: disabled" -ForegroundColor Green |
| 131 | + Write-Host "" |
| 132 | + |
| 133 | + Write-Host "Done! Repository $FullRepo is configured." -ForegroundColor Cyan |
| 134 | + Write-Host "" |
| 135 | +} |
| 136 | + |
| 137 | +# Main logic |
| 138 | +if ($All) { |
| 139 | + Write-Host "Fetching all repositories in $Org..." -ForegroundColor Cyan |
| 140 | + Write-Host "" |
| 141 | + |
| 142 | + $repos = gh repo list $Org --limit 500 --no-archived --visibility public --json name --jq '.[].name' | Sort-Object |
| 143 | + |
| 144 | + if (-not $repos) { |
| 145 | + Write-Host "Error: No repositories found or unable to fetch repos" -ForegroundColor Red |
| 146 | + exit 1 |
| 147 | + } |
| 148 | + |
| 149 | + $repoList = $repos -split "`n" | Where-Object { $_ -ne "" } |
| 150 | + $total = $repoList.Count |
| 151 | + $current = 0 |
| 152 | + |
| 153 | + Write-Host "Found $total repositories" -ForegroundColor Cyan |
| 154 | + Write-Host "========================================" -ForegroundColor Cyan |
| 155 | + Write-Host "" |
| 156 | + |
| 157 | + foreach ($repo in $repoList) { |
| 158 | + $current++ |
| 159 | + Write-Host "[$current/$total] " -NoNewline -ForegroundColor Magenta |
| 160 | + Setup-Repository -FullRepo "$Org/$repo" |
| 161 | + Write-Host "----------------------------------------" -ForegroundColor DarkGray |
| 162 | + Write-Host "" |
| 163 | + } |
| 164 | + |
| 165 | + Write-Host "========================================" -ForegroundColor Cyan |
| 166 | + Write-Host "All $total repositories have been configured!" -ForegroundColor Green |
| 167 | +} elseif ($RepoName) { |
| 168 | + $FullRepo = "$Org/$RepoName" |
| 169 | + |
| 170 | + # Check if repo exists |
| 171 | + $repoCheck = gh repo view $FullRepo 2>&1 |
| 172 | + if ($LASTEXITCODE -ne 0) { |
| 173 | + Write-Host "Error: Repository $FullRepo not found" -ForegroundColor Red |
| 174 | + exit 1 |
| 175 | + } |
| 176 | + |
| 177 | + Setup-Repository -FullRepo $FullRepo |
| 178 | + |
| 179 | + Write-Host "View settings: https://github.com/$FullRepo/settings" |
| 180 | + Write-Host "View ruleset: https://github.com/$FullRepo/settings/rules" |
| 181 | +} else { |
| 182 | + Write-Host "Usage:" -ForegroundColor Yellow |
| 183 | + Write-Host " .\setup-repo.ps1 <repo-name> # Configure a single repo" |
| 184 | + Write-Host " .\setup-repo.ps1 -All # Configure all repos in the org" |
| 185 | + Write-Host "" |
| 186 | + Write-Host "Examples:" |
| 187 | + Write-Host " .\setup-repo.ps1 VS-MyNewExtension" |
| 188 | + Write-Host " .\setup-repo.ps1 -All" |
| 189 | + exit 1 |
| 190 | +} |
0 commit comments