Auto-Transition of JIRA Issues With Bamboo Build Results
You can use Bamboo and Bitbucket integrated with JIRA to automate the status changes of your tickets. This tutorial shows you how.
Join the DZone community and get the full member experience.
Join For FreeIf you are working on a JIRA issue, you need to change the status of the issue every time you perform any activity on your code, like changing or modifying the code or deploying the code for testing. You change the status with a single click, but nowadays, everyone likes automation and for things to be done within a fraction of a second. Also, due to a heavy code base, some builds take a longer time to process, and until then, the developer or manager has to wait to change the status of the JIRA ticket.
Atlassian, the enterprise company providing many useful software development and collaboration tools, has provided the integration of Bamboo builds with JIRA workflows. This helps you to configure a workflow in a JIRA application. Therefore, a JIRA issue can be transitioned to another state in your workflow according to the result of the build process (successful or failed).
The “transition id” of the issue plays a key role in changing the status of the JIRA issue automatically. Every customized workflow has its own unique transition id. Apart from the transition id, which is relevant to the transition of the issue state in JIRA, there is a unique step id for each step name in JIRA.
We can define different JIRA workflows according to organizational need. A JIRA workflow is a set of transitions and statuses which help an issue to move from one state to other. There are some default workflows that cannot be edited, but you can copy and use these workflows to create your own.
You can create your own workflows or import other workflows from Atlassian Marketplace.
Solution to This Problem
Pre-requisites to implement this use case are:
- Task type as Script is required in a Bamboo server.
- Knowledge of how to use or choose the correctRest API for Bamboo and JIRA.
First, you need to get the transition ID of an issue from a workflow. Suppose we have a predefined workflow named “Software Simplified Workflow for Project DEV” applied to any project. This workflow has the below steps:
Step Name (id) |
Linked Status |
Transitions (id) |
To Do (1) |
TO DO |
To Do (11) |
In Progress (6) |
IN PROGRESS |
To Do (11) |
Done (11) |
DONE |
To Do (11) |
In the above workflow, we have Transitions(id), which is unique for all transitions. For the current workflow, it's11 for "TO DO," 21 for "In progress," and 31 for "Done." Another workflow may have other numbers as ids.
We can utilize this transition id for changing the transition of any particular issue.
Solution: Let’s Implement This
Step 1
Create a project in Bamboo and, inside this project, create a couple of plans. Plan 1, with the name “IT,” will have multiple jobs inside it, and inside the default job, we can create multiple tasks.
The screenshot below has a plan with the name “IT.” In this plan, dependencies are added to trigger child plans so that after the completion of this task, the child plan may get a trigger. We can have multiple child plans and, as of now, we have single child plan with the name “Issue_Transition.”
Step 2
Now we need to add the script in Tasks of bamboo plan 2 with the name as “Issue_Transition” and also we need to add trigger as “After plan fail” to execute if Plan key is failing.
This script is responsible for fetching all JIRA issue with status as “In Progress” and transition the issue on the basis of Bamboo build result, either Successful or Failed
If build Result is Successful, all issue from “In Progress” will be transitioned to “Done” state
And if build result is Failed, all issue from “In Progress” will be transitioned to “To Do” State
In order to achieve this one should have knowledge on JSON Rest API and JQ to parse JSON files.
Below script is responsible for the conditional transition of the issue.You need to add this script in bamboo Task type as Script section
You can track all history of transitioning an issue from History tab in JIRA.
#!/bin/bash
#Below mention script is responsible for fetching the issue with "In Progress" status from running JIRA instance with project name as DEV
#message=$(curl -u username: password -X GET -H "Content-Type: application/json" http://<hostname>:8080/rest/api/2/search?%3DDEV%20%20AND%20STATUS%3D%20%22TO%20DO%22 | jq-linux32 ".issues|.[]|.key")
message_issue=$(curl -u username: password -X GET -H "Content-Type: application/json" http://<hostname>:8080/rest/api/2/search?%3DDEV%20%20AND%20STATUS%3D%20%22In%20Progress%22 | jq-linux32 ".issues|.[]|.key")
read -a array <<< $message_issue
issue=${array[0]}
echo $message_issue > get_jira_issue.json
#Below mention script is responsible for checking latest build state in BAMBOO with project name as <bamboo_project_name>-IT and perform transition of issues
#For Software Simplified Workflow for Project DEV transition ids are To Do (11) In Progress (21) Done (31)
message_build_state=$(curl -u username:password -X GET -H "Content-Type: application/json" http://<hostname>:8085/rest/api/latest/result/<bamboo_project_name>-IT?expand&buildstate&)
value=`echo $message_build_state > buildStatus`
ResultbuildState=`cat buildStatus | grep -oP '(?<>)[^<]+' buildStatus | head -n 1`
echo "Result status of latest build is $ResultbuildState"
echo $ResultbuildState> build_result.json
#Now Transition will be done on the basis of build result above
#TRANSITION OF ISSUE FROM “In Progress" or Current state to " To Do" DURING BUILD EXECUTION
if [ "$ResultbuildState" == "Failed”]
then
cat get_jira_issue.json
for i in `cat get_jira_issue.json`
do
issue=`echo $i | tr -d '"'`
echo $issue
message=`curl -u username:password -X POST --data '{"transition":{"id":"11"}}' -H "Content-Type: application/json" http://<hostname>:8080/rest/api/latest/issue/$issue/transitions?.fields
done
fi
if [ "$ResultbuildState" == "Successful" ]
then
#TRANSITION OF ISSUE FROM "In Progress" or Current state to " Done" DURING BUILD EXECUTION
for i in `cat get_jira_issue.json`
do
echo "$i"
issue=`echo $i | tr -d '"'`
message=$(curl -u username:password -X POST --data '{"transition":{"id":"31"}}' -H "Content-Type: application/json" http://<hostname>:8080/rest/api/latest/issue/$issue/transitions?.fields)
done
fi
Benefits:
- Improved working proficiency.
- Reduced the human intervention and for transitioning of the issue.
- No license add-on required.
Opinions expressed by DZone contributors are their own.
Comments