Sub topic 12
How Code Owner Approval Is Satisfied
This chapter assumes you can write a CODEOWNERS file, know that the last matching pattern wins, and have enabled "Require review from Code Owners" on a protected branch. Here you will see how GitHub decides that a pull request has enough code owner approvals.
The rule: every owned file needs an owner's approval
When code owner review is required, GitHub looks at each file changed in the pull request and finds the owners of that file. The pull request is satisfied only when every changed file that has an owner is covered by an approving review from at least one of that file's owners.
Approval is counted per file, not per owner list. One approval does not cover other files that have different owners.
Take this file:
* @example-org/maintainers
/apps/web/ @example-org/web-team
/apps/web/auth/ @example-org/security
/docs/ @example-org/docs-teamA pull request changes apps/web/auth/login.js, apps/web/index.js, and docs/intro.md.
| Changed file | Owner who must approve |
|---|---|
| `apps/web/auth/login.js` | `@example-org/security` |
| `apps/web/index.js` | `@example-org/web-team` |
| `docs/intro.md` | `@example-org/docs-team` |
Because the last matching pattern wins, @example-org/maintainers owns none of these files. Three separate groups must approve. Files with no owner need no code owner approval.
One owner is enough, and teams count as one
If a file lists several owners, such as /docs/ @example-org/docs-team [email protected], any one of them can approve. When the owner is a team, one approval from any team member satisfies that team.
The approver needs write access to the repository. An approval from someone without write access does not count toward code owner review, even if they are listed as an owner.
Combining with other approval settings
Code owner review works alongside the general settings in the branch protection rule.
- Required approving reviews. If you require two approvals, you need two approvals and the code owner coverage. One person can supply both only if they are a code owner and the count is met by others too.
- Dismiss stale approvals. A new push removes earlier approvals, so owners must approve again. This applies to code owner approvals as well.
- Require approval of the most recent push. Someone other than the last pusher must approve. A code owner who pushed the final commit cannot count as the approval for their own files.
Common mistakes
- Expecting one broad owner, such as the fallback
*owner, to cover files that a later pattern assigned elsewhere. - Listing an owner who lacks write access, so the approval never counts.
- Forgetting that a new commit invalidates approvals when stale review dismissal is on.
Exercise
A pull request changes apps/web/auth/login.js and apps/web/index.js. Using the CODEOWNERS file in this chapter, @example-org/security approves and nobody else does. Is the code owner requirement met? Explain.
Show answer
No. apps/web/index.js is owned by @example-org/web-team, and approval is checked per file. The security team's approval covers only apps/web/auth/login.js. A member of the web team must also approve.