|
| 1 | +# |
| 2 | +# Setup script for new CodingWithCalvin repositories |
| 3 | +# Run after creating a repo from a template |
| 4 | +# |
| 5 | +# Usage: .\setup-repo.ps1 <repo-name> |
| 6 | +# Example: .\setup-repo.ps1 VS-MyNewExtension |
| 7 | +# |
| 8 | + |
| 9 | +param( |
| 10 | + [Parameter(Mandatory=$true, Position=0)] |
| 11 | + [string]$RepoName |
| 12 | +) |
| 13 | + |
| 14 | +$ErrorActionPreference = "Stop" |
| 15 | + |
| 16 | +$Org = "CodingWithCalvin" |
| 17 | +$FullRepo = "$Org/$RepoName" |
| 18 | + |
| 19 | +Write-Host "Setting up repository: $FullRepo" -ForegroundColor Cyan |
| 20 | +Write-Host "" |
| 21 | + |
| 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 | +} |
| 28 | + |
| 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_auto_merge=false ` |
| 40 | + -F web_commit_signoff_required=false ` |
| 41 | + --silent |
| 42 | + |
| 43 | +Write-Host " - Issues: enabled" -ForegroundColor Green |
| 44 | +Write-Host " - Projects: disabled" -ForegroundColor Green |
| 45 | +Write-Host " - Wiki: disabled" -ForegroundColor Green |
| 46 | +Write-Host " - Discussions: enabled" -ForegroundColor Green |
| 47 | +Write-Host " - Merge commits: disabled" -ForegroundColor Green |
| 48 | +Write-Host " - Rebase merge: disabled" -ForegroundColor Green |
| 49 | +Write-Host " - Squash merge: enabled" -ForegroundColor Green |
| 50 | +Write-Host " - Delete branch on merge: enabled" -ForegroundColor Green |
| 51 | +Write-Host "" |
| 52 | + |
| 53 | +Write-Host "2. Creating branch ruleset..." -ForegroundColor Yellow |
| 54 | + |
| 55 | +$rulesetJson = @' |
| 56 | +{ |
| 57 | + "name": "PRs to Main", |
| 58 | + "target": "branch", |
| 59 | + "enforcement": "active", |
| 60 | + "bypass_actors": [ |
| 61 | + { |
| 62 | + "actor_type": "OrganizationAdmin", |
| 63 | + "bypass_mode": "always" |
| 64 | + } |
| 65 | + ], |
| 66 | + "conditions": { |
| 67 | + "ref_name": { |
| 68 | + "include": ["~DEFAULT_BRANCH"], |
| 69 | + "exclude": [] |
| 70 | + } |
| 71 | + }, |
| 72 | + "rules": [ |
| 73 | + {"type": "deletion"}, |
| 74 | + {"type": "non_fast_forward"}, |
| 75 | + { |
| 76 | + "type": "pull_request", |
| 77 | + "parameters": { |
| 78 | + "required_approving_review_count": 1, |
| 79 | + "dismiss_stale_reviews_on_push": true, |
| 80 | + "require_code_owner_review": false, |
| 81 | + "require_last_push_approval": false, |
| 82 | + "required_review_thread_resolution": false, |
| 83 | + "allowed_merge_methods": ["squash"] |
| 84 | + } |
| 85 | + } |
| 86 | + ] |
| 87 | +} |
| 88 | +'@ |
| 89 | + |
| 90 | +$rulesetJson | gh api "repos/$FullRepo/rulesets" -X POST --input - --silent |
| 91 | + |
| 92 | +Write-Host " - Bypass: Organization Admins" -ForegroundColor Green |
| 93 | +Write-Host " - Prevent deletion: enabled" -ForegroundColor Green |
| 94 | +Write-Host " - Prevent force push: enabled" -ForegroundColor Green |
| 95 | +Write-Host " - Require PR: enabled" -ForegroundColor Green |
| 96 | +Write-Host " - Required approvals: 1" -ForegroundColor Green |
| 97 | +Write-Host " - Dismiss stale reviews: enabled" -ForegroundColor Green |
| 98 | +Write-Host " - Allowed merge: squash only" -ForegroundColor Green |
| 99 | +Write-Host "" |
| 100 | + |
| 101 | +Write-Host "Done! Repository $FullRepo is configured." -ForegroundColor Cyan |
| 102 | +Write-Host "" |
| 103 | +Write-Host "View settings: https://github.com/$FullRepo/settings" |
| 104 | +Write-Host "View ruleset: https://github.com/$FullRepo/settings/rules" |
0 commit comments