Integrate GitLab with Azure Boards
A do it yourself guide on how to integrate GitLab Repository with Azure Boards (part of Azure DevOps) using Logic Apps.
Problem statement
Working on a customer presentation, I prepared a demo to showcase how Azure Boards can improve customer’s developer velocity. The customer uses GitLab to host their repositories and I didn’t want to force them to migrate all their code from day one. So I explored how to integrate Azure Boards and GitLab Repository to update the status of tasks or bugs when a commit or a pull request is added to a repo.
Guess what: there is no plugin to integrate GitLab and Azure Boards, so I had to integrate the two products by myself using Azure Logic Apps. Maybe I am the only person having this problem :)
The solution
My objective was to demonstrate that integration is possible. I focused on the scenario of updating a work item with the code change info every time a new commit is created.
In the above diagram, I summarized the following steps:
- A commit is added to a GitLab repository. The commit contains the Azure Boards work item written between two numeric symbols (FYI ‘#’). In this example, it is work item 250.
- A GitLab webhook triggers the logic app with all the commit data.
- The logic app parse those data and add the commit URL as a comment in the work item specified in the commit title.
The proposed solution is one of the several integrations available, and I hope this small guide will help you implement it and then adapt it for your specific problem.
The Logic App is playing the key role of the middle layer by receiving the events from GitLab, processing them (parsing and extracting data) and calling Azure DevOps via a connector. A middle layer was mandatory here since there is no direct integration between the two products.
Let’s implement it!
Prerequisite
- A GitLab repository
- An Azure DevOps account, a project and a work item
- An Azure subscription
Step 1: create a HTTP triggered Logic App
If it is the first time creating a Logic App, you can find an introduction to Logic Apps here. Azure Logic App is a great tool to integrate applications using a low-code/no-code platform.
You pay logic apps only for their usage and the initial 4000 invocations are free, so don’t worry about any cost for running this proof of concept.
After login in Azure, please select “Logic Apps” from the search and create a new Logic App.
Create the logic app and start with a common trigger When a HTTP request is received:
You will land on the Logic Apps Designer:
We need the Logic App URL and it will be available only after saving. Click on the save button in the top left corner and copy the generated URL:
Step 2: create a web hook in GitLab
From your GitLab repository, please click on Settings and select webhook:
You can paste the URL in the corresponding field. In this example, I want to trigger the Logic App every time a new push is performed in the repository, but there are many options available as you can see from the above screenshot.
Now I would like to understand the payload of a webhook request and the best way is to commit some new code in the repository and come back to the Logic App History to visualize it:
When you click on Show raw outputs of the “When a HTTP request is received” trigger, you will see the full input:
Please copy paste the request body in a file (request.json). Open the file with a JSON parser and explore it.
Exploring the JSON, I found the information that I need:
- The url of the commit is available in the JSON element $.body.commits[0].url
- The title of the commit is available in the JSON element $.body.commits[0].message
Step 3: How to parse a POST request in Logic Apps
You can resume the logic app designer session and select the When a HTTP request is received box. Click on Use sample payload to generate schema and paste there the content of request.json (created in the previous step). In this way the Logic App will be able to parse the request content and place it inside paramters.
Add a new step and search for “Variables” and select “Initialize variable” from the list.
You should proceed with naming the varibale with the following values:
- Name: commitUrl
- Type: String
- Value: triggerBody()?[‘commits’][0][‘url’]
(click on add dynamic content and select expression)
As you can notice the expression is the Logic Apps translation of the element path founded in the previous step.
Please rename this step to commitUrl:
Please repeat this step of adding a Initiate variable two times.
Please write the following parameters, for commitMessage:
- Name: commitMessage
- Type: String
- Value: triggerBody()?[‘commits’][0][‘message’]
(click on add dynamic content and select expression)
And for commitItem:
- Name: commitItem
- Type: String
- Value: (click on add dynamic content and select expression) split(variables(‘commitMessage’),’#’)[1]
The commitItem variable is useful to extract the working item number from the commit message.
Step 4: Azure DevOps Connector in Logic Apps
In the last step of the logic app we should connect to Azure DevOps and update the relevant working item.
Please click on add next step and select “Azure DevOps”, you will see that there are several actions available:
Scroll down and select “Update a work item”. Logic apps requires to access to your Azure DevOps organization:
After granting access, you will be asked to provide the following information:
Please select your organization and project and then add the commitItem and commitUrl variables as show below:
Then save your logic app.
Step 5: Test it!
You can now change a file in your repository and a commit message that match a work item id. You will see that a new comment is added in your work item.
Future improvements
In this solution, I illustrated a simple use case. Both GitLab and Azure Boards offers a lot of flexibility in their integrations:
By combining the triggers and actions available in GitLab and Azure DevOps, several use cases can be enabled, including:
- Visualize real-time development status leveraging on Azure Boards Dashboards
- Assign a task/issue to a developer to review a PR
- Update a working item when a PR is merged with additional information on the reviewer and the target branch
- And much more :)
About the author
Since 2012, Francesco managed seven mobile projects as a Mobile Lead or CTO. He covered several industries, from banking to lifestyle. Francesco led teams from one to seventy developers on four continents. He used technologies such as JavaScript, PhoneGap, Objective-C, Java, Kotlin, Swift, ReactNative and Flutter in his project. His most successful project has almost 8 million active users a month.
Today, Francesco is working as Cloud Solution Architect for one of the primary cloud providers in the world, providing advice and supporting the customer journeys on application innovation.
Disclaimer
This article reflects my personal opinions, and it should not be attributed to the view of any of my past or present employers.