branch-protection.json
Branch protection ruleset configuration for GitHub repository protection.Overview
The branch protection configuration file defines the ruleset that protects your repository’s main branch. This file is automatically generated by pyrig and used by theprotect-repo command to create or update GitHub branch protection rules.
The protect-repo command loads this file and applies it to GitHub via the API. It passes the JSON as a dict to the API, so any adjustments must align with GitHub’s REST API schema. To customize, manually configure settings in GitHub, export the ruleset, and use that structure when subclassing RepoProtectionConfigFile.
Inheritance
Inherits from:JsonConfigFile
What this means:
- Uses JSON format for configuration
- Loads/dumps with Python’s
jsonmodule - Validation ensures expected configuration is present in the actual file
- File can be manually uploaded to GitHub
- Configuration is passed directly to GitHub’s REST API
File Location
Path:branch-protection.json (project root)
Extension: .json - JSON configuration file.
Purpose
This configuration file serves as a declarative definition of your repository’s branch protection rules. Instead of hardcoding protection settings in Python, pyrig generates a JSON file that:- Matches GitHub’s ruleset export/import format
- Can be manually uploaded to GitHub if needed
- Provides transparency into protection rules
Configuration Structure
The file contains a complete GitHub ruleset definition:Key Configuration Elements
Ruleset Name
Identifies the ruleset in GitHub. This is the name that appears in Settings → Rules → Rulesets.
Target
Applies rules to branches (alternatives:
"tag" for tags, "push" for push events).Enforcement
Rules are enforced. Alternatives:
"disabled": Rules not enforced"evaluate": Dry-run mode (logs violations without blocking)
Conditions
Applies to the default branch (usually
main or master). Use ~DEFAULT_BRANCH to automatically target the repository’s default branch.Branches to exclude from the ruleset. Empty by default.
Rules
Creation/Update/Deletion Protection
- creation: Blocks creating branches matching the pattern
- update: Blocks direct pushes to the branch
- deletion: Prevents branch deletion
Required Linear History
Required Signatures
Pull Request Requirements
Minimum number of approving reviews required to merge.
Dismisses existing approvals when new commits are pushed.
Requires approval from code owners (defined in CODEOWNERS file).
Requires that the last push be approved (prevents self-merge).
All review comment threads must be resolved before merging.
Only squash and rebase merges allowed (no merge commits).
Required Status Checks
Branch must be up to date with base branch before merging.
Status checks not required when creating the branch (only on merge).
List of required status checks. Pyrig requires the
health_check job from the health check workflow to pass.Non-Fast-Forward Protection
Bypass Actors
GitHub’s standard ID for repository admins.
Type of actor (alternatives:
"User", "Team", "OrganizationAdmin").Repository admins can always bypass all rules.
How It Works
Automatic Generation
When initialized viauv run pyrig mkroot, the branch-protection.json file is created by:
- Generating ruleset configuration:
RepoProtectionConfigFile.I.configs()creates the complete ruleset - Setting required status checks: Uses health check workflow job IDs
- Configuring bypass actors: Adds repository admin bypass permissions
- Applying security defaults: Enforces pyrig’s opinionated protection rules
- Health check workflow job IDs for required status checks
- Standard GitHub actor IDs for bypass permissions (actor_id: 5 = Repository admins)
- Pyrig’s opinionated security defaults
Application to GitHub
Theprotect-repo command loads this file and applies it to GitHub:
- Loads
branch-protection.jsonusingRepoProtectionConfigFile.I.load() - Checks if a ruleset with the same name exists
- Creates or updates the ruleset via GitHub API
- Applies all protection rules to the main branch
Usage
Automatic Creation
The file is automatically created when you initialize your project:branch-protection.json with pyrig’s default protection rules.
Applying to GitHub
The protection rules are automatically applied by the health check workflow. To manually apply:REPO_TOKEN secret to be configured in your repository.
Manual Upload to GitHub
You can also manually upload this file to GitHub:- Go to repository Settings → Rules → Rulesets
- Click “New ruleset” → “Import a ruleset”
- Upload
branch-protection.json - Review and create
protect-repo command.
Customization
To customize branch protection rules, subclassRepoProtectionConfigFile:
myapp/rig/configs/git/branch_protection.py and it will override the default configuration.
Verifying Configuration
After applying, verify the ruleset in GitHub after the CI/CD pipeline runs and activated it with the commanduv run pyrig protect-repo:
- Go to repository Settings → Rules → Rulesets
- Find the
main-protectionruleset - Review the applied rules
Best Practices
- Don’t modify the file directly: Use subclassing to customize rules instead of editing the generated file
- Align with GitHub’s schema: If you manually edit the JSON, ensure it matches GitHub’s ruleset API schema
- Export from GitHub for reference: Manually configure rules in GitHub UI, export the ruleset, and use that structure for customization
- Test in a separate repository: Test custom protection rules in a test repository before applying to production
- Keep bypass actors minimal: Only allow repository admins to bypass rules
- Use the CI/CD pipeline: Let the health check workflow apply protection automatically instead of manual uploads