Overview
Theprotect-repo command applies comprehensive security protections to a GitHub repository, including repository-level settings and branch protection rulesets. It enforces pyrig’s opinionated security defaults to maintain code quality and prevent accidental destructive operations.
What It Does
The command configures two levels of protection:Repository Settings
- Sets repository description from
pyproject.toml - Sets default branch to
main - Enables “delete branch on merge”
- Disables merge commits (enforces squash and rebase only)
Branch Protection Rules
- Requires pull request reviews with code owner approval
- Requires status checks to pass (health check workflow)
- Requires linear commit history
- Requires signed commits
- Disables force pushes
- Disables branch deletions
The command is idempotent: safe to run multiple times, updates existing rulesets instead of creating duplicates.
Usage
Basic Usage
Using .env File
With Verbose Output
Quiet Mode
Expected Output
Prerequisites
GitHub Personal Access Token
Create a token withrepo scope:
Go to GitHub Settings
Navigate to Settings → Developer settings → Personal access tokens → Tokens (classic)
Protection Rules
Rules are loaded frombranch-protection.json in your project root:
Customizing Protection Rules
To customize the protection rules:Behavior
Checks
REPO_TOKEN in environment variables first, then falls back to .env file.Automatically detects the GitHub repository from Git remote configuration.
Updates existing rulesets instead of creating duplicates. Safe to run multiple times.
Loads protection rules from
branch-protection.json in the project root.Applied Settings
Repository-Level
| Setting | Value | Purpose |
|---|---|---|
| Description | From pyproject.toml | Keeps repo description in sync |
| Default branch | main | Standard default branch |
| Delete branch on merge | Enabled | Keeps repository clean |
| Merge commits | Disabled | Enforces linear history |
Branch Protection
| Rule | Configuration | Purpose |
|---|---|---|
| Pull requests | Required with code owner approval | Ensures code review |
| Status checks | Health check must pass | Ensures CI passes |
| Linear history | Required | Prevents merge commits |
| Signed commits | Required | Ensures commit authenticity |
| Force pushes | Disabled | Prevents history rewriting |
| Branch deletion | Disabled | Protects main branch |
When to Use
Use protect-repo When:
- Setting up a new repository
- Enforcing security standards across an organization
- Updating protection rules after policy changes
- Ensuring compliance with security requirements
- Migrating from manual protection settings
Example Workflow
Troubleshooting
Missing Token Error
REPO_TOKEN environment variable:
Insufficient Permissions
repo scope permissions.
Repository Not Found
Security Considerations
- Token Security: Store
REPO_TOKENsecurely, never commit to version control - Scope Limitation: Use tokens with minimal required scopes
- Token Rotation: Rotate tokens regularly
- Audit Logs: Review GitHub audit logs for protection changes
- Team Access: Ensure team members understand the protection rules
Related Commands
Implementation
Theprotect-repo command calls RepoProtectionConfigFile.I.protect_repo(), which:
- Loads
REPO_TOKENfrom environment or.env - Reads
branch-protection.json - Applies repository settings via GitHub API
- Creates/updates branch protection rulesets
pyrig/rig/cli/commands/protect_repo.py:10.
Run
uv run pyrig protect-repo --help to see the command’s built-in help text.