From 937c2f9f69218be5ba442bfbd8064a7f9034450c Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 16 Oct 2026 12:18:07 +0000 Subject: [PATCH] Gate main on one ci-passed check, and script its protection Pasteup is to carry the same branch ruleset as the sigrix-io public repositories, which requires a single aggregate check, `ci-passed`, rather than each job by name. The workflow gains that job, copied from sigrix-io/mullion: it runs after the test matrix whatever the outcome and fails if any leg failed or was cancelled, so changing the matrix never means editing the ruleset. scripts/protect-repo.sh applies, through `gh api`: - sigrix-io/mullion's "default-branch" ruleset: no deletion or force-push of the default branch; changes only through pull requests, with no approval required; merging needs `ci-passed` green from GitHub Actions and the branch up to date. Its bypass actor, a sigrix team, becomes repository admins in the same pull-request-only mode, since a personal repository has no teams. - The same merge settings as mullion, which differ from the defaults only by deleting branches once merged. - Dependabot alerts, and once the repository is public, private vulnerability reporting (which SECURITY.md points to), secret scanning and push protection. It is safe to re-run: the ruleset is updated in place. On GitHub Free a private repository cannot have rulesets, and the script says so and asks to be run again right after the repository goes public. Tested against a fake `gh` that serves canned API responses and runs the script's own jq filters: a private repository on Free (refused at creation and at listing), on a paid plan, a re-run after going public, a validation error, and no access. The ruleset sent equals mullion's except for the documented bypass change and the omitted default-valued review parameters. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01E6aBDEnU4vqKQ4F9cnJMrL --- .github/workflows/tests.yml | 13 ++++ README.md | 5 ++ scripts/protect-repo.sh | 145 ++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100755 scripts/protect-repo.sh diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 16307ab..205b6c2 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -44,3 +44,16 @@ jobs: - name: Test run: pytest -rs + + # The aggregate gate, as in sigrix-io/mullion: the one check the branch + # ruleset requires on main, so changing the matrix or adding a job does not + # mean editing the ruleset to match. + ci-passed: + if: always() + needs: [test] + runs-on: ubuntu-latest + steps: + - name: Every job succeeded + run: | + echo '${{ toJSON(needs) }}' + [ "${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}" = "false" ] diff --git a/README.md b/README.md index 366fa07..ba13bee 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,11 @@ pytest ``` CI runs the same on Python 3.11 and 3.13, against a regular install of the package and with ffmpeg present, so the preview-video tests encode real files. +Its `ci-passed` job succeeds only when every other job does, and is the one +check `main` requires. + +`scripts/protect-repo.sh` applies the repository's branch ruleset and security +settings through the GitHub CLI; it needs `gh` signed in as a repository admin. Project layout: - `src/pasteup/main.py`: the FastAPI app diff --git a/scripts/protect-repo.sh b/scripts/protect-repo.sh new file mode 100755 index 0000000..668f142 --- /dev/null +++ b/scripts/protect-repo.sh @@ -0,0 +1,145 @@ +#!/usr/bin/env bash +# Apply the branch protection sigrix-io uses on its public repositories, plus +# the security settings a public repository should have. +# +# scripts/protect-repo.sh [owner/repo] (default: ftymoshchuk/pasteup) +# +# Needs the GitHub CLI signed in as an admin of the repository (`gh auth +# status`). Safe to re-run: the ruleset is updated in place and every setting +# is idempotent. Run it again right after making the repository public, since +# some of these settings only exist for public repositories. +set -euo pipefail + +repo="${1:-ftymoshchuk/pasteup}" +ruleset_name="default-branch" +ruleset_id_query=".[] | select(.name == \"$ruleset_name\" and .source_type == \"Repository\") | .id" + +# sigrix-io/mullion's "default-branch" ruleset: the default branch cannot be +# deleted or force-pushed, changes land only through pull requests (no +# approval needed), and a pull request merges only when the `ci-passed` check +# is green and the branch is up to date with main. 15368 is the GitHub Actions +# app, so nothing else can report that check. +# +# Two differences from the sigrix original: +# - Its bypass actor is a sigrix team, and a personal repository has no teams. +# Repository admins (built-in role 5) take its place, with the same limit: +# they can bypass only through a pull request, never by pushing directly. +# - Review parameters it leaves at their defaults (required reviewers, +# dismissal restrictions) are left out rather than repeated. +ruleset=$(cat <<'JSON' +{ + "name": "default-branch", + "target": "branch", + "enforcement": "active", + "conditions": { + "ref_name": { "include": ["~DEFAULT_BRANCH"], "exclude": [] } + }, + "rules": [ + { "type": "deletion" }, + { "type": "non_fast_forward" }, + { + "type": "pull_request", + "parameters": { + "required_approving_review_count": 0, + "dismiss_stale_reviews_on_push": false, + "require_code_owner_review": false, + "require_last_push_approval": false, + "required_review_thread_resolution": false, + "allowed_merge_methods": ["merge", "squash", "rebase"] + } + }, + { + "type": "required_status_checks", + "parameters": { + "strict_required_status_checks_policy": true, + "do_not_enforce_on_create": false, + "required_status_checks": [{ "context": "ci-passed", "integration_id": 15368 }] + } + } + ], + "bypass_actors": [ + { "actor_id": 5, "actor_type": "RepositoryRole", "bypass_mode": "pull_request" } + ] +} +JSON +) + +die() { + echo "error: $*" >&2 + exit 1 +} + +explain_ruleset_failure() { + echo "Ruleset not applied: $1" >&2 + if [ "$visibility" = private ] && grep -q "HTTP 403" <<<"$1"; then + echo "On GitHub Free, rulesets work only on public repositories. Make $repo public, then" >&2 + echo "run this script again straight away. Going public gives nobody push access, so" >&2 + echo "the minute in between exposes nothing." >&2 + fi +} + +apply_ruleset() { + local existing_id output + if ! existing_id=$(gh api "repos/$repo/rulesets" --jq "$ruleset_id_query" 2>&1); then + explain_ruleset_failure "$existing_id" + return 1 + fi + + if [ -n "$existing_id" ]; then + output=$(gh api --method PUT "repos/$repo/rulesets/$existing_id" --input - <<<"$ruleset" 2>&1) || { + explain_ruleset_failure "$output" + return 1 + } + echo "Updated the '$ruleset_name' ruleset" + else + output=$(gh api --method POST "repos/$repo/rulesets" --input - <<<"$ruleset" 2>&1) || { + explain_ruleset_failure "$output" + return 1 + } + echo "Created the '$ruleset_name' ruleset" + fi +} + +command -v gh >/dev/null || die "the GitHub CLI (gh) is not installed" +visibility=$(gh api "repos/$repo" --jq .visibility) || die "cannot read $repo; check the name and \`gh auth status\`" +echo "$repo is $visibility" + +ruleset_applied=true +apply_ruleset || ruleset_applied=false + +# The same merge settings as sigrix: all three merge methods, no auto-merge, +# and branches deleted once their pull request merges. +gh api --method PATCH "repos/$repo" \ + -F allow_merge_commit=true -F allow_squash_merge=true -F allow_rebase_merge=true \ + -F allow_auto_merge=false -F delete_branch_on_merge=true >/dev/null +echo "Merge settings: merge, squash and rebase allowed; merged branches are deleted" + +gh api --method PUT "repos/$repo/vulnerability-alerts" >/dev/null +echo "Dependabot alerts enabled" + +if [ "$visibility" = public ]; then + # SECURITY.md tells reporters to use this. + gh api --method PUT "repos/$repo/private-vulnerability-reporting" >/dev/null + echo "Private vulnerability reporting enabled" + + gh api --method PATCH "repos/$repo" --input - >/dev/null <<'JSON' +{ + "security_and_analysis": { + "secret_scanning": { "status": "enabled" }, + "secret_scanning_push_protection": { "status": "enabled" } + } +} +JSON + echo "Secret scanning and push protection enabled" +else + echo "Still private: run this again after making it public, to enable private vulnerability" + echo "reporting, secret scanning and push protection, which exist only for public repositories." +fi + +if [ "$ruleset_applied" = true ]; then + ruleset_id=$(gh api "repos/$repo/rulesets" --jq "$ruleset_id_query") + gh api "repos/$repo/rulesets/$ruleset_id" --jq \ + '"In force: \(.name) (\(.enforcement)), rules: \([.rules[].type] | join(", "))"' +else + exit 2 +fi