xperexpo
Published in

xperexpo

Change parent & child issue states with script runner in Jira

Customizing Jira workflows is very funny as well as annoying sometimes. During our journey to a new team-forming experience, we decided to create a new customized Jira workflow structure. I tried some out-of-box solutions of Jira and some other plugins also. Despite having easy-of-use features those options didn’t meet my expectations. I was in big trouble but lighten up folks, Script Runner helped me in many ways. Whatever feature is given the best option is mostly to create your own script.

As you know script runner is also free for up to 10 users in your Jira organization. So the first step is installing Adaptivist Script runner to your Jira account.

Script runner supports Groovy language so you better be familiar or check out syntax https://groovy-lang.org/documentation.html

ScriptRunner also provides a Scrip Console for playground coding.

In my case, I have Epics, Story, and Subtasks that should be transited automatically by some scenarios.

For this blog, let me take this scenario: When all subtasks are done move Story to the “Ready for test” state.

Check out your Subtask Workflow from Jira. As for ours, we have 5 states. In edit mode click the script required to transition and add a “Post Function”

Select “Script Runner Post Function” then select the “Run Script” option from the opening page.

Now you're in, follow the white rabbit. I directly write my code into the “Additional Code” section but you may also use the “Condition” part for some conditional actions.

“issue” is default variable for initiated jira item in your workflow. So you can use this variable to get properties for current item.

1️⃣ First, define your variables.

def doneIssueTypeName=’Done’
def nextStepIssueTypeName=’Ready for Test’
def parentissue = issue.fields.parent
def progressingSubTaskCount=0
def returnstring=’’

2️⃣ Get your parent story with properties. (don’t forget null check since you may have some orphan subtasks)

if (parentissue!=null){
def story=get(“rest/api/2/issue/${parentissue.key}”)
.header(“Accept”, “application/json”)
.asObject(Map)
.body

Please check Jira Developer documentation for Rest services definitions https://developer.atlassian.com/cloud/jira/platform/rest/v3/intro/

3️⃣ If all subtasks are done then you may move the story. Note: you may use logger.warn for console logging

progressingSubTaskCount=story.fields.subtasks.findAll{(it.fields.status.name!=doneIssueTypeName)}.size()
logger.warn “progressingSubTaskCount : ${progressingSubTaskCount}”
if (progressingSubTaskCount==0){

4️⃣ The next step is listing Story issue type transitions

def storyIssueTransition = get(“rest/api/2/issue/${story.key}/transitions”)
.header(“Accept”, “application/json”)
.asObject(Map)
.body
.transitions.find {
(it as Map).to.name == nextStepIssueTypeName
} as Map

5️⃣ Finally if the “Ready for test” transition appears you can peacefully move the transition. Bulls in the eye 🎯

if (storyIssueTransition!=null){
def transitionIssue = post(“/rest/api/2/issue/${story.key}/transitions”)
.header(“Content-Type”, “application/json”)
.body([transition: [id: storyIssueTransition.id]])
.asObject(Map)
returnstring +=”Parent issue status moved to ${nextStepIssueTypeName}”
}

❗️Note: you may see editor errors, but don't mind, this won’t prevent you from compiling the script.

You can check the whole script from my github account

See you on the next blog

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store