Summarize Your Merge Requests with Automated Comments

How to set up a comment bot on Gitlab

Ivan Montiel
3 min readAug 31, 2017

I’m loving Gitlab’s CI and integrated Pipeline. I also like that I am able to use the gitlab-runner to have the CI pipeline run on my local machines as well.

I’m also loving Terraform since it let’s me use Code as Infrastructure, which helps automate deployments and create consistent infrastructure between development and production environments.

The only thing I felt was missing would be for the Gitlab CI to post what changes in terraform a merge request would cause. Just because a terraform plan passes doesn’t mean all the changes are wanted. But it’s a little tedious to do a code review and also have to pull up the CI console output to review what terraform might change.

That’s why I decided that I’d like the CI to make a comment on a pull request if there is one. So let’s get started.

Create a new user

We’re going to want to make a new user that will make the request. Let’s just call it Terry, the Terraform Plan Bot.

Setting up a new user in Gitlab is easy enough, so let’s just skip to the next step.

Give that user access to your repos

Go to your group members and add that new user you created. In my case, I gave the bot user Developer access.

Create an authorization token for that user

We are going to use the Gitlab API to post, so we’ll need an OAuth Token for that user. You can get one in User -> Settings -> Access Tokens.

Give the token a nice name — “Gitlab CI Comment Authorization” or something like that — and make sure to add the api scope. You don’t need to add an Expires at date.

Create the personal access token and save it someone special.

Add the authorization token to your CI pipeline env vars

Jump to your project and head over to Settings -> Pipelines and scroll on down to the Secret variables section. Add your Key “ OAUTH_TOKEN” and your Value as the personal access token we just saved.

Click that Add new variable button and now all we have to do is the code!

The Code

I’m going to do this in NodeJS, but you can do this in any languages you want, even bash. It doesn’t really matter.

const request = require('request');const privateToken = process.env.OAUTH_TOKEN;
const projectId = process.env.CI_PROJECT_ID;
const mergeRequestId = process.env.PR_ID;
const message = `
Your summary message here.
`;
const path = 'https://gitlab.com/api/v4/projects/' + projectId + '/merge_requests/' +
mergeRequestId + '/notes?private_token=' + privateToken;
request.post(path, { form: { body: message } });

Now, we do need to do something special in order to get that PR_ID:

#!/bin/bashPR_ID=$(curl -s -L "http://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/merge_requests?private_token=${OAUTH_TOKEN}&state=opened" | jq -r ".[]|select(.sha == \"$CI_COMMIT_SHA\")|.iid")node create_note.js

And that’s about it! Your message can be anything and can have any markdown in it just like you normally would put if you were commenting on your Merge Request!

Thanks for reading! If you liked this article, feel free to follow me on Twitter.

--

--