ScriptRunner how to automatically transition an Epic to Completion when all its children are complet

Roberto L
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 22, 2017

Hello Community,

I am trying to develop a ScriptRunner Script that will allow me to transition an Epic to completion once all of the Stories/Tasks/Defects/Subtasks under it are completed.

Could someone point me in the right direction to achieve such functionality.

I appreciate your help!

-Roberto

1 answer

1 accepted

4 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 28, 2017

Hi Roberto, 

Hopefully is not too late but I crafted a post function, that should be put into the "Closed" step in the workflow. Hopefully you will understand the logic from the comments added in the code, if not, please shout :)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.workflow.TransitionOptions

def issue = issue as MutableIssue
def user = ComponentAccessor.userManager.getUserByKey("admin")

parentIssue = issue.isSubTask() ? issue.parentObject :
ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.id)?.find {it.issueLinkType.name == "Epic-Story Link"}?.sourceObject

if (!parentIssue) {
// there is not any Epic issue as source so do nothing
return
}

log.debug "Parent issue $parentIssue"

// check if the parent issue has any subtasks that are not closed
def openSubtasks = parentIssue?.subTaskObjects?.findAll {it.status.name != "Done"}

log.debug "Open subtasks $openSubtasks"
// if there are subtasks that are not open (apart from the one in transition) - then do nothing
if (openSubtasks - issue) {
return
}

// get all the open linked (with the epic-story link) issues and are not closed
def linkedIssues = ComponentAccessor.getIssueLinkManager().getOutwardLinks(parentIssue.id)?.findAll {it.issueLinkType.name == "Epic-Story Link"}*.destinationObject?.findAll {it.status.name != "Done"}

log.debug "Open linded issues $linkedIssues"
// if there are linked issues that are open (apart from the one in transition) - then do nothing
if (linkedIssues - issue) {
return
}

// if the script reached here means that all the subtasks and the linked - with the Epic Story link - issues are closed
// therefore transit the issue
transitIssue(parentIssue, 41, user)

def transitIssue(Issue issue, int actionId, ApplicationUser cwdUser) {
def issueService = ComponentAccessor.getIssueService()

IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.setComment("This is a comment")
issueInputParameters.setSkipScreenCheck(true)

def transitionOptions= new TransitionOptions.Builder()
.skipConditions()
.skipPermissions()
.skipValidators()
.build()

def transitionValidationResult =
issueService.validateTransition(cwdUser, issue.id, actionId, issueInputParameters, transitionOptions)

if (transitionValidationResult.isValid()) {
return issueService.transition(cwdUser, transitionValidationResult).getIssue()
}

log.error "Transition of issue ${issue.key} failed. " + transitionValidationResult.errorCollection
}

Please let me know if this does the trick.

Regards, 

Thanos

Jerry Wang January 4, 2019

Hello Thanos,

 

thank you for sharing your programming code. I've tried it but it didn't work at our company. I guess maybe it is because of the action id? May I ask how could I find the action id, or why you used 41 in the programming?

Thank you a lot!

Best,

Jerry

Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 4, 2019

Hi Jerry 

In order to find the transition id of the step you have to navigate to the desired workflow and then change the display from diagram to Text and then you will see a column with Transition names and their ids, something like

Screenshot 2019-01-04 at 11.06.05.png

Like # people like this
hai yan Wang January 4, 2019

Hi Thanos,

but unfortunately it still didn't work. I have no idea why not :(

thanks 

Best,

Jerry

Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 4, 2019

Can you please check your application logs for errors ?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events