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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Scriptrunner - getting linked issues

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 ?

3 answers

1 accepted

9 votes
Answer accepted
Alexey Matveev
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.
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)
}

Heads up for anyone using this in the future: When getting the linkedIssue for Inward Links, you should grab the source object rather than the destination. Getting the destination object will return your original issue for an inward link. 

So, toward the end of the file, use: 

def linkedIssue = issueLink.getSourceObject()

Like # people like this

@Devan Corona Give that man a bunch of cookies

Like # people like this
Moses Thomas
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 04, 2021

@Devan Corona  Thanks ! for pointing that out ;)

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"

Hi @Chris Shepherd ,

I linked issues of instance A with instance B, while reporting under link column it shows nothing. Do you have any Script to show linked issues in the list?

Like Matthias K_ likes this
Alex
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.
Jan 09, 2024

@Chris Shepherd Thank you for work

0 votes
Alex
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.
Jan 10, 2024

I edited the code a little, maybe it will be useful to you 

 

import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.component.ComponentAccessor
import java.util.List
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.component.ComponentAccessor

def issue = Issues.getByKey('PROJECT-4197')

List results = []

allOutIssueLink = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())

for (i = allOutIssueLink.iterator(); i.hasNext();) {
def issueLink =(IssueLink) i.next()
def linkedIssue = issueLink.getDestinationObject()
String type = linkedIssue.getIssueType().getName()

results += linkedIssue.getKey()
}

return results

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events