Skip to content

Say hello to the new PR page.

Read more

How to enforce pull request reviewers in GitHub

Kenny DuMez
Kenny DuMez
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Ensuring that the right reviewers are assigned to pull requests (PRs) in GitHub is important for maintaining high standards of code quality and compliance within software development projects. This guide provides a step-by-step approach to enforcing pull request reviewers both through GitHub's built-in Codeowners feature.

Enforcing PR reviewers helps to:

  • Ensure code quality: Having the right set of eyes on every code change ensures high quality and adherence to coding standards.
  • Maintain project standards: Specific reviewer requirements can help maintain consistency across the project's codebase.
  • Compliance and accountability: Enforcing reviewer rules ensures that all code changes are vetted by authorized personnel, enhancing security and accountability.

To enforce PR reviewer rules you can also set up branch protection policies in your GitHub repository:

  1. Navigate to your repository settings: Go to 'Settings' > 'Branches'.
  2. Add or edit a branch protection rule: Select the branch you want to protect (typically main or master) and click on 'Add rule' or edit an existing one.
  3. Enable required reviews: Check the box for 'Require pull request reviews before merging'. This setting prevents merging until the specified number of approvals from reviewers has been obtained.

To further control the review process, specify who must review PRs:

  • Code owners: Use a CODEOWNERS file in the repository to automatically request reviews from the right people based on the files changed in the PR. This file is placed in the root, docs/, or .github/ directory of the repository and specifies users or teams that are responsible for specific parts of the project.

Example of a CODEOWNERS file:

Terminal
## Format: <path> <owner>
/src/ @frontend-team
/docs/ @documentation-team
  • Number of reviewers: Specify the number of required reviewers to ensure that multiple perspectives are considered before merging changes.

After setting up the required reviewers, enforce these policies consistently across the project:

  • Automate with GitHub Actions: Create custom GitHub Actions workflows that check if the PRs meet the review criteria specified in your branch protection rules and CODEOWNERS file. This can include checks for approvals from specific teams or individuals.

Example of a GitHub Action to enforce reviewer policies:

Terminal
name: Enforce Reviewers
on: pull_request_review
jobs:
check-reviewers:
runs-on: ubuntu-latest
steps:
- name: Check approved reviewers
uses: some-org/review-enforcer-action@v1
with:
required-reviewers: 2
required-teams: frontend-team, backend-team

Regularly monitor the effectiveness of your reviewer policies and make adjustments as needed. This can involve updating the CODEOWNERS file, modifying branch protection settings, or refining your GitHub Actions.

Implementing strict PR reviewer rules is important for maintaining high code quality, ensuring compliance, and fostering accountability within development projects. With GitHub's branch protection features, setting up comprehensive reviewer policies ensures that each piece of code is thoroughly vetted by the appropriate experts before integration. By continually monitoring and adapting these review protocols, teams can maintain a high standard of code integrity and project coherence, ultimately leading to more robust and reliable software solutions.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Built for the world's fastest engineering teams, now available for everyone