Sub topic 11
Requiring Code Owner Review with Rulesets
This chapter assumes you can write a CODEOWNERS file, know that the last matching pattern wins, and have used branch protection rules. It shows how to get the same code owner enforcement from a repository ruleset.
Why use a ruleset
A ruleset is a named set of rules that applies to branches or tags matched by a pattern. Rulesets differ from branch protection rules in a few ways:
- Several rulesets can apply to the same branch, and their rules combine. The strictest value of each setting wins.
- Each ruleset has an enforcement status: Active, Evaluate (log what would happen), or Disabled.
- Anyone with read access can view the rules, so contributors can see why a merge is blocked.
- A ruleset can be defined at the organization level and applied to many repositories.
The pull request rule
Code owner review is a setting inside the "Require a pull request before merging" rule. It does nothing on its own.
To set it up in the web interface:
- Open Settings, then Rules, then Rulesets.
- Choose New ruleset, then New branch ruleset.
- Name it, and set enforcement to Active.
- Under Target branches, add the default branch or a pattern such as
release/*. - Turn on Require a pull request before merging.
- Inside that rule, turn on Require review from Code Owners. Also set Required approvals to at least 1.
- Optionally turn on Dismiss stale pull request approvals when new commits are pushed and Require approval of the most recent reviewable push.
- Select Create.
Creating the ruleset with the API
The same ruleset can be created with the REST API. Replace OWNER and REPO with real values.
gh api -X POST repos/OWNER/REPO/rulesets --input - <<'EOF'
{
"name": "Require code owner review",
"target": "branch",
"enforcement": "active",
"conditions": {
"ref_name": { "include": ["~DEFAULT_BRANCH"], "exclude": [] }
},
"rules": [
{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 1,
"dismiss_stale_reviews_on_push": true,
"require_code_owner_review": true,
"require_last_push_approval": false,
"required_review_thread_resolution": false
}
}
]
}
EOFCommon errors
- Enforcement left on Evaluate or Disabled. The ruleset reports results but blocks nothing. Switch it to Active.
- Rule enabled but no CODEOWNERS file. No owners match, so no owner approval is required.
- Owners without write access. An owner needs write access to the repository. Otherwise the review request is skipped, and the owner's approval does not count.
- Both a ruleset and a branch protection rule apply. Both are enforced. If merges are blocked unexpectedly, check each one.
- No bypass list. Add roles or teams to the bypass list only if they truly need to merge without owner approval. Bypass actors skip the rule.
Exercise
A ruleset targets main with enforcement set to Active. It requires a pull request with one approval, but "Require review from Code Owners" is off. A pull request changes /docs/guide.md, which a team owns in CODEOWNERS. Is the team's approval required to merge?
Show answer
No. The owners are still requested for review, but the ruleset only requires one approval from anyone with write access. Turning on "Require review from Code Owners" makes approval from an owner of each changed file mandatory.