Learning center

CODEOWNERS requests reviews; only a branch rule makes them required

A CODEOWNERS file on its own only asks owners to review, and any approval from someone with write access can still merge their code. To enforce ownership, turn on code owner review in the branch protection rule or ruleset that covers the branch, after you have checked the file for errors.

What CODEOWNERS does by itself#

GitHub's docs describe the file's default effect as a request: "Code owners are automatically requested for review when someone opens a pull request that modifies code that they own." Draft pull requests are an exception until they are marked ready. Enforcement is a separate step: "When someone with admin or owner permissions has enabled required reviews, they also can optionally require approval from a code owner before the author can merge a pull request." About code owners

With that option on, "any pull request that affects code with a code owner must be approved by that code owner before the pull request can be merged into the protected branch." About protected branches In a ruleset, the switch is require_code_owner_review, described as "Require an approving review in pull requests that modify files that have a designated code owner." Create a repository ruleset

So the same file means two different things depending on a setting stored elsewhere. Someone reading CODEOWNERS cannot tell which one applies.

A fictional refund change that went around its owners#

Suppose a fictional repository, acme/payments, has this CODEOWNERS file in .github/:

*                 @acme/platform
/src/refunds/     @acme/payments-risk

The branch rule on main requires one approval, and code owner review is off. Tess, on the platform team, opens a pull request that changes src/refunds/limits.ts to raise the automatic refund ceiling during a migration. GitHub requests a review from @acme/payments-risk. Uma, a platform teammate, approves within minutes. CI is green, because the limit is a constant and the tests read it. Tess merges.

The risk team's review request is still pending on a merged pull request. Nothing failed: the file did what it does by default, and the rule did what it was configured to do. The repository looked as if refunds had an owner who must approve, and it had an owner who was asked.

With Require review from Code Owners turned on, Uma's approval would satisfy the one-approval count but not the owner requirement, and the merge box would wait for a member of @acme/payments-risk.

Advisory and enforced ownership compared#

QuestionCode owner review offCode owner review on
Who is asked to reviewOwners of changed paths, automaticallySame
Whose approval satisfies the ruleAny writer, up to the required countAny writer for the count, plus one owner of each owned path changed
How many owners must approve a path with severalNoneAny one of them
A line naming a user or team that lacks write accessThat owner is not assignedSame, so the path can lose its required owner with no error at merge time
Paths with no matching lineAny writerAny writer

The fourth row is why enforcement can still fail quietly. "If you specify a user or team that doesn't exist or has insufficient access, a code owner will not be assigned," and "if any line in your CODEOWNERS file contains invalid syntax, that line will be skipped." A file over 3 MB "will not be loaded" at all. About code owners Audit the file before you enforce it, as the guide to CODEOWNERS errors describes.

What enforcement does not catch#

Enforced ownership means one owner approved. When reviews from code owners are required, "an approval from any of the owners is sufficient," so a line with two teams needs only one of them. About code owners It also says nothing about how carefully the owner read the change.

It does not protect the file that defines it. A pull request that edits CODEOWNERS gets its review requests from the version on the base branch, but once it merges, the new version decides every later review. The docs advise that "to protect a repository fully against unauthorized changes, you also need to define an owner for the CODEOWNERS file itself." The guide to owning the CODEOWNERS file covers that.

The cost of enforcing#

Every owner team becomes a required stop for its paths. A team that is small, away or overloaded holds up every pull request that touches its code, and a broad change that crosses many owned paths waits for every one of those teams. The guide to large cross-cutting changes covers splitting and staging those.

Advisory ownership has a cost too, just a less visible one: owners learn that their review requests are optional, and the requests become noise they filter out.

Decide and configure#

Keep CODEOWNERS advisory while ownership is still being worked out: teams are being formed, the file has errors, or owners have not agreed to be on the hook. Enforce it once the file is clean and each owner team has agreed to respond within a working day.

  1. Fix every error GitHub reports for the file, and confirm each team has explicit write access.
  2. Give the CODEOWNERS file and .github/ an owner of their own.
  3. In the classic rule for the branch, select Require a pull request before merging, then Require review from Code Owners. In a ruleset, set "require_code_owner_review": true in the pull_request rule.
  4. Close the exceptions. A classic rule does not apply to administrators unless you also select Do not allow bypassing the above settings, and in a ruleset every actor on the bypass list can merge without an owner's approval. Keep that list narrow, as the bypass list guide describes.
  5. Tell authors where to find the owner of a file before opening a pull request: the docs note that hovering the shield icon on a file shows who owns it and from which line.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading