Sub topic 5
Requesting Reviewers and CODEOWNERS
This chapter assumes you can open a pull request (PR) from a feature branch and that you know what a review is. It covers how to ask specific people to review your work, and how a CODEOWNERS file makes some of those requests automatic.
Requesting reviewers by hand
A reviewer request is a notification tied to a PR. The person gets an email or a GitHub notification, and the PR appears in their review queue. You can request people or whole teams from the Reviewers section in the PR sidebar. You can also do it from the command line:
gh pr edit 123 --add-reviewer octocat,my-org/docs-teamOnly people with at least read access to the repository can be requested, and only people with write access can submit reviews that count toward required approvals. Choose reviewers who know the code you changed. Add one more person if the change touches an area you don't know well.
What CODEOWNERS does
A CODEOWNERS file maps file paths to the users or teams responsible for them. When a PR changes a matching file, GitHub requests a review from the owners automatically. You don't have to remember who to ask.
GitHub looks for the file in .github/, the repository root, or docs/. It uses the first one it finds, in that order.
# .github/CODEOWNERS
*.js @octocat
/docs/ @my-org/docs-teamEach line is a path pattern followed by one or more owners. The patterns work like .gitignore patterns.
*.jsmatches JavaScript files anywhere in the repository./docs/matches only thedocsdirectory at the root.- The last matching line wins. Put general rules first and specific ones after them.
Owners need write access to the repository. A team owner must be visible to the repository and have write access too. Otherwise GitHub ignores that line.
Making owner review mandatory
By default, a CODEOWNERS request is only a request. To enforce it, turn on Require review from Code Owners in a branch protection rule or ruleset for the target branch. The PR then cannot merge until an owner of each changed path approves it. The next chapters on branch protection cover that setting in more detail.
Common errors
- Owner never gets requested. The file is in the wrong location, or the owner lacks write access. Open the file on GitHub. It flags syntax errors and unknown owners.
- The wrong owner is requested. A later, broader pattern is overriding an earlier specific one. Reorder the lines.
- File exists only on a feature branch. GitHub reads
CODEOWNERSfrom the PR's base branch, so changes to the file take effect after they merge. - Draft PRs. Owners are not requested on a draft PR until you mark it ready for review.
- Too many owners. A catch-all
* @my-org/everyoneline sends noise to everyone and makes requests easy to ignore. Prefer small, specific teams.
Exercise
Your CODEOWNERS file has * @my-org/everyone on the first line and /docs/ @my-org/docs-team on the second. A PR changes only docs/intro.md. Who is requested, and what would change if you swapped the two lines?
Show answer
Only @my-org/docs-team is requested, because the last matching line wins. If you swapped the lines, * @my-org/everyone would match last, so everyone would be requested and the docs team would be ignored.