Close an Epic & Initiative if respectives Issues are Done

Martin Benavides October 26, 2021

Hi community,

I'm brand new to Groovy Scripts, currently using Jira Server v8.14.0.

I am trying to implement a script post-function using ScriptRunner which will transition an Epic to Done if all childs (Story,Task or Bug) are Done using a Script from Adaptavist Library and it is working. If I use the same script changing the issueLinkType (Parent-Child Link), Initiative will be transitioned to Done if all childs(Epic) are Done.

I have use them separately (one script for Epic & one for Initiative) in the same transition (to Done) and it is working, is it possible to have one script for both Epic&Initiative instead of two for this purpose?

Any guidance would be very appreciated.

Thank you!

https://library.adaptavist.com/entity/close-an-epic-when-all-the-issues-under-that-epic-are-closed

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 actionName = 'Close'

//Name of the resolution children issues should have
final resolutionName = 'Done'

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

def workflow = ComponentAccessor.workflowManager.getWorkflow(issue)
def actionId = workflow.allActions.findByName(actionName)?.id
def linkManager = ComponentAccessor.issueLinkManager

def epicIssue = linkManager.getInwardLinks(issue.id).find { it.issueLinkType.name == issueLinkName }?.sourceObject
if (!epicIssue) {
return
}

// Find all the linked - with the "Epic-Story Link" link - issues that their status is not the same as resolutionName

def linkedIssues = linkManager
.getOutwardLinks(epicIssue.id)
.findAll { it.issueLinkType.name == issueLinkName }
*.destinationObject?.findAll { it.resolution?.name != resolutionName }

// If there are still open linked issues (except the one in transition) - then do nothing

if (linkedIssues - issue) {
return
}

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

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

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

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

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

1 answer

0 votes
Damian Wodzinski
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.
October 27, 2021

May I ask you a question, why this needs to be done in one script? It is working right now, so there is no need for merging them.

Creating one script from two shouldn't be hard. Try to compare logic behind those two and put everything in one file, you will need to watch out for the same variables, because one might override another one.

Martin Benavides October 27, 2021

Fair enough.
I would like to keep it as simple as possible due I currently have many postfunctions on the same transition, and if there is a way to merge this would be great, because, also I am using one more for Story hierarchy, so that makes it 3 script postfunction, but my Groovy skills are very limited to find the right script for this purpose.

 

Thanks for your help Damian. Have a good one!

Suggest an answer

Log in or Sign up to answer