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

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

This widget could not be displayed.

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

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published Jul 25, 2018 in Marketplace Apps

Jira Cloud and Bitbucket Cloud Integration with Microsoft Teams

One of the newest products in the Microsoft family - Microsoft Teams,  is a chat-based hub for teamwork that integrates all the people, content, and tools your team needs to be more engaged and ...

733 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you