Sub topic 4
CODEOWNERS and code owner review
This chapter assumes you know how to open and review pull requests and that you can edit files and settings in a repository where you have admin access. It covers the CODEOWNERS file and how it connects to branch protection.
What CODEOWNERS does
A CODEOWNERS file maps file paths to people or teams. When a pull request changes a file that matches a pattern, GitHub automatically requests a review from the owners of that file.
On its own, the file only requests reviews. It does not block anything. To make the owners' approval mandatory, you must turn on a rule that requires code owner review. The file says who owns what, and the rule decides whether their approval is needed.
Writing the file
GitHub looks for CODEOWNERS in .github/, in the repository root, or in docs/. It uses the first one it finds, in that order. Each line has a path pattern followed by one or more owners. Owners can be users (@alice) or teams (@my-org/docs-team). Teams must be in the same organization and must have write access to the repository.
# .github/CODEOWNERS
# Last matching pattern wins.
* @my-org/maintainers
/docs/ @my-org/docs-team
*.tf @my-org/platformThe patterns follow most of the same rules as .gitignore. Two things matter here:
- The last matching pattern wins. Put general patterns first and specific ones after them.
- A path that matches a later pattern uses only that pattern's owners. In the example, a change to
main.tfis owned by@my-org/platform, and@my-org/maintainersis not requested for it.
Requiring code owner review
In a classic branch protection rule, enable Require review from Code Owners under the pull request settings. In the API this is require_code_owner_reviews inside required_pull_request_reviews:
{
"required_status_checks": {
"strict": true,
"checks": [{ "context": "build" }]
},
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1,
"dismiss_stale_reviews": true,
"require_code_owner_reviews": true
},
"restrictions": null,
"allow_force_pushes": false,
"allow_deletions": false
}With this rule, a pull request cannot merge until the owners of every changed file have approved it. Rulesets have the same option, named require_code_owner_review in the pull_request rule.
Common errors
- The file is on the wrong branch. GitHub reads
CODEOWNERSfrom the pull request's base branch. Changes to the file take effect only after they merge. - An owner has no write access. Such an owner is ignored, so the requirement may be impossible to satisfy.
- A syntax error hides a rule. GitHub flags invalid lines on the file's page, so check there after editing.
- Owners are not protected. If the file itself is owned by nobody, anyone with an approving review can change who owns what. Add a line such as
/.github/CODEOWNERS @my-org/maintainers.
Exercise
A CODEOWNERS file has * @my-org/maintainers on line 1 and *.tf @my-org/platform on line 2. Whose approval does a pull request that changes only infra/main.tf need, assuming code owner review is required?
Show answer
Only @my-org/platform. The last matching pattern wins, so *.tf overrides * for that file, and @my-org/maintainers is not an owner of it.