Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Restricting 'Linked Issue' relationship

Karl Samson
Contributor
June 4, 2024

Hi All, we have a requirement to restrict the Linked Issue relationship on a certain project to 'Contains' and 'Contained in'. We initially copied some code from Adaptavist  - Control Available Issue Link Types - Adaptavist Library

However, copying the full code and modifying for our required relationships, throws up a text type error. When we removed the last line of code, ScriptRunner likes it, and executes successfully, but it doesn't actual work!?

Our code:-

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 = ["contains"]
def allowedInwardTypesNames = ["is contained in"]

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.
Code removed from the very bottom of the script:" linkTypesField.setFieldOptions(allowedLinks)".
Any help getting the code to work would, as always, be greatly appreciated! 
Thanks All.

1 answer

1 accepted

1 vote
Answer accepted
Florian Bonniec
Community Champion
June 4, 2024

Hi @Karl Samson 

 

What is the error returned ?

 

Regards

Karl Samson
Contributor
June 5, 2024

Hi Florian, there is no error. The script apparently runs successfully, but it just doesn't do as requested. i.e It DOES NOT restrict the use of Link relationships!?

Florian Bonniec
Community Champion
June 5, 2024

Before continuing investigating, are you speaking about More > Link or the Linked Issue field ?

Karl Samson
Contributor
June 6, 2024

Linked Issue field.

Linked Issue.jpg

Florian Bonniec
Community Champion
June 6, 2024

Did you set the script as Initialiser and set the proper Mapping to enable the Behaviour on it ?

This one works for me 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

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)
FormField linkTypes = getFieldById("issuelinks-linktype")

def allowedOutwardTypes = ["contains"]
def allowedInwardTypes = ["is contained in"]
IssueLinkTypeManager issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)

//get the outward link names you want

Map outwardAllowedLinks = issueLinkTypeManager.getIssueLinkTypes(false).findAll{ it.outward in allowedOutwardTypes }.collectEntries{

    [it.outward,it.outward]

}

//get the inward link names you want

Map inwardAllowedLinks = issueLinkTypeManager.getIssueLinkTypes(false).findAll{ it.inward in allowedInwardTypes }.collectEntries{

    [it.inward,it.inward]

}

def allowedLinks = outwardAllowedLinks << inwardAllowedLinks

log.debug("Allowed Links = $allowedLinks")

linkTypes.setFieldOptions(allowedLinks)
 

 

Regards

Karl Samson
Contributor
June 6, 2024

Yes, I think so. However, my Script Checker still has a problem with the last line of code, as before!?

Florian Bonniec
Community Champion
June 6, 2024

It should only be a warning no ? It's because we did not use type for the list. It should not impact runtime.

Karl Samson
Contributor
June 6, 2024

I think it is coming up as an error?Linked Issue_Error.jpg

Florian Bonniec
Community Champion
June 6, 2024

Type error so it should not be the cause as despite the fact that we did not specify it, we are using the right one. I also have this error on my side. Could you share the entire configuration of the behaviour and hide if require sensitive information ?

Karl Samson
Contributor
June 6, 2024

UCP - Edit Behaviour_1.jpgUCP - Edit Behaviour_2.jpg

Karl Samson
Contributor
June 6, 2024

UCP - Edit Behaviour_3.jpg

Karl Samson
Contributor
June 6, 2024

UCP - Edit Behaviour_1.jpgUCP - Edit Behaviour_2.jpgUCP - Edit Behaviour_3.jpg

Florian Bonniec
Community Champion
June 6, 2024

You should not add the script in link field, you should add it on the Initialiser section, Create Script and paste it there.

By doing so the script will be call when the screen open. If you let it in linked issue field, it only trigger when you update the field.

 

Regards

Karl Samson
Contributor
June 6, 2024

Solved! Thank you so much!! :)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.12.7
TAGS
AUG Leaders

Atlassian Community Events