Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Display Local and Remote Issue Links

Created 1 year ago, Updated 26 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.14)
compatibility bullet
ScriptRunner For Jira (6.18.0)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.issue.link.RemoteIssueLinkManager

// Get the components
def issueManager = ComponentAccessor.issueManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def remoteIssueLinkManager = ComponentAccessor.getComponent(RemoteIssueLinkManager)

// Define the params to get an issue and filter the issue links by type
final issueKey = 'TEST-1'
final issueLinkTypeName = 'Blocks'

// Get the issue
def issue = issueManager.getIssueByCurrentKey(issueKey)

// Get the issue links to other issues
def issueLinks = issueLinkManager.getOutwardLinks(issue.id)
def filteredLinks = issueLinks.findAll { it.issueLinkType.name == issueLinkTypeName }

// Collect the HTML links pointing to the linked issues
def baseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)
def linkedIssuesHtmlLinks = filteredLinks.collect { issueLink ->
    def issueUrl = "${baseUrl}/${issueLink.destinationObject.key}"
    "<a href='${issueUrl}'>${issueLink.destinationObject.key}</a>"
}

// Collect the HTML links pointing to the remote links
def remoteLinks = remoteIssueLinkManager.getRemoteIssueLinksForIssue(issue)
def remoteLinksHtml = remoteLinks.collect { remoteLink ->
    "<a href='${remoteLink.url}'>${remoteLink.title}</a>"
}

// Display the links
"""<p>Issue links:</p>
<ul>
    ${linkedIssuesHtmlLinks.collect { "<li>${it}</li>" }.join('\n')}
</ul>
<p>Remote links:</p>
<ul>
    ${remoteLinksHtml.collect { "<li>${it}</li>" }.join('\n')}
</ul>"""
Having an issue with this script?
Report it here