Sub topic 10
Requiring Code Owner Review with Branch Protection
This chapter assumes you can write a CODEOWNERS file, know that the last matching pattern wins, and understand that GitHub requests owners as reviewers on pull requests automatically. Here you turn those requests into approvals that must happen before merge.
Requests are not requirements
A CODEOWNERS file only asks owners to review. Without more settings, a pull request can merge with no owner approval at all. A branch protection rule adds the enforcement.
The setting is Require review from Code Owners. It appears under Require a pull request before merging in the branch protection rule. The rule only has effect if the pull request requirement is also on.
Turning it on
- Open Settings, then Branches, in the repository.
- Add or edit a rule for the target branch, such as
main. - Select Require a pull request before merging.
- Select Require review from Code Owners.
- Optionally set Required number of approvals and Dismiss stale pull request approvals when new commits are pushed.
You can set the same options through the REST API:
gh api -X PUT repos/OWNER/REPO/branches/main/protection \
--input - <<'EOF'
{
"required_status_checks": null,
"enforce_admins": true,
"required_pull_request_reviews": {
"require_code_owner_reviews": true,
"required_approving_review_count": 1,
"dismiss_stale_reviews": true
},
"restrictions": null
}
EOFReplace OWNER and REPO with real values. The call replaces the whole protection configuration for the branch. Fields you leave out are reset, so read the existing rule first if the branch is already protected.
How the pieces combine
The two review settings are independent. Both must be satisfied.
| Setting | What it checks |
|---|---|
| Required approving reviews | The pull request has at least N approvals from people with write access. |
| Require review from Code Owners | Every file changed has an owner, and an owner of that file has approved. |
| Dismiss stale approvals | New commits clear earlier approvals, so owners must approve the final code. |
With one required approval and code owner review enabled, a single approval from someone who is not an owner satisfies the count but not the owner requirement. The pull request stays blocked until an owner approves.
Common errors
- Owner has no write access. An owner must have write permission on the repository. Otherwise GitHub cannot count their approval, and the pull request can stay blocked.
- Team not visible. A team owner needs access to the repository, or the entry is ignored.
- Rule on the wrong branch. The rule applies only to branches matching its pattern. Check that it matches your target branch.
- Unowned files. A file with no matching owner needs no code owner approval. Use a
*fallback if every file should require one.
Exercise
A repository requires one approving review and has "Require review from Code Owners" enabled. A pull request changes /docs/intro.md, owned by @example-org/docs-team. A maintainer who is not on that team approves it. Can it merge, and why?
Show answer
No. The maintainer's approval meets the required count of one, but the code owner requirement is not met until a member of @example-org/docs-team approves (assuming that team has write access). The pull request stays blocked until then.