|
| 1 | +# Branch-Protection für `main` |
| 2 | + |
| 3 | +Die GitHub-Settings sind nicht Teil des Repository-Codes und können deshalb |
| 4 | +nicht per PR eingespielt werden. Dieser Leitfaden zeigt, wie der Owner die |
| 5 | +empfohlenen Schutzregeln setzt — entweder per `gh` CLI oder im Web-UI. |
| 6 | + |
| 7 | +## Empfohlenes Regelset |
| 8 | + |
| 9 | +Für den `main`-Branch: |
| 10 | + |
| 11 | +1. **Require pull request before merging** — direkte Pushes blockiert |
| 12 | + - Required reviews: 1 (bei Solo-Maintainer: self-review genügt nicht, aber |
| 13 | + die Owner-Overrides funktionieren) |
| 14 | + - Dismiss stale approvals on new commits: on |
| 15 | +2. **Require status checks to pass before merging** — CI muss grün sein |
| 16 | + - Alle Jobs aus `backend.yml`, `frontend.yml`, `scripts.yml`, `pre-commit.yml` |
| 17 | +3. **Require branches to be up to date before merging** — kein veralteter Branch-Merge |
| 18 | +4. **Do not allow bypassing the above settings** — auch für Admins |
| 19 | +5. **Restrict who can push to matching branches** — nur Maintainer-Team |
| 20 | +6. **Allow force pushes** — **off** |
| 21 | +7. **Allow deletions** — **off** |
| 22 | + |
| 23 | +## gh-CLI Setup |
| 24 | + |
| 25 | +Die `gh api`-Aufrufe unten legen die Regeln an. `REPO` muss als |
| 26 | +`owner/repo` gesetzt sein (`gh repo view --json nameWithOwner -q .nameWithOwner`). |
| 27 | + |
| 28 | +```bash |
| 29 | +REPO="makanakoeln/orbvis" |
| 30 | + |
| 31 | +gh api -X PUT "repos/${REPO}/branches/main/protection" \ |
| 32 | + -H "Accept: application/vnd.github+json" \ |
| 33 | + -F 'required_status_checks[strict]=true' \ |
| 34 | + -F 'required_status_checks[contexts][]=backend' \ |
| 35 | + -F 'required_status_checks[contexts][]=docker-build-backend' \ |
| 36 | + -F 'required_status_checks[contexts][]=frontend' \ |
| 37 | + -F 'required_status_checks[contexts][]=shellcheck' \ |
| 38 | + -F 'required_status_checks[contexts][]=pre-commit' \ |
| 39 | + -F 'enforce_admins=true' \ |
| 40 | + -F 'required_pull_request_reviews[required_approving_review_count]=1' \ |
| 41 | + -F 'required_pull_request_reviews[dismiss_stale_reviews]=true' \ |
| 42 | + -F 'restrictions=' \ |
| 43 | + -F 'allow_force_pushes=false' \ |
| 44 | + -F 'allow_deletions=false' |
| 45 | +``` |
| 46 | + |
| 47 | +## Web-UI-Äquivalent |
| 48 | + |
| 49 | +`Settings → Branches → Branch protection rules → Add rule`: |
| 50 | + |
| 51 | +- **Branch name pattern:** `main` |
| 52 | +- Aktiviere alles aus dem Regelset oben. |
| 53 | +- Die Checks-Namen erscheinen erst, nachdem der jeweilige Workflow einmal |
| 54 | + auf `main` gelaufen ist — sonst ist die Auswahlliste leer. |
| 55 | + |
| 56 | +## Prüfen |
| 57 | + |
| 58 | +```bash |
| 59 | +gh api "repos/${REPO}/branches/main/protection" | jq |
| 60 | +``` |
| 61 | + |
| 62 | +sollte die gesetzten Regeln zurückgeben (keine 404). |
0 commit comments