Gitlab: How to automatically add code reviewers to your merge requests

Alex Roh
3 min readJun 29, 2022

--

Do you manually add code reviewers to every single merge request? Read on to discover how you can automate that process!

Option 1: Embed a Gitlab quick action in merge request templates

How to set up

  1. Create a .gitlab/merge_request_templates folder in the project root directory
  2. In .gitlab/merge_request_templates, create a new Markdown(.md) file
  3. Embed the /assign_reviewer quick action and the code reviewers’ Gitlab usernames:
    /assign_reviewer @reviewer1 @reviewer2 @reviewer3 (and so on…)
  4. Commit the changes, push to origin, and merge!

How to use

  1. Open a new merge request or click “Edit” on an existing one
  2. Choose your newly created template in the Description section
  3. Update the description, issue ticket number, etc.
  4. Save

That’s it!

Option 2: Utilize the Gitlab API and CI/CD

How to set up

1. Create a new Gitlab group or a subgroup of an existing group and add code reviewers to the group. You can skip this step if you already have a group or wish to hard-code the reviewer IDs in the CI/CD pipeline job

2. Create a .gitlab-ci.yml file in the project root directory

3. Configure a new job to run in merge request pipelines with rules. Add a “set up merge request" stage, if applicable

Optional: Use rules:changes to have more granular control over which changes trigger the job. If you have an android and ios directory in your repository, for example, you can create two separate jobs that assign Android and iOS code reviewers, respectively, depending on what changes are included in the merge request

4. In before_script, instruct the job to install jq, a JSON processor, on the runner before it runs. Depending on your runner’s environment, you may have to use a different package manager from the one used below

5. Write a script that assigns code reviewers to your merge request via the Gitlab API

Step 1) Fetch the code reviewer list via the Gitlab API. If you wish to hard-code the reviewer list, skip this step

Tip #1: Make sure your token has read API access to your group. CI_JOB_TOKEN, the default token generated for every job, does not have API read access

Tip #2: If you created a subgroup of an existing group, make sure you strictly isolate direct members from inherited members and use the appropriate API

Step 2) Extract the reviewers’ Gitlab IDs, not the usernames, from the JSON response and put them in an array. If you wish to hard-code the reviewer list, hard-code an array of the code reviewers’ Gitlab IDs instead

Step 3) Use the Gitlab API to add the reviewers to your merge request

6. Commit the changes, push to origin, and merge!

How to use

Just create a new merge request!

Sample .gitlab-ci.yml

And there you have it! Two options to automatically assign code reviewers to your merge requests.

If you’ve done something similar or know of any alternatives, let me know in the comments section!

Thanks for reading.

If you find this content valuable,

  1. 👏
  2. Share it with your coworkers
  3. Come fuel me up with coffee!

--

--