Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Transition Inward Linked issues

Michael Schultz October 8, 2019

Hello. I have a script that I've been working for transitioning the linked issues of the parent issue. Basic logic is: when a parent issue is Resolved, go through each linked issue and resolve those as well. 

I am able to get all the way to validating the transition when it fails. Here is my script:

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkImpl;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParametersImpl

IssueService issueService = ComponentAccessor.getIssueService()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject(event.issue.key)
def actionId = 2 // change this to the step that you want the issues to be transitioned to
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def type = issue.getIssueType().getName()

log.debug("The issue type is: " + issue.getIssueType().name)

if (type.toString() == 'Task') {

List<IssueLink> allInIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId());
allInIssueLink?.each { linked ->
def linkedIssue = linked.getDestinationObject()
Long linkedId = linkedIssue.id
def transitionValidationResult = issueService.validateTransition(currentUser, linkedId, actionId,new IssueInputParametersImpl())

if (transitionValidationResult.isValid()) {
def transitionResult = issueService.transition(currentUser, transitionValidationResult)

if (transitionResult.isValid())
{ log.debug("Transitioned issue $issue through action $actionId") }

else
{ log.debug("Transition result is not valid") }
}

else {
log.debug("The transitionValidation is not valid: " + transitionValidationResult)
}
}
}

 

3 answers

1 accepted

0 votes
Answer accepted
Michael Schultz October 10, 2019

I found the solution here. The line:

def linkedIssue = linked.getDestinationObject()

Needed to be changed to: 

def linkedIssue = linked.getSourceObject()

This gets the correct InwardLinked (Source) issues. Instead of getting the InwardLinked (Destination) issue or the "Parent" issue, which was not needed.

0 votes
Tuncay Senturk _Snapbytes_
Community Champion
October 8, 2019

I think the problem is getting the linked issues. 

You should get linked issue as below

def linkedIssue = linked.getDestinationObject()

 And then use issueId as linkedIssue.id

I hope I am not missing anything

Michael Schultz October 8, 2019

@Tuncay Senturk _Snapbytes_ 

Unfortunately, it's still not working, getting the same invalid result response. Here is the updated code with your suggestions:

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkImpl;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParametersImpl

IssueService issueService = ComponentAccessor.getIssueService()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject(event.issue.key)
def actionId = 2 // change this to the step that you want the issues to be transitioned to
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def type = issue.getIssueType().getName()

log.debug("The issue type is: " + issue.getIssueType().name)

if (type.toString() == 'Task') {

List<IssueLink> allInIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId());
allInIssueLink?.each { linked ->
def linkedIssue = linked.getDestinationObject()
Long linkedId = linkedIssue.id
def transitionValidationResult = issueService.validateTransition(currentUser, linkedId, actionId,new IssueInputParametersImpl())

if (transitionValidationResult.isValid()) {
def transitionResult = issueService.transition(currentUser, transitionValidationResult)

if (transitionResult.isValid())
{ log.debug("Transitioned issue $issue through action $actionId") }

else
{ log.debug("Transition result is not valid") }
}

else {
log.debug("The transitionValidation is not valid: " + transitionValidationResult)
}
}
}

 
Also, if it helps, the output of allInIssueLink in my code above is:

[com.atlassian.jira.issue.link.IssueLinkImpl@77985b01[id=227305,sourceId=523908,destinationId=406825,issueLinkType=10000], com.atlassian.jira.issue.link.IssueLinkImpl@56830a75[id=227304,sourceId=523907,destinationId=406825,issueLinkType=10000]]
Tuncay Senturk _Snapbytes_
Community Champion
October 8, 2019

And what's the error message?

Michael Schultz October 8, 2019

@Tuncay Senturk _Snapbytes_ 

It's 

The transitionValidation is not valid
Tuncay Senturk _Snapbytes_
Community Champion
October 8, 2019

Can you please replace the below code

log.debug("The transitionValidation is not valid: " + transitionValidationResult)

with this one

log.error("The transitionValidation is not valid: " 
+ transitionValidationResult?.errorCollection?.errorMessages?.join(","))

hopefully, we might understand the actual cause

Michael Schultz October 10, 2019

@Tuncay Senturk _Snapbytes_ 

Thanks for your direction here. The logging you provided ultimately led me to discovering where my issue lied.

Tuncay Senturk _Snapbytes_
Community Champion
October 10, 2019

Yeah, debugging always saves time.

I always confuse the directions, inward - source, outward - destination .

That's why I wanted to log it and see what was wrong.

Anyway, I'm glad to hear that you achieved.

0 votes
Tuncay Senturk _Snapbytes_
Community Champion
October 8, 2019

Hi  @Michael Schultz 

Sorry but could you please clarify? What's the problem here?

Tuncay Senturk _Snapbytes_
Community Champion
October 8, 2019

I did not check the code btw.

Before diving into the code, I just wanted to have more info.

Thanks

Michael Schultz October 8, 2019
def transitionValidationResult = issueService.validateTransition(currentUser, linkedId, actionId,new IssueInputParametersImpl())

if (transitionValidationResult.isValid()) {
def transitionResult = issueService.transition(currentUser, transitionValidationResult)

if (transitionResult.isValid())
{ log.debug("Transitioned issue $issue through action $actionId") }

else
{ log.debug("Transition result is not valid") }
}

else {
log.debug("The transitionValidation is not valid: " + transitionValidationResult)
}
}
}

This is where it fails. Not a valid result. The Step ID of 2 is in fact my Closed status. Also, the Long ID of the child issues come back just fine as well. 

Suggest an answer

Log in or Sign up to answer