Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

Trying to fetch all linked issues linktype outward & inward description of current parent issue

Sam October 20, 2022

Hi All,

I need help in scriptrunner :

 Trying to fetch all linked issues linktype outward & inward description of current issue's parent ticket.

And join together of same linktype in json format .I have mentioned below example.

For example :

{ "Includes" : [ "ABC-123", "ABC-334", ], "Relates" : [ "XYZ-456", "XYZ-118", ], }

 

Appreciate your help !

 

Here is the code snippet which i have written  but output is not in expected format :

 

import com.atlassian.jira.issue.fields.rest.json.JsonData
import groovy.json.JsonBuilder
import org.jdom.Content
import groovy.json.JsonOutput
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.Attachment
import org.apache.commons.io.FilenameUtils
import com.atlassian.jira.util.io.InputStreamConsumer
import org.springframework.util.StreamUtils
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.URIBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseDecorator
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.Response
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import java.io.ByteArrayInputStream
import java.math.MathContext
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkImpl
import com.atlassian.jira.issue.link.IssueLinkTypeImpl

def issueManager = ComponentAccessor.getIssueManager()
def attachmentManager = ComponentAccessor.getAttachmentManager()
def mutableIssue = issueManager.getIssueObject("ABC-123")
//def mutableIssue = issue
def parentIssue = mutableIssue.parentObject
log.warn("parentIssue : ${parentIssue}")



def IssueLinkManager = ComponentAccessor.getIssueLinkManager()
def links = "";
List allOutIssueLink = IssueLinkManager.getInwardLinks(mutableIssue.parentObject.getId())
for (Iterator outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
def linkedIssue = issueLink.getSourceObject();
if ( (issueLink.issueLinkType.getOutward()) != ""){
links = links + "," +"[" + issueLink.issueLinkType.getOutward()+" : " + linkedIssue.key +"]"
}
}
allOutIssueLink = IssueLinkManager.getOutwardLinks(mutableIssue.parentObject.getId())
for (Iterator OutIterator = allOutIssueLink.iterator(); OutIterator.hasNext();) {
IssueLink issueLink = (IssueLink) OutIterator.next();
def linkedIssue = issueLink.getDestinationObject();
log.warn("${issueLink.issueLinkType.getName()}")

if ( (issueLink.issueLinkType.getInward()) != ""){
links = links + ","+ "[" +issueLink.getIssueLinkType().getInward()+" : " + linkedIssue.key + "]"
}
}
log.warn("Links : ${links}")

def json = JsonOutput.toJson(links)
def json_beauty = JsonOutput.prettyPrint(json)

log.warn("json_beauty : ${json_beauty}")

 

 

Output :

 

json_beauty : ",[Includes : ABC-123],[Includes : ABC-342],[relates : XYZ-123],[relates to : XYZ-654],[Includes : ABC-187],[Block by : XYZ-38],[Includes : ABC-214],[jira_subtask_inward : XYZ-146],[jira_subtask_inward : XYZ-1457]

 

 

2 answers

1 accepted

0 votes
Answer accepted
aas
Contributor
October 21, 2022

Hi @Sam

Perhaps this is not at all optimal and under certain conditions it may not work but try

import com.atlassian.jira.issue.fields.rest.json.JsonData
import groovy.json.JsonBuilder
import org.jdom.Content
import groovy.json.JsonOutput
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.Attachment
import org.apache.commons.io.FilenameUtils
import com.atlassian.jira.util.io.InputStreamConsumer
import org.springframework.util.StreamUtils
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.URIBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseDecorator
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.Response
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import java.io.ByteArrayInputStream
import java.math.MathContext
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkImpl
import com.atlassian.jira.issue.link.IssueLinkTypeImpl

def attachmentManager = ComponentAccessor.getAttachmentManager()
def issue = ComponentAccessor.getIssueManager().getIssueObject("TEST-123")

def IssueLinkManager = ComponentAccessor.getIssueLinkManager()
def links = "[";
List allOutIssueLink = IssueLinkManager.getInwardLinks(issue.getId())
links += "\"${allOutIssueLink[0].issueLinkType.getOutward()}\": "
links += "["
int i = 0
allOutIssueLink.each{link ->
    if (i == 0){
        links +=link.getSourceObject().key
        i++
    } else{
        links += ", " + link.getSourceObject().key
        i++
    }
}
links += "], "

log.warn(links)
allOutIssueLink = IssueLinkManager.getOutwardLinks(issue.getId())
links += "\"${allOutIssueLink[0].issueLinkType.getInward()}\": "
links += "["
i = 0
allOutIssueLink.each{link ->
    if (i == 0){
        links +=link.getSourceObject().key
        i++
    } else{
        links += ", " + link.getSourceObject().key
        i++
    }
}
links += "]"
links += "]"

log.warn("Links : ${links}")

def json = JsonOutput.toJson(links)
def json_beauty = JsonOutput.prettyPrint(json)

log.warn("json_beauty : ${json_beauty}")
Sam October 21, 2022

Hi,

Thank you for the help. I tried in my instance and getting the below error.

Result.PNGlog.PNG

aas
Contributor
October 21, 2022

Hi, you have this error because issue which you want get links doesn't have inward links. Look at picture. Do you have both types of links in your issue?

123.png

If no you can add some check like this

def attachmentManager = ComponentAccessor.getAttachmentManager()
def issue = ComponentAccessor.getIssueManager().getIssueObject("TESTTEST-176")

def IssueLinkManager = ComponentAccessor.getIssueLinkManager()
def links = "[";
List allOutIssueLink = IssueLinkManager.getInwardLinks(issue.getId())
if (allOutIssueLink ){
links += "\"${allOutIssueLink[0].issueLinkType.getOutward()}\": "
links += "["
int i = 0
allOutIssueLink.each{link ->
if (i == 0){
links +=link.getSourceObject().key
i++
} else{
links += ", " + link.getSourceObject().key
i++
}
}
links += "], "
}
log.warn(links)

allOutIssueLink = IssueLinkManager.getOutwardLinks(issue.getId())
if (allOutIssueLink ){
links += "\"${allOutIssueLink[0].issueLinkType.getInward()}\": "
links += "["
i = 0
allOutIssueLink.each{link ->
if (i == 0){
links +=link.getSourceObject().key
i++
} else{
links += ", " + link.getSourceObject().key
i++
}
}
links += "]"
links += "]"
}


0 votes
Sam October 24, 2022

Thank you very much.It worked.

Suggest an answer

Log in or Sign up to answer