How to add label when merge request is created on gitlab?

5.3k views Asked by At

I would like to add label "foo" once merge request is created.

How can I do it?

If not possible via gitlab gui (using some robot) it is possible via gitlab api?

2

There are 2 answers

0
Adam Marshall On

You’d have to register a new Webhook, then have something listen and check for the Merge Request Event (https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#merge-request-events).

Then you’ll have to parse it, checking the object_attributes.state field to see if the MR was created, updated, merged, closed, or pushed to, and grab the ID in object_attributes.id., Then you can use the Merge Request API to add a label to the MR. You’ll have to use the Update MR operation and change the labels (https://docs.gitlab.com/ee/api/merge_requests.html#update-mr)

0
Egor On

It is possible to do so using Gitlab quick actions in the merge requests' default description.

In Gitlab on the project level go to:
Settings -> Merge requests -> Default description template for merge requests

and set the following description:
/label ~123 ~"my-label"
where 123 - is the id of one existed label and my-label is the name of another existed label. If labels do not exist - it will not work.