Sub topic 15
Designing ownership for teams and monorepos
This chapter assumes you can write CODEOWNERS patterns, know that the last matching pattern wins, and have branch protection set to require code owner review. It covers how to lay out ownership so reviews stay useful and unrelated changes are not blocked.
Design goals
A good layout does three things:
- Every sensitive path has an owner who can actually review it.
- A pull request touching one area asks only that area's owners.
- No single team becomes the approval bottleneck for the whole repository.
Own by team, not by person
Prefer team owners such as @example-org/web-team over individual users. People change roles and go on vacation, and a team can absorb that. Every owner needs write access to the repository, so check team permissions when you add a team. Otherwise the owner can't satisfy the requirement.
Use a small fallback owner
The * line catches everything not matched later. Give it to a broad group of maintainers, not a tiny team. If three people own the fallback, any change to an unmatched file waits on them.
Mirror the directory structure
In a monorepo, give each top-level application or package its own line. Add narrower lines only for paths that need stricter review.
# .github/CODEOWNERS
* @example-org/maintainers
# One line per application
/apps/web/ @example-org/web-team
/apps/api/ @example-org/api-team
/apps/mobile/ @example-org/mobile-team
# Shared code: either team can approve
/packages/ui/ @example-org/web-team @example-org/mobile-team
# Stricter review inside an app (later line wins)
/apps/api/billing/ @example-org/payments
# Infrastructure and review rules
/infra/ @example-org/platform
/.github/CODEOWNERS @example-org/engineering-leadsShared paths and approval
Several owners on one line are alternatives. One approval from any of them satisfies that rule. You can't require two different teams to approve the same file, because only the last matching line counts. Give the file one owner team that represents both groups, or use required approval counts alongside CODEOWNERS.
A pull request that touches files under different rules needs an approval for each rule. A change to /apps/web/ and /infra/ needs the web team and the platform team.
Common design mistakes
| Mistake | Effect | Fix |
|---|---|---|
| One team owns `*` and nothing else | Every pull request waits on that team | Add per-directory lines |
| Owner team lacks write access | The requirement can't be met | Grant the team write access |
| Broad line placed after narrow lines | Narrow rules are overridden | Order from general to specific |
| Owners listed per file | The file grows hard to maintain | Own directories, not files |
Exercise
A repository has these lines in order: * @org/maintainers, /apps/ @org/app-team, /apps/api/ @org/api-team. A pull request changes /apps/api/server.js and /apps/web/index.js. Which teams must approve?
Show answer
@org/api-team and @org/app-team. The first file matches /apps/api/ last, so only the API team owns it. The second file matches /apps/ last, so the app team owns it. @org/maintainers is not needed, because the * line is overridden for both files.