Sub topic 8
Rulesets and the rules they can enforce
This chapter assumes you know basic Git, GitHub pull requests, and the idea of required status checks. It also assumes you have admin access to a repository you can experiment in. You should have seen classic branch protection rules in earlier chapters.
What a ruleset is
A ruleset is a named collection of rules that GitHub applies to a set of branches or tags. Classic branch protection attaches one rule to one branch name pattern. A ruleset separates three questions:
- Which refs does it cover? This is the target and its conditions.
- What does it enforce? This is the list of rules.
- Who may skip it, and is it switched on? This is the bypass list and the enforcement status.
This chapter covers the middle question. Later chapters cover targeting and enforcement.
Rule types
Each entry in the rules array has a type. Some types take parameters.
| Rule type | What it enforces |
|---|---|
| `deletion` | Blocks deleting the matching refs. |
| `non_fast_forward` | Blocks force pushes. |
| `pull_request` | Requires changes to arrive through a pull request, with review settings. |
| `required_status_checks` | Requires named checks to pass before merging. |
| `required_linear_history` | Allows only squash or rebase merges. |
| `required_signatures` | Requires verified signed commits. |
Rulesets also offer rules that classic protection lacks, such as restricting branch or tag names and file paths, which later chapters touch on.
A working example
This request body for POST /repos/{owner}/{repo}/rulesets protects the default branch. Its enforcement is evaluate, so GitHub records what would be blocked without blocking anything.
{
"name": "protect-default-branch",
"target": "branch",
"enforcement": "evaluate",
"bypass_actors": [],
"conditions": {
"ref_name": {
"include": ["~DEFAULT_BRANCH"],
"exclude": []
}
},
"rules": [
{ "type": "deletion" },
{ "type": "non_fast_forward" },
{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 1,
"dismiss_stale_reviews_on_push": true,
"require_code_owner_review": false,
"require_last_push_approval": false,
"required_review_thread_resolution": true
}
},
{
"type": "required_status_checks",
"parameters": {
"strict_required_status_checks_policy": true,
"required_status_checks": [{ "context": "build" }]
}
}
]
}Save it as ruleset.json, then create the ruleset and inspect what applies to main:
gh api --method POST repos/OWNER/REPO/rulesets --input ruleset.json
gh api repos/OWNER/REPO/rules/branches/mainReading the parameters
Each parameter matches a setting you may know from classic protection:
required_approving_review_countsets how many approvals a pull request needs.dismiss_stale_reviews_on_pushclears approvals when new commits arrive.require_last_push_approvalrequires someone other than the last pusher to approve.required_review_thread_resolutionrequires all review conversations to be resolved before merging.strict_required_status_checks_policyrequires the branch to be up to date with the base branch before merging.
The pull_request rule here sets require_code_owner_review to false. Turn it on only when a CODEOWNERS file exists, or reviews will wait on owners who are not defined.
How the rules combine
All rules in one ruleset apply together. A change must satisfy every rule that matches its ref. A ruleset has no "last match wins" logic, unlike a CODEOWNERS file.
Exercise
A ruleset contains deletion, non_fast_forward, and required_status_checks. Its enforcement is evaluate. Someone force-pushes to a matching branch. What happens, and what must change to block the push?
Show answer
The force push succeeds, and GitHub records that the non_fast_forward rule would have blocked it. To block it, set the ruleset's enforcement to active.