It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner - getting linked issues

Chris Shepherd Jan 30, 2018

I have the following code in a Scriptfield:

import com.atlassian.jira.ComponentManager
import groovy.xml.MarkupBuilder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.issue.link.LinkCollectionImpl;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.component.ComponentAccessor;

import org.apache.log4j.Logger
import org.apache.log4j.Level

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


String results = "";
def valid = false;
List<IssueLink> allOutIssueLink = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
;
def linkedIssue = issueLink.getDestinationObject()
String type = linkedIssue.getIssueType().getName();

log.debug(type)
}

 

When I run it against an issue with one linked issue & 2 sub-tasks,  it only lists the subtasks.  How do I get the linked issue ?

2 answers

1 accepted

1 vote
Answer accepted
Alexey Matveev Community Leader Jan 30, 2018

I guess your linked issue is inward. You should make two cycles: one with ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId()) and the other one with ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId()). It would be something like this

import com.atlassian.jira.ComponentManager
import groovy.xml.MarkupBuilder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.issue.link.LinkCollectionImpl;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.component.ComponentAccessor;

import org.apache.log4j.Logger
import org.apache.log4j.Level

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


String results = "";
def valid = false;
List<IssueLink> allOutIssueLink = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
;
def linkedIssue = issueLink.getDestinationObject()
String type = linkedIssue.getIssueType().getName();

log.debug(type)
}

List<IssueLink> allInIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allInIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
;
def linkedIssue = issueLink.getDestinationObject()
String type = linkedIssue.getIssueType().getName();

log.debug(type)
}
0 votes
Chris Shepherd Jan 31, 2018

Thanks Alexey,
You certainly pointed me in the right direction, except with inward links I needed the source object not the destination object.
cheers
Chris

For others, here is my code that checks to see if we have a change control linked to an issue:

 

import com.atlassian.jira.ComponentManager
import groovy.xml.MarkupBuilder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.issue.link.LinkCollectionImpl;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.component.ComponentAccessor;


String results = "";
def valid = false;

List<IssueLink> allOutIssueLink = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
def linkedIssue = issueLink.getDestinationObject()
String type = linkedIssue.getIssueType().getName();
if(type.contains("Change"))
{
valid = true;
results += linkedIssue.getKey() + "<br />"
}
}

if(valid){return results;}

allOutIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
def linkedIssue = issueLink.getSourceObject()
String type = linkedIssue.getIssueType().getName();
if(type.contains("Change"))
{
valid = true;
results += linkedIssue.getKey() + "<br />"
}
}
if(valid){return results;}

return "No change control"

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Webinar alert! An IT Admin's Guide to Securing Collaboration at Scale

Hello, Atlassian Community! My name is Dave Meyer and I'm a Principal Product Manager at Atlassian. I wanted to give this community a heads up about an upcoming Webinar that might be of interest...

110 views 2 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you