How to Automatically Assign Reviewers To GitHub Pull Requests

How to Automatically Assign Reviewers To GitHub Pull Requests

Have you ever collaborated on a GitHub project in a small-sized team, where anyone could review your PRs, but you would need to manually add everyone as a reviewer for 'fastest fingers first'?

In this post, I'll share how you can quickly set up GitHub Actions to automate the process.

Note: This post specifically addresses working on a project with personal accounts and outside of a 'GitHub organisation', so no code review settings to set this up.

Last year, I worked with 3 mentees who were mostly new to working on a team project, and one 'obstacle' they faced was remembering to tag one another in PRs for review (and myself). There are not many articles that actually outline the steps, so this is me documenting for future usage.

Steps to Automatically Assign Reviewers to PRs

It's actually pretty simple to set up, as there are a number of existing actions in GitHub Marketplace that you can incorporate into your project.

The one I'm going to be using is the 'Auto Assign Action' by kentaro_m.

It has a pretty decent overview of the available configurations options and you can also check out the GitHub repo for more details.

  1. Set up GitHub Actions for your project

  • Create your .github folder.

  • Create the workflows sub-folder and the two config files as shown below.

a) action.yml is the workflow file that gets run everytime you open a new PR (or other triggers you've defined in the file).

b) auto_assign.yml is the configuration file that automatically assigns the reviewers.

Note: I might not be remembering correctly, but the setup did not work when I named auto_assign.yml differently.

So, ensure to name correctly (or let me know in the comments if a different name worked!)

  1. Setup for actions.yml

name: 'Auto Assign Reviewers'
on:
  pull_request:
    types: [opened, reopened]

jobs:
  add-reviews:
    runs-on: ubuntu-latest
    steps:
      - uses: kentaro-m/auto-assign-action@v1.2.5
        with:
          repo-token: ${{ secrets.ASSIGN_REVIEWERS }}
  • The above action will run for the opened and reopened triggers i.e when you open a new PR or reopen an existing one. You can add other trigger types.

  • It runs one job add-reviews, which has one step that uses the action from the marketplace. In this code, I have used v1.2.5, but you can always update that line to use the latest version (v2.0.0, as at the time of publishing this post).

  • You also need to include a repo-token for authentication. This can be created in the project repository.

    • Navigate to 'Settings >> Secrets and Variables (under Security) >> Actions'.

    • Under 'Repository secrets', add a new secret. In my case, I have named the secret 'ASSIGN_REVIEWERS', and this is what I added in the config file.

  1. Setup for auto_assign.yml

# Set to true to add reviewers to pull requests
addReviewers: true

# list all the GitHub account usernames you want as reviewers
reviewers: 
  - khairahscorner
  - accountA
  - accountB

# set to 0 to ensure it assigns everyone listed above as reviewers (or specifiy a limit)
numberOfReviewers: 0

# Adds whoever is authoring the pull request as assignee
addAssignees: author

Other Actions

There are several other actions you can also try.

E.g This one also works for issues, but you can probably still use the same action in this post by updating actions.yml.

name: 'Auto Assign Reviewers'
on:
  issues:
    types: [opened]
  pull_request:
    types: [opened, reopened]

In summary, have a look through to decide what works best for your use case.


Thanks for reading, and I hope you find the post useful and handy!