I'm looking for a straightforward way to leave a note for a Merge Request after it has been successfully merged. Ideally, I'd like this note to prompt the user to execute a specific URL, something like "please execute this link: http://...."
Is there a simple solution for achieving this, either through CI/CD or another approach? Any guidance or recommendations would be greatly appreciated.
Thank you!
Adding a message to a MR is quite simple using the gitlab cli and a CI/CD job. The CI/CD job definition should be something like:
add_note_to_mr:
image:
name: gitlab/glab
entrypoint: [""]
script:
- glab mr note $MR_IID --unique --message "please execute this link: http://...."
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
$MR_IID
can potentially be replaced by the default variable $CI_MERGE_REQUEST_IID
. This would run on every update of the MR, but certainly not when it is closed.
The problem is that you need to get the event where the MR is merged. That makes it more complicated since Gitlab CI will run a build on the target branch, but according to the docs no information about closed MRs will be included.
My best guess to overcome this is to add a Webhook that triggers the pipeline. BUT THIS IS FAR FROM SIMPLE!
If you still want to try this is how I was able to achive it in the past (with closed issues but MRs should be pretty much the same):
https://gitlab.com/api/v4/projects/{your-project-id}/ref/main/trigger/pipeline?token={TRIGGER-TOKEN}
. To be sure, just mask the trigger token. Select "Merge Request Events" rules:
- if: $CI_PIPELINE_SOURCE == "trigger"
.json
file that contains the MR IID in the attribute object_attributes.iid
.script
of the job to parse the .json
script:
- apk add jq
- MR_IID=$(cat $TRIGGER_PAYLOAD | jq ".object_attributes.iid")
- glab mr note $MR_IID --unique --message "please execute this link: http://...."
So the complete job yaml should look like
add_note_to_closed_mr:
image:
name: gitlab/glab
entrypoint: [""]
script:
- apk add jq
- MR_IID=$(cat $TRIGGER_PAYLOAD | jq ".object_attributes.iid")
- glab mr note $MR_IID --unique --message "please execute this link: http://...."
rules:
- if: $CI_PIPELINE_SOURCE == "trigger"
You might want to add some security checks to not run add the comment if the MR is still open. Check the contents of the payload file by cat $TRIGGER_PAYLOAD
. To see what you have available to exit early. Instead of installing jq
you could also use grep
since the .json
is not extremely complex. Even better, use a small python script to do the job. A python interpreter should be available in the glab image.
Edit: To check if the MR is really closed (or merged), you could add that to jq before execution of glab in your script...
- MR_IID=$(cat $TRIGGER_PAYLOAD | jq '.object_attributes | select(.action=="close" or .action=="merge") | .iid')
- if [ -n $MR_IID ]; glab mr note $MR_IID --unique --message "please execute this link: http://...."; fi
Edit 2: Note that installing jq every time you run the job may be quite a heavy download. I would definetly recommend replacing the logic by a python script.