iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📘

Automating Jira Status Transitions on GitLab Merge Request Merges

に公開
2023/10/11

Transitioning Status When a Merge Request is Merged in GitLab

Introduction

One day at work.

Boss: "Oh? Is this Jira issue still in review? Wasn't it merged already?"
Me: "Ah! I'm sorry... I forgot to transition the status..."
Boss: "Understood. I'll move it for you."
Me: "Thank you. Sorry about that..."

Another day

Colleague: "Since it's merged, I transitioned the status of the issue you were handling."
Me: "I'm sorry... thank you."

As you can see, I frequently forgot to transition Jira statuses, causing trouble for others.
Furthermore, we are now measuring lead times for issues, making it increasingly critical if someone forgets to transition the status.
I realized this couldn't continue, so I integrated Jira and GitLab to automatically transition to a specified status when a merge request is merged.
This article shares how to do that.
It is very simple, so if you are facing similar issues, please give it a try.
Note that this guide is based on Jira Cloud.

Preparation on the Jira Side

The following three pieces of information are required on the Jira side:

  • Jira API Token
  • Transition ID of the target status in Jira
  • Jira URL

Let's look at how to obtain each one.

Jira API Token

Access this page to generate an API token and click the "Create API token" button.
Untitled
You will then be prompted for a label for the token. Enter any string and click the "Create" button.
Untitled
The token will be displayed; click the "Copy" button.
Please note that this token cannot be retrieved again once the modal is closed, so make sure to save it in a safe place like a notepad.

Obtaining the Transition ID for the Target Issue

To move an issue to a specific status when a merge request is merged, you need the status ID.
You can obtain this by accessing https://your-jira-project-name.atlassian.net/rest/api/2/issue/issue-identifier/transitions.
When you access the URL, a JSON response like the one shown below will appear. Identify the id for the status you want to transition to and record it just as you did with the API token.
Note: I've provided a formatted JSON here for clarity, but the actual response will be unformatted, so you'll need to search through it.

{
	"expand": "transitions",
	"transitions": [
		{
			"id": "11",
			"name": "Backlog",
			"to": {
				...omitted
				}
			},
			...omitted
		},
		{
			"id": "21",
			"name": "Selected for Development",
			"to": {
				...omitted
				}
			},
			...omitted
		},
		{
			"id": "31",
			"name": "In Progress",
			"to": {
				...omitted
				}
			},
			...omitted
		},
		{
			"id": "41",
			"name": "Done",
			"to": {
				...omitted
				}
			},
			...omitted
		},
		{
			"id": "51",
			"name": "In Review",
			"to": {
				...omitted
				}
			},
			...omitted
		}
	]
}

The issue identifier refers to the area boxed in red in the image below, which is displayed when you open a Jira issue.
2023-09-17_10h53_48.png

Jira URL

This is https://your-jira-project-name.atlassian.net.

Preparation on the GitLab Side

Go to Integrations within the Settings of the GitLab project you want to link.
2023-09-17_11h08_30.png
You will see a list of various external integrations; click on Jira from that list.
2023-09-17_11h10_05.png
An input form will appear. Fill it out as follows:

  • Enable integration
    Check this box. The integration won't be active unless this is checked.
  • Web URL, Jira API URL
    Specify the URL for the integration.
    Enter the https://your-jira-project-name.atlassian.net URL you obtained during the Jira setup.
    Note that if the Jira API URL is the same as the Web URL, you can leave it blank.
  • Authentication method
    Specify the user who can access https://your-jira-project-name.atlassian.net.
    Initially, radio buttons will be displayed as shown below. Since we are using Jira Cloud, select Basic.
    2023-09-17_11h16_39.png
    For "Email or username," enter the email address or username of any user who has access to the URL.
    For "API token or password," enter the API token you obtained from Jira.
    While using a user password might result in a successful connection test, status transitions won't occur when a merge request is merged, so make sure to enter the API token.
  • Trigger
    Configure the events to be executed during GitLab commits or merge requests, as shown in the image.
    2023-09-17_11h22_15.png
    First, for the "Commit" and "Merge request" checkboxes: these are used to select whether to trigger events when a commit is made to GitLab or when a merge request is created or merged.
    Since we only want to trigger events for merge requests this time, uncheck "Commit."
    Next, for Comment settings: these configure whether to record merge request or commit events in Jira comments.
    Selecting Standard for "Comment detail" will display the commit title and the target branch name in the Jira issue.
    Selecting All details will add further details like the commit message.
    Since our objective is just to transition the status when a merge request is merged, uncheck "Enable comments" under "Comment settings" to prevent comments from being added to Jira.
    Transition Jira issues to their final state sets the destination for the Jira issue.
    Move to Done moves the issue to the default "Done" status created by Jira.
    Use custom transitions allows you to configure the transition to a specific status by specifying its Transition ID.
    We will use "Use custom transitions."
    Select "Use custom transitions" and enter the ID of the status you want to transition to, which you previously obtained from https://your-jira-project-name.atlassian.net/rest/api/2/issue/issue-identifier/transitions.
    Finally, configure Jira issue matching.
    This setting is for GitLab to find the associated Jira issue.
    2023-09-17_11h56_13.png
    If you set the Jira issue regex as shown in the image and include "Dashboard ID-Issue Number" in the merge request or commit message, it will automatically link them.
    If there are cases where you want to comment without linking, set a Jira issue prefix.
    For example, if you set the prefix JIRA#, the integration between GitLab and Jira will only occur when you use JIRA#Dashboard ID-Issue Number.
  • Issues
    This is available for GitLab Premium plans and above.
    It allows you to display and sort Jira issues within GitLab.
    We won't be using it this time.

Once the settings are complete, click the Save changes button.
At this point, the integration is complete, so let's see it in action.

Creating a Merge Request Linked with Jira

From here, we will actually verify the operation.
Note that the Jira issue to be linked this time is shown in the image below.
Untitled
Also, the method for finding matching issues uses "Jira issue regex," and "MFMZ-[0-9]*" is specified.
Let's begin.
First, create a merge request in the GitLab project linked with Jira.
Next, include one of the following in the comments within the merge request:

  • Resolves MFMZ-6
  • Closes MFMZ-6
  • Fixes MFMZ-6

Since the behavior is the same for all of them, feel free to enter whichever you prefer.
Once the merge request is created, a link to MFMZ-6 will be set, so follow the link to the Jira issue.
If a web link like the one below has been established, the integration is complete.
Finally, merge the merge request and confirm that the issue moves to the status set in "Use custom transitions."

Current Challenges

So far, we have integrated Jira and GitLab and enabled status transitions. The setup itself is simple and can be implemented quickly, but the following challenges remain:

  • It is not possible to specify different destination statuses for different issues upon merging.
  • The target issue does not transition to the "In Progress" status when a branch is created.

However, based on the documentation, it seems these challenges cannot be addressed with the method introduced here. Apparently, it is possible by installing the GitLab for Jira Cloud Jira app. I actually tried installing it, but gave up because I couldn't get the integration with GitLab to work correctly. For some reason, the target GitLab link couldn't be found, and the solution remains unclear. If anyone knows how to fix this, please leave a comment.

As described above, it's far from perfect, but for now, it should significantly reduce the number of times I forget to transition the status after a merge.

Conclusion

In this article, we integrated Jira and GitLab. The settings were simple once understood, but information was scarce, making the initial setup quite challenging. Also, it was tough that both Jira and GitLab documentation were very text-heavy. Nonetheless, I'm glad I could get the integration working for now. In the future, I hope to continue investigating in my spare time to further automate status transitions.

Thank you for reading this far.

References

GitLab's Jira integration method and usage
Explanation of Jira integration settings in GitLab
Qiita article on GitLab and Jira integration (An extremely valuable article that helped me achieve this integration)

Discussion