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

Control Available Issue Link Types

Features
Behaviours
Created 1 year ago, Updated 15 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.issue.link.IssueLinkTypeManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

def linkTypesField = getFieldById("issuelinks-linktype")

def allowedOutwardTypesNames = ["blocks", "relates to", "causes"]
def allowedInwardTypesNames = ["is blocked by", "relates to", "is caused by"]

def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def allLinkTypes = issueLinkTypeManager.getIssueLinkTypes(false)

// Get the outward link names you want
def outwardAllowedLinks = allLinkTypes.findAll { linkType ->
    linkType.outward in allowedOutwardTypesNames
}.collectEntries { linkType ->
    [(linkType.outward): linkType.outward]
}
// Get the inward link names you want
def inwardAllowedLinks = allLinkTypes.findAll { linkType ->
    linkType.inward in allowedInwardTypesNames
}.collectEntries { linkType ->
    [(linkType.inward): linkType.inward]
}

// Combine maps of allowed link direction names
def allowedLinks = outwardAllowedLinks + inwardAllowedLinks
log.debug("Allowed Links = $allowedLinks")

// The options for the 'issuelinks-linktype' field have to be set in this structure: [blocks:blocks, relates to:relates to]
// because the html structure of the field uses the actual link direction name as the value property.
linkTypesField.setFieldOptions(allowedLinks)
Having an issue with this script?
Report it here