Get linked issues of a linked issue

zaharovvv_suek_ru
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.
July 11, 2018

I have three issues - "issueA", "issueB", "issueC". These issues have the following links:

- "issueA" has two inward links to "issueB", "issueC"

- "issueB", "issueC" has outward links to "issueA"

Briefly, I have issue B. Then I want to get all linked issues if issue B such as issue A. Then I want to get all linked issues of issue A and they are issueB and and issue C. 

I am running this code in Post-Function of transition "In Progress" from issue B to get outward links of "issueA", but it returns issue A and it is wrong:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.IssueManager
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.type.EventDispatchOption;
import com.opensymphony.workflow.loader.ActionDescriptor
import com.opensymphony.workflow.loader.StepDescriptor
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParametersImpl

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def issue = ComponentAccessor.getIssueManager().getIssueObject("issueB")
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
issueLinkManager.getOutwardLinks(issue.id).each{ issueLink ->
def linkedIssue = issueLink.getDestinationObject()
if(linkedIssue.getIssueType().getName() == 'Epic') {
log.info('1.1 ***EPIC.*** Issue type is Epic. The epic issue is: '
+ linkedIssue
+ '***EPIC***') //Here we get issue A. It is ok
log.info('1.2 linkedIssue.id: ' + linkedIssue.id)
def linkManager = ComponentAccessor.getIssueLinkManager()
log.info("1.3 * linkManager*")
linkManager.getInwardLinks(linkedIssue.id).each{ fooLink ->
def fooIssue = fooLink.getDestinationObject();
log.info('2.2.* FooIssue: ' + fooIssue
+ '. Type of fooIssue is: ' + fooIssue.issueType.name + '*')
} }
}

However "linkManager"(issueLinkManager) returns "issue A", but "linkManager"(issueLinkManager) should return "issueB" and "issueC" cause I am giving to it 'issueA'.

The Logs looks like this:

2018-07-11 17:29:27,052 INFO [acme.CreateSubtask]: 1.1 ***EPIC.*** Issue type is Epic. The epic issue is: issueA***EPIC***
2018-07-11 17:29:27,052 INFO [acme.CreateSubtask]: 1.2 linkedIssue.id: 28685
2018-07-11 17:29:27,052 INFO [acme.CreateSubtask]: 1.3 * linkManager*
2018-07-11 17:29:27,054 INFO [acme.CreateSubtask]: 2.2.* FooIssue: issueA. Type of fooIssue is: Epic*

 

Guys, how can I read correctly inward links of linked issue 'issueA'?

2 answers

1 accepted

1 vote
Answer accepted
zaharovvv_suek_ru
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.
July 13, 2018

Big thanks to ScriptRunner support! 

Mistake was to use getDestinationObject() instead of getSourceObject().

The whole code looks like this:

def issue = ComponentAccessor.getIssueManager().getIssueObject("issueB")
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
issueLinkManager.getOutwardLinks(issue.id).each{ issueLink ->
def linkedIssue = issueLink.getDestinationObject()
if(linkedIssue.getIssueType().getName() == 'Epic') {
log.info('1.1 ***EPIC.*** Issue type is Epic. The epic issue is: '
+ linkedIssue
+ '***EPIC***') //Here we get issue A. It is ok
log.info('1.2 linkedIssue.id: ' + linkedIssue.id)
def linkManager = ComponentAccessor.getIssueLinkManager()
log.info("1.3 * linkManager*")
linkManager.getInwardLinks(linkedIssue.id).each{ fooLink ->
def fooIssue = fooLink.getSourceObject();
log.info('2.2.* FooIssue: ' + fooIssue
+ '. Type of fooIssue is: ' + fooIssue.issueType.name + '*')
} }
}
Omprakash Thamsetty
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 26, 2019

@zaharovvv_suek_ruI have exact requirement. I needs to check linked Task status for 2 issues and then 3rd needs to close.

Ex:

Issue A - (Story) --> Linked to Issue B (TASK) and Issue C (TASK) and Issue D (Request)

So

I needs Close Issue D if Issue B and Issue C closed that are linked to Issue A. So I am thinking to place this code on TASK workflow postfunction on Close.

How I can check both Issue B and Issue C status in  script ?

Thanks,

Om

0 votes
Cristian Rosas [Tecnofor]
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.
July 11, 2018

Hi zaharov,

If you get outward links for A and the result is B, then if you get inward link for B, the result will be A... isn't it?

 

Editted: Oh I misread the coding, gonna check again.

You made log.info for linkedIssue and linkedIssue.id, shouldn't you use fooLink?

 

zaharovvv_suek_ru
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.
July 11, 2018

@Cristian Rosas [Tecnofor] I've updated my question. Please, see my question again. Thanks in advance.

Cristian Rosas [Tecnofor]
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.
July 11, 2018

Ok, now I don't see why it would say that the inward link of IssueA is IssueA :(

Just in case, even if this is an epic you are using links, not epic link and subtasks? I'm asking because I saw this:

INFO [acme.CreateSubtask]

I'll try to replicate it. Can you tell me where is this ubicated? (post-function in workflow creation step or transition step, listener....)

zaharovvv_suek_ru
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.
July 12, 2018

I am running this code in Post-Function of transition "In Progress" from issue B to get outward links of "issueA", but it returns issue A and it is wrong.

This info INFO [acme.CreateSubtask] is shown by 

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

I've taken this code from here Adaptivist documentation:

https://www.adaptavist.com/doco/display/SFJ/Set+logging+to+help+debug+your+scripts

Suggest an answer

Log in or Sign up to answer