Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

with using Groovy script How to automatically close all linked issues when parent issue closed

Nagaraju Reddy August 22, 2018

HI team,

Is there any way to close linked issues automatically when I closed parent. If yes, could you please provide the achieved code.

Eg: see below screen shot: I have linked SP-1 to FR-1, I have closed SP-1 but FR-1 not closed I need to close this when I closed SP-1.

Thanks
Nagarajscriprt.PNG

3 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
August 22, 2018

I think this should work:

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

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueService = ComponentAccessor.getIssueService()
def currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def parameters = issueService.newIssueInputParameters()
def workflowManager = ComponentAccessor.getWorkflowManager()

def transitionOptions = new TransitionOptions.Builder()
.skipConditions()
.skipPermissions()
.skipValidators()
.build()
issueLinkManager.getOutwardLinks(issue.id)?.each {
linkedIssue ->
def jiraWorkflow = workflowManager.getWorkflow(linkedIssue.destinationObject)
def closeAction = jiraWorkflow.getAllActions()?.find { it.name.toString() == "Close"} // The "Close" transition name
def actionId
if (closeAction) {
actionId = closeAction.getId()
}

def result = issueService.validateTransition(currentUser, linkedIssue.destinationObject.id, actionId, parameters, transitionOptions)
if (result.isValid()) {
def issueResult = issueService.transition(currentUser, result)
if (!issueResult.isValid()) {
log.warn("Failed to transition ${linkedIssue.destinationObject.key}, errors: ${issueResult.errorCollection}")
} else {
issueResult
}
} else {
log.warn("Couldn't transition ${linkedIssue.destinationObject.key}, errors: ${result.errorCollection}")
}

}

 But you should have a global transition to Close Status in the linked Issues Workflows. If you dont want users to see this transition, you can add one condition to the transition to hide it.

Nagaraju Reddy August 24, 2018

HI 

perfect it is working good.

Thanks

Nagaraju

Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
August 24, 2018

Could you please mark this response as accepted? Thank you :)

0 votes
Mark Markov
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, 2018

Hello @Nagaraju Reddy

Here is example of code that will transition linked issue:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.jira.transition")
log.setLevel(Level.DEBUG)

def issueService = ComponentAccessor.getIssueService()
def linkType = ["relates to"]
// 6.x validateTransition wants a User, so we have to use getDirectoryUser()
// 7.x validateTransition wants an ApplicationUser, so remove the .getDirectoryUser() after we upgrade
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkMgr = ComponentAccessor.getIssueLinkManager()
// Look through the outward links
log.debug("commence for loop")
for (IssueLink link in linkMgr.getOutwardLinks(issue.id)) {
log.debug("entered for loop")
def destIssue = link.getDestinationObject()
// Does the name of the link match "relates to" ?
if (linkType.contains(link.issueLinkType.name)) {
log.debug("relates to is the link type")
def destStatusObject = destIssue.getStatus()
// Is the status of the linked issue "Display" ?
if (destStatusObject.name == "In Testing") {
// Prepare our input for the transition
log.debug("Status == Display")
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.with
{
//setComment("Cancelled Vacation Request by HR")
setSkipScreenCheck(true)
}

// Validate transitioning the linked issue to "Signs Needed"
def validationResult = issueService.validateTransition(user, destIssue.id, 71, issueInputParameters)
if (validationResult.isValid()) {
log.debug("Validation Result is valid")
// Perform the transition
def issueResult = issueService.transition(user, validationResult)
if (!issueResult.isValid()) {
log.debug("Failed to transition task ${destIssue.key}, errors: ${issueResult.errorCollection} ")
}
} else {
log.debug("Could not transition task ${destIssue.key}, errors: ${validationResult.errorCollection}")
}
} else {
log.debug("Skipping link: ${link.issueLinkType.name} ${destIssue.key} ${destStatusObject.name}(wrong status) ")
}
} else {
log.debug("Skipping link: ${link.issueLinkType.name} ${destIssue.key} (wrong type) ")
}
}

But there are some moments that you will need to correct.

First one

You will need a transition on linked issue that goes to destination status.

Second one

You will need to correct ID of this transition in this line

def validationResult = issueService.validateTransition(user, destIssue.id, 71, issueInputParameters)

71 - is id of transition

And third one
Link must be outward.

Nagaraju Reddy August 24, 2018

Thanks Mark, It is working good

Kumar
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 12, 2018

Hi All,

Instead of closing an linked issue i want to close my sub-tasks  In my project i have like 4 different sub-tasks is this query will work for the my issue??

if it is not could you prove me the query for closing sub-tasks automatically when the Issue is closed.

Thanks,

Phani

 

0 votes
Deleted user August 22, 2018

Hi @Nagaraju Reddy,

You will be able to close all the sub-tasks automatically when a parent issue is closed using the scripts available in this page. As mentioned in the document you should change the action ID based on the workflow you are using.

If you are looking for closing linked issues when the main issue gets closed you might find this link helpful.

 

Hope this helps!!

Regards,

Priyanka

TAGS
AUG Leaders

Atlassian Community Events