How Close an Epic when all issues in Epic are closed using Scriptrunner

Fabio Manzoni February 7, 2020

Hi Guys,

I need some help to close my Epic when all issues in Epic are closed, I followed this documentation https://library.adaptavist.com/entity/close-an-epic-when-all-the-issues-under-that-epic-are-closed,

The Scriptrunner shows the aren't errors in my code, but this post function in Stories can't work properly.

Also I did three changes.

final String actionName = "Closed" I changed to final String actionName = "Done"

final String issueLinkName = "Epic-Story Link" I changed to final String issueLinkName = "Epic Link"

*.destinationObject?.findAll { it.status.statusCategory.name != "Complete" } I changed to *.destinationObject?.findAll { it.status.statusCategory.name != "Done" }

Someone used this documentation and worked right?

Also I tried with Done, DONE, done, Epic Name, Epic Link and Issues in Epic

 

Can someone help me?

Thanks, 

Fabio

 

2 answers

1 accepted

0 votes
Answer accepted
Fabio Manzoni February 20, 2020

Here the final code, this worked for me.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.workflow.TransitionOptions

// the name of the action you want to move the issue to
final String actionName = "Done"

// the name of the issue link
final String issueLinkName = "Epic-Story Link"

 


def issueLinkManager = ComponentAccessor.issueLinkManager

def epicIssue = issueLinkManager.getInwardLinks(issue.id).find { it.issueLinkType.name == issueLinkName }?.sourceObject
if (!epicIssue) {
return
}
def workflow = ComponentAccessor.workflowManager.getWorkflow(epicIssue)
def actionId = workflow.allActions.findByName(actionName)?.id

// Find all the linked - with the "Epic-Story Link" link - issues that their status is not completed
def linkedIssues = issueLinkManager
.getOutwardLinks(epicIssue.id)
.findAll { it.issueLinkType.name == issueLinkName }
*.destinationObject?.findAll { it.status.statusCategory.name != "Complete" }

// If there are still open linked issues (except the one in transition) - then do nothing
if (linkedIssues - issue) {
return
}

def issueService = ComponentAccessor.issueService
def issueInputParameters = issueService.newIssueInputParameters()

issueInputParameters.setComment("This Epic closed automatically because all the issues in this Epic are closed.")
issueInputParameters.setSkipScreenCheck(true)

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

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def transitionValidationResult = issueService.validateTransition(loggedInUser, epicIssue.id, actionId, issueInputParameters, transitionOptions)
assert transitionValidationResult.isValid() : transitionValidationResult.errorCollection

def result = issueService.transition(loggedInUser, transitionValidationResult)
assert result.isValid() : result.errorCollection

0 votes
Marc Minten _EVS_
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.
February 10, 2020

Can you explain the reasons for your changes ? I have the impression that you changed things without really knowing what you are changing and what the impact is ?

I changed to final String actionName = "Done" : your epic workflow action to "close" the Epic is called like this ?

I changed to final String issueLinkName = "Epic Link" : I think "Epic Link" is the name of a custom field of issues in epics, but is not link name ? Why do you change this ???

I changed to *.destinationObject?.findAll { it.status.statusCategory.name != "Done" } : I think "Done" is not a valid Jira status category ! Why do you change this ?

Fabio Manzoni February 10, 2020

Hi  @Marc Minten _EVS_ 

Done its my last transition and work flow status.

About "Epic Link" you are right, it's the name of field link, but the link name in Jira is "Issues in Epic" also I tried this name and doesn't work. I did changes trying to catch the rigth name of link and the name showed in script can't work.

Done it's the last statuscategory in Jira.

If I just copy this code https://library.adaptavist.com/entity/close-an-epic-when-all-the-issues-under-that-epic-are-closed, can't work properly here, then I did the changes according with my work flow.

Suggest an answer

Log in or Sign up to answer