Gitlab auto-assign reviewer for merge request
Asked Answered
E

4

10

I've been using GitHub for my previous project, and I had what is called auto-assign reviewer for pull requests that automatically assigns a reviewer from my team to do the code review.

now I'm working on a project hosted in Gitlab, I'm asking if gitlab supports this feature for merge requests or is there any alternatives for this feature on gitlab.

I already checked reviewer roulette and danger but it's not clear and I couldn't find any docs on how to implement it.

Thanks in advance.

Ethics answered 20/2, 2022 at 17:24 Comment(2)
I've been checking a feature like this also since it has been tedious adding multiple reviewers each time. I think the auto-assign feature is an ongoing enhancement. So far, there's no easy way to do it. Sharing gitlab thread (gitlab.com/gitlab-org/gitlab/-/issues/197169)Donar
This now has a response in that thread: gitlab.com/gitlab-org/gitlab/-/issues/197169#note_1264254460 You have to create a GitLab Group and add all users to the new group which should be auto assigned. Then in your code repo go to the Merge Request-Settings and add to the Merge commit message template the following: /assign_reviewer @my-new-fancy-groupBituminize
B
7

According to https://gitlab.com/gitlab-org/gitlab/-/issues/197169#note_1264254460

You have to create a GitLab Group and add all users to the new group which should be auto assigned. Then in your code repo go to the Merge Request-Settings and add to the Merge commit message template the following:

/assign_reviewer @my-new-fancy-group

Note that as of this writing, you should be adding to the Default description template for merge requests not the Merge commit message template.

An alternative is here

https://medium.com/@alex.roh/gitlab-how-to-automatically-add-code-reviewers-to-your-merge-requests-bcdefc2a8599

  • Create a .gitlab/merge_request_templates folder in the project root directory if it doesn’t exist

  • In .gitlab/merge_request_templates, create a new Markdown (.md) file

  • Embed the /assign_reviewer quick action and the code reviewers’ Gitlab usernames:

    /assign_reviewer @reviewer1 @reviewer2 @reviewer3 (and so on…)

  • Commit the changes, push to origin, and merge!

Bituminize answered 8/2, 2023 at 5:13 Comment(3)
As of 2024: add to the Merge commit message template beware to add this to the "Default description template for merge requests", not the "Merge commit message template".Bromal
Yes, the Default description template for merge requests in the repo settings will accept quick actions, no need to add a merge_request_templates fileJoyjoya
Edited to reflect this. Thanks all.Bituminize
F
1

You can do it manually with danger file, or write script in one of your ci steps, using gitlab API.

First get approvers list`

approvers = await axios.get(         
`https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/merge_requests/${CI_MERGE_REQUEST_IID}/approval_rules`,
{ headers: { 'PRIVATE-TOKEN': `${GITLAB_TOKEN}` }
}); 

Then filter received data:

const newReviewers = approvers?.data
  .filter(el => el?.rule_type === 'code_owner')
  ?.map({ eligible_approvers } => eligible_approvers?.map(({id}) => id));

and then post new reviewers:

axios.put(`
https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/merge_requests/${CI_MERGE_REQUEST_IID}`,
{ reviewer_ids: `${REVIEWER_ID_ARR}` },
{ 
 params: { private_token: `${GITLAB_TOKEN}` },
 headers: { 'Content-Type': 'application/json' } 
});
Faye answered 3/1, 2023 at 11:58 Comment(0)
B
0

There are some options that are similar to auto-assign but require Premium or Ultimate plans (they're not available for free plans).

First, you'll have to set up Code Owners to your project. Code Owners are people or groups that you can tie to your project's files so developers / anyone modifying that file can know who to go to for help or problems.

To setup Code Owners you add a CODEOWNERS file to either:

  • your project root directory
  • the .gitlab directory in your project root
  • a docs directory in your project root

The file syntax looks like this:

# Code Owners for a file
filename @username1 @username2

# Code Owners for a directory
directoryname/ @username1 @username2

# All group members as Code Owners for a file
filename @groupname

# All group members as Code Owners for a directory
directoryname/ @groupname

Next you'll have to setup Approval Rules for your Merge Requests.

To add Code Owners as Merge Request approvers, go to your project Settings -> General, expand Merge request (MR) approvals, locate Eligible Users and add how many approvals you require. Eligible Users in this context equates to Code Owners.

For more information you can view the Code Owners docs, Merge Request Approval Rules docs, and adding Code Owners as approvals for changes to protected branches.

Brahma answered 23/3, 2022 at 23:55 Comment(1)
The problem with this approach is that code owners are still not the same as reviewers and they don't see a merge request on the merge requests page, unless they are explicitly assigned to it as reviewers (Even though, they have permissions to approve it)Danyel
R
0

I had the same issue at my work and I wrote a Tampermonkey script to have additional buttons in the merge request view in Gitlab, which will put your preselected names into reviewers. It works for both existing and new merge requests.

After you install Tampermonkey in the browser you can get the script from my repo: https://gitlab.com/sandorfarkas/gitlab-auto-reviewers

enter image description here

Railway answered 29/9, 2023 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.