iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤖

Streamlining Code Reviews: How to Use GitHub Code Owners for Automatic Reviewer Assignment

に公開

This article explains how to use GitHub's CODEOWNERS to automate pull request reviewer assignments and clarify responsibilities.

Background

When we started operating a monorepo, we encountered the following challenges in our pull request workflow:

  • Reviewer assignments were often missed or misallocated because the main person in charge varied by package.
  • When changes spanned multiple packages (e.g., schema changes), the number of reviewers who needed to check the PR was so large that manually specifying them every time became a major burden.
  • Onboarding new members to the team was difficult.

Initially, we addressed this through a Working Agreement (internal team rules), but we eventually decided to systematize it using GitHub's Code Owners feature.

What is Code Owners?

You can use a CODEOWNERS file to define individuals or teams that are responsible for code in a repository.

https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners

This feature allows you to designate owners for specific files or directories.

How to Configure Code Owners

You can configure it by creating a .github/CODEOWNERS file and using the same syntax as .gitignore. Although not widely known, using email addresses is also supported. You can set teams as well.

However, only users or teams with push access to the repository can be specified.

/project-a/ @bicstone
/project-b/ t.oishi@example.com
/project-c/ @octo-org/octocats
.graphqls @bicstone t.oishi@example.com @octo-org/octocats

Additionally, when you open the CODEOWNERS file on GitHub, syntax check results are displayed, which helps you detect syntax errors or cases where a user no longer has push access to the repository.

Screenshot of a CODEOWNERS file open on GitHub. It displays "This CODEOWNERS file is valid."
"This CODEOWNERS file is valid." is displayed

How to Assign Users Using the Shuffle Feature

By utilizing team features, you can also shuffle the users assigned when a team is specified in Code Owners.

https://docs.github.com/en/organizations/organizing-members-into-teams/managing-code-review-settings-for-your-team#about-auto-assignment

To configure this, navigate to the team's "Settings" and select "Code review."

Enable "Enable auto assignment" and configure the number of users to assign automatically, the assignment algorithm, and other settings.

Access the team's "Settings" and configure it from "Code review."
Set from "Enable auto assignment"

How to Require Approval from Code Owners

Branch protection rules include a setting called "Require review from Code Owners." Enabling this makes approval from Code Owners mandatory for merging.

https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/defining-the-mergeability-of-pull-requests/managing-a-branch-protection-rule#creating-a-branch-protection-rule

To configure this, navigate to the repository's "Settings" and select "Branches."

You can enable it by checking "Require review from Code Owners" within "Protect matching branches."

Enable "Require review from Code Owners" within "Protect matching branches."
Set from "Require review from Code Owners"

Benefits

Improving Review Efficiency through Systematization

When you create a PR for a relevant file, reviewers are automatically assigned without the reviewee having to do anything.

GitHub screenshot. In the pull request detail page history, it shows "bicstone requested review from ○, ○, ○ and ○ as code owners now."
Reviewers are automatically assigned

By systematizing this process, there are several benefits for both reviewees and reviewers:

  • Eliminates the need to manually enter reviewers when creating a pull request, saving time and effort.
  • Reduces the burden of onboarding, as reviewer management is now automated.
  • Since multiple members are assigned mechanically, whoever is free can review first, leading to faster turnaround times.
  • Since reviewers are automatically assigned to all pull requests related to their area of responsibility, it helps build shared knowledge within the team and eliminates silos.
  • The shuffle feature can be used to reduce the burden on specific reviewers as needed.

Clarifying Code Ownership

When you open a file on GitHub, the corresponding Code Owners are displayed. It is very useful to see not only the contributors who developed the code but also the responsible team (Code Owners) without having to open a PR, which often helps later on.

Moreover, by using Branch protection in conjunction, merging is impossible without approval from Code Owners, making code responsibility explicit.

Screenshot of a file open on GitHub. The Code Owners for this file are displayed.
Code Owners for this file are displayed

Conclusion

By leveraging GitHub's Code Owners feature, we have achieved improved code review efficiency and clarified code responsibility in a monorepo environment.

We believe that by improving the speed and quality of code reviews, the overall delivery speed of the team increases, allowing us to build a team that can provide value more quickly.

It only needs to be set up once and requires no ongoing maintenance, so I highly recommend giving it a try.

Discussion