|
| 1 | +#!/usr/bin/env bash |
| 2 | +# setup-branch-protection.sh — apply the main-branch ruleset to dedev-llc/rpr. |
| 3 | +# |
| 4 | +# Idempotent: re-running updates the existing ruleset rather than duplicating. |
| 5 | +# |
| 6 | +# WHEN TO RUN: |
| 7 | +# - After flipping the repo from private → public (rulesets are paywalled |
| 8 | +# on private repos with the free GitHub plan). |
| 9 | +# - After adding/removing CI checks in .github/workflows/ci.yml — update |
| 10 | +# the REQUIRED_CHECKS array below to match the new job names. |
| 11 | +# |
| 12 | +# REQUIREMENTS: |
| 13 | +# - gh CLI authenticated as a dedev-llc org admin |
| 14 | +# - The CI workflow (.github/workflows/ci.yml) must already exist on main, |
| 15 | +# otherwise the required status checks will block all PRs forever (no run |
| 16 | +# can ever produce them). |
| 17 | +# |
| 18 | +# WHAT THE RULESET ENFORCES: |
| 19 | +# - No deletion of main |
| 20 | +# - No force pushes (non-fast-forward) to main |
| 21 | +# - Linear history (no merge commits inside PRs — squash/rebase only) |
| 22 | +# - All changes go through a pull request |
| 23 | +# - PR conversations must be resolved before merge |
| 24 | +# - Stale review approvals are dismissed when new commits are pushed |
| 25 | +# - All required CI checks must pass before merge |
| 26 | +# - Org admins can bypass in an emergency (configurable below) |
| 27 | + |
| 28 | +set -euo pipefail |
| 29 | + |
| 30 | +REPO="dedev-llc/rpr" |
| 31 | +RULESET_NAME="main-protection" |
| 32 | + |
| 33 | +# Required CI status check job names — must match the `name:` (or job key when |
| 34 | +# no name is set) of the jobs in .github/workflows/ci.yml. For matrix jobs, |
| 35 | +# include each variant explicitly. |
| 36 | +REQUIRED_CHECKS=( |
| 37 | + "python (3.9)" |
| 38 | + "python (3.10)" |
| 39 | + "python (3.11)" |
| 40 | + "python (3.12)" |
| 41 | + "version-sync" |
| 42 | + "json-lint" |
| 43 | + "shellcheck" |
| 44 | + "npm-pack" |
| 45 | +) |
| 46 | + |
| 47 | +# Build the JSON array of required status checks. |
| 48 | +contexts_json=$(printf '%s\n' "${REQUIRED_CHECKS[@]}" \ |
| 49 | + | python3 -c ' |
| 50 | +import json, sys |
| 51 | +checks = [{"context": line.strip()} for line in sys.stdin if line.strip()] |
| 52 | +print(json.dumps(checks)) |
| 53 | +') |
| 54 | + |
| 55 | +# Build the full ruleset payload. Heredoc + envsubst-style substitution would |
| 56 | +# be cleaner but we want zero dependencies — using python for JSON assembly. |
| 57 | +ruleset_json=$(CONTEXTS="$contexts_json" python3 - <<'PY' |
| 58 | +import json, os |
| 59 | +payload = { |
| 60 | + "name": "main-protection", |
| 61 | + "target": "branch", |
| 62 | + "enforcement": "active", |
| 63 | + "conditions": { |
| 64 | + "ref_name": { |
| 65 | + "include": ["~DEFAULT_BRANCH"], |
| 66 | + "exclude": [], |
| 67 | + } |
| 68 | + }, |
| 69 | + "rules": [ |
| 70 | + {"type": "deletion"}, |
| 71 | + {"type": "non_fast_forward"}, |
| 72 | + {"type": "required_linear_history"}, |
| 73 | + { |
| 74 | + "type": "pull_request", |
| 75 | + "parameters": { |
| 76 | + "required_approving_review_count": 0, |
| 77 | + "dismiss_stale_reviews_on_push": True, |
| 78 | + "require_code_owner_review": False, |
| 79 | + "require_last_push_approval": False, |
| 80 | + "required_review_thread_resolution": True, |
| 81 | + "allowed_merge_methods": ["squash", "rebase"], |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + "type": "required_status_checks", |
| 86 | + "parameters": { |
| 87 | + "strict_required_status_checks_policy": False, |
| 88 | + "required_status_checks": json.loads(os.environ["CONTEXTS"]), |
| 89 | + }, |
| 90 | + }, |
| 91 | + ], |
| 92 | + "bypass_actors": [ |
| 93 | + # OrganizationAdmin = anyone with org admin role on dedev-llc. |
| 94 | + # bypass_mode "always" means they can bypass without going through |
| 95 | + # the normal flow (e.g. emergency direct push). Change to "pull_request" |
| 96 | + # to require admins to still go through a PR but skip the checks. |
| 97 | + { |
| 98 | + "actor_id": 1, |
| 99 | + "actor_type": "OrganizationAdmin", |
| 100 | + "bypass_mode": "always", |
| 101 | + } |
| 102 | + ], |
| 103 | +} |
| 104 | +print(json.dumps(payload, indent=2)) |
| 105 | +PY |
| 106 | +) |
| 107 | + |
| 108 | +echo "Target repo: $REPO" |
| 109 | +echo "Ruleset name: $RULESET_NAME" |
| 110 | +echo "Required checks:" |
| 111 | +printf ' - %s\n' "${REQUIRED_CHECKS[@]}" |
| 112 | +echo |
| 113 | + |
| 114 | +# Look for an existing ruleset with the same name. |
| 115 | +existing_id=$(gh api "repos/$REPO/rulesets" --jq ".[] | select(.name == \"$RULESET_NAME\") | .id" 2>/dev/null || true) |
| 116 | + |
| 117 | +if [ -n "$existing_id" ]; then |
| 118 | + echo "Updating existing ruleset (id=$existing_id)..." |
| 119 | + printf '%s' "$ruleset_json" | gh api -X PUT "repos/$REPO/rulesets/$existing_id" --input - > /dev/null |
| 120 | + echo "✓ Ruleset updated" |
| 121 | +else |
| 122 | + echo "Creating new ruleset..." |
| 123 | + printf '%s' "$ruleset_json" | gh api -X POST "repos/$REPO/rulesets" --input - > /dev/null |
| 124 | + echo "✓ Ruleset created" |
| 125 | +fi |
| 126 | + |
| 127 | +echo |
| 128 | +echo "View at: https://github.com/$REPO/rules" |
0 commit comments