Page MenuHomePhabricator

Implement a bot which subscribes/mentions multiple reviewers for merge requests
Open, Needs TriagePublicFeature

Description

GitLab does not allow multiple reviewers, which can be fairly annoying. I mentioned this on fedi, and mocked up a quick POC.

The idea being that if a user places the line

Reviewers: @samtar, @some, @others

in their merge request description, these users will be subscribed to the MR and a message similar to the below will be posted:

@samtar @some @others

You have been added as a reviewer to this MR. Please review it and provide your feedback.

Maybe this could be reworked to instead tag a group as a reviewer, which would then mention/subscribe all members?

Thoughts/suggestions/etc. welcome, I'm just trying to get around a fairly annoying limitation of GitLab :')


1import gitlab
2import re
3
4"""
5Example commit/MR description:
6
7 ```
8 A description of the merge request.
9
10 Reviewers: @user1, @user2, @user3
11 Bug: T123456
12 ```
13"""
14
15gl = gitlab.Gitlab('https://gitlab.wikimedia.org')
16
17# This regex will match the Reviewers: line in the MR description
18reviewers_regex = re.compile(r"Reviewers: ?(?P<reviewers>.*?)\n", re.IGNORECASE)
19
20notication_message = "\n\nYou have been added as a reviewer to this MR. Please review it and provide your feedback."
21
22# I picked a project for testing
23project_name = "repos/commtech/wishlist-intake"
24project = gl.projects.get(project_name)
25
26# We'd normally get the open MRs like this
27mrs = project.mergerequests.list(state='opened', order_by='updated_at')
28
29# But for testing, I just picked a well-known MR ID
30mr = project.mergerequests.get(30)
31
32"""
33The returned description for MR 30 is:
34
35 ```
36 [WIP]\n\n\nReviewers: @samtar, @test\n\nBug: T362275
37 ```
38"""
39
40# Extract the reviewers from the MR description
41reviewers_match = reviewers_regex.search(mr.description)
42if reviewers_match:
43 reviewers = reviewers_match.group('reviewers').split(',')
44 users_to_notify = []
45 for reviewer in reviewers:
46 # Clean up the reviewer name
47 reviewer = reviewer.strip()
48 reviewer = reviewer[1:] if reviewer.startswith('@') else reviewer
49
50 user = gl.users.list(username=reviewer)
51 if len(user) == 1:
52 user = user[0]
53 print(f"[:)] User {reviewer} found: ", user.name, user.username, user.id)
54 # Subscribe the user to the MR
55 mr.subscribe(user.id)
56 # Append the user to the list of users to notify
57 users_to_notify.append("@" + user.username)
58 else:
59 print(f"[:(] User {reviewer} not found, or more than one result returned")
60
61 if users_to_notify:
62 # Notify the users
63 print("\n== Leaving the following message ==\n\n" + ', '.join(users_to_notify) + notication_message)
64 mr.notes.create({'body': ', '.join(users_to_notify) + notication_message})
65
66"""
67The output of running this script is:
68
69 ```
70 [:)] User samtar found: Samtar samtar 267
71 [:(] User test not found, or more than one result returned
72
73 == Leaving the following message ==
74
75 @samtar
76
77 You have been added as a reviewer to this MR. Please review it and provide your feedback.
78 ```
79"""

Event Timeline

Have we raised this limitation GitLab (Upstream pit of despair 🕳️) to see if its something they potentially want to work on and improve?

Likely - googling for gitlab "multiple reviewers" lists https://gitlab.com/gitlab-org/gitlab-foss/-/issues/32943 as the second result for me :)

Have we raised this limitation GitLab (Upstream pit of despair 🕳️) to see if its something they potentially want to work on and improve?

It is, for clarity, explicitly a part of their business strategy, and was deliberately removed from the Community Edition.

This would be a natural fit to plug into gitlab-webhooks, and I think I'm in favor of something like it. Also a reasonable match for the already-implemented mw:Git/Reviewers behavior that provides GitLab @-mentions for matched users.

I've thought before about abusing tags for this purpose - something like a "Reviewer-[Username]" tag, but that's clunky and the @-mention approach is probably a more natural fit for people's expectations.

The Reviewers: ... syntax seems reasonable to me. It would mirror the syntax that Diffusion/arc use for the same general purpose.

We can build this functionality using the existing https://wikitech.wikimedia.org/wiki/GitLab/Webhooks tool. This would be very similar to the existing https://wikitech.wikimedia.org/wiki/GitLab/Webhooks#Gitlab_Mentions sink in that the outcome is a comment on an MR letting someone know that it exists. The implementation would be so similar that I think it might be easiest to implement this as an additional feature in https://gitlab.wikimedia.org/repos/releng/gitlab-webhooks/-/blob/main/src/glwebhooks/sinks/gitlabmentions.py rather than making it a separate sink.

Open question: If we are having folks put @{username} references into the MR description, won't that already ping the mentioned users?

Open question: If we are having folks put @{username} references into the MR description, won't that already ping the mentioned users?

Good question, I know that a comment with an @{username} adds an entry in your GitLab todo list—maybe the same happens for MR descriptions(?).

The comment feature is how @dancy handled https://www.mediawiki.org/wiki/Git/Reviewers for GitLab in the gitlab-webhooks repo—adds a "note" (i.e., a comment) mentioning usernames.

Open question: If we are having folks put @{username} references into the MR description, won't that already ping the mentioned users?

Good question, I know that a comment with an @{username} adds an entry in your GitLab todo list—maybe the same happens for MR descriptions(?).

The mention of my username that @thcipriani added to the description of https://gitlab.wikimedia.org/thcipriani/stacked/-/merge_requests/2 created a new TODO entry for me in gitlab:

Screenshot 2024-05-20 at 12.42.57.png (122×2 px, 39 KB)

The mention/TODO does not seem to create a potentially blocking comment thread in the MR as the idea of making a "note" would I believe.

I just added a note to https://phabricator.wikimedia.org/T289712#9818356 that may be relevant here:

There's a problem with the method that is used to add mentions to a merge request. When the gitlab-mentions-bot mentions someone in a note, the bot account itself becomes subscribed to the MR and it receives update notifications. That's no good.