It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner Behaviour to limit or restrict link types

I'm trying to limit the available link types within one of my projects to the specific ones they will be using. 

See Code below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink

def list = {"Blocks", "Relates"]

def allowedLinks = ComponentAccessor.getIssueLinkManager.issueLinkType(list.contains(it.name)){
collectEntries{
[(it.id):it.name]
}

getFieldById("issuelinks-linktype").setFormValue(list)

I'm not sure what I'm doing wrong... help?

1 answer

1 accepted

Hi Chase

I wrote an example Scriptrunner Behaviour script here that allows you to restrict the available link directions that show on the create/edit and transition screens for the "Linked Issues" field.

Use it as a Behaviour initialiser script.

I think the logic could be improved for getting the allowed links as I do not really like how I am combining the 2 maps at the moment but it works for now.

I will improve it if I get a chance at a later date.

 

Note: To see the names of your outward and inward links, hit gg on the keyboard and search for "issue linking"

 

Regards

Matthew

Matt, Thank you for your reply. Can you put the script somewhere I can access them?

It should be available on the link I put in my reply as it is set to public.

Does this link not work?

https://bitbucket.org/snippets/mclark_AV/Ebozr9/behaviour-control-the-linked-issues-list

I cannot access bitbucket where I'm at for come reason.

I will add it here. Hopefully, it keeps its formating:

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 = ["blocks", "relates to", "causes"]
def allowedInwardTypes = ["is blocked by", "relates to", "is caused by"]

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]
}

//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. I checked on the software screens, not checked service desk yet.

linkTypes.setFieldOptions(allowedLinks)
Like # people like this

That worked perfectly! Thank you for your support!

Hi @Matthew Clark _Adaptavist_ ,

Not working although included the exact same script into initialiser in a behaviour.

Mapped it into Project, while editing or viewing if tried linking, it shows as all link options. Log shows as below

Anything missed ?

2020-07-02 04:56:55,851-0500 http-nio-8080-exec-2 url:/rest/scriptrun...st/validators.json username:Lokesh_D1 DEBUG Lokesh_D1 296x376842x1 1548eal 112.133.248.8,0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/validators.json [a398ab30fa6fd476bf022fd750562689] Allowed Links = [blocks:blocks, causes:causes, relates to:relates to, is blocked by:is blocked by, is caused by:is caused by]
2020-07-02 04:56:54,496-0500 http-nio-8080-exec-11 url:/secure/QuickEditIssue.jspa username:Lokesh_D1 DEBUG Lokesh_D1 296x376837x1 1548eal 112.133.248.8,0:0:0:0:0:0:0:1 /secure/QuickEditIssue.jspa [c.o.scriptrunner.runner.ScriptBindingsManager] Issue Link details Bonfire Testing
2020-07-02 04:56:54,496-0500 http-nio-8080-exec-11 url:/secure/QuickEditIssue.jspa username:Lokesh_D1 DEBUG Lokesh_D1 296x376837x1 1548eal 112.133.248.8,0:0:0:0:0:0:0:1 /secure/QuickEditIssue.jspa [c.o.scriptrunner.runner.ScriptBindingsManager] Issue Link created com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent@76369486
2020-07-02 04:56:21,045-0500 Caesium-1-2 INFO ServiceRunner [c.a.j.p.scheduler.job.VirusScanningJobRunner] --- VirusScanningJob completed!
2020-07-02 04:56:21,045-0500 Caesium-1-2 INFO ServiceRunner [c.a.j.p.scheduler.job.VirusScanningJobRunner] Num of attachments to scan [0]
2020-07-02 04:56:21,045-0500 Caesium-1-2 INFO ServiceRunner [c.a.j.p.scheduler.job.VirusScanningJobRunner] --- Running VirusScanningJob...
2020-07-02 04:56:17,019-0500 http-nio-8080-exec-9 url:/rest/scriptrun...st/validators.json username:Lokesh_D1 DEBUG Lokesh_D1 296x376814x1 1548eal 112.133.248.8,0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/validators.json [a398ab30fa6fd476bf022fd750562689] Allowed Links = [blocks:blocks, causes:causes, relates to:relates to, is blocked by:is blocked by, is caused by:is caused by]
2020-07-02 04:56:12,440-0500 http-nio-8080-exec-13 url:/rest/scriptrun...st/validators.json username:Lokesh_D1 DEBUG Lokesh_D1 296x376809x1 1548eal 112.133.248.8,0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/validators.json [a398ab30fa6fd476bf022fd750562689] Allowed Links = [blocks:blocks, causes:causes, relates to:relates to, is blocked by:is blocked by, is caused by:is caused by]
2020-07-02 04:55:59,004-0500 http-nio-8080-exec-25 url:/rest/scriptrun...st/validators.json username:Lokesh_D1 DEBUG Lokesh_D1 295x376798x1 1548eal 112.133.248.8,0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/validators.json [a398ab30fa6fd476bf022fd750562689] Allowed Links = [blocks:blocks, causes:causes, relates to:relates to, is blocked by:is blocked by, is caused by:is caused by]
2020-07-02 04:55:57,659-0500 http-nio-8080-exec-37 url:/secure/QuickEditIssue.jspa username:Lokesh_D1 DEBUG Lokesh_D1 295x376792x1 1548eal 112.133.248.8,0:0:0:0:0:0:0:1 /secure/QuickEditIssue.jspa [c.o.scriptrunner.runner.ScriptBindingsManager] Issue Link details Blocks
2020-07-02 04:55:57,658-0500 http-nio-8080-exec-37 url:/secure/QuickEditIssue.jspa username:Lokesh_D1 DEBUG Lokesh_D1 295x376792x1 1548eal 112.133.248.8,0:0:0:0:0:0:0:1 /secure/QuickEditIssue.jspa [c.o.scriptrunner.runner.ScriptBindingsManager] Issue Link created com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent@2d0136a0
2020-07-02 04:55:47,810-0500 http-nio-8080-exec-31 url:/rest/scriptrun...st/validators.json username:Lokesh_D1 DEBUG Lokesh_D1 295x376787x1 1548eal 112.133.248.8,0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/validators.json [a398ab30fa6fd476bf022fd750562689] Allowed Links = [blocks:blocks, causes:causes, relates to:relates to, is blocked by:is blocked by, is caused by:is caused by]
2020-07-02 04:55:44,633-0500 http-nio-8080-exec-23 url:/rest/scriptrun...st/validators.json username:Lokesh_D1 DEBUG Lokesh_D1 295x376783x1 1548eal 112.133.248.8,0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/validators.json [a398ab30fa6fd476bf022fd750562689] Allowed Links = [blocks:blocks, causes:causes, relates to:relates to, is blocked by:is blocked by, is caused by:is caused by]

Hi Lokesh

Unfortunately, the only thing I can see from the logs is that it should be setting the allowed link types to the following:

blocks
causes
relates to
is blocked by
is caused by
is caused by

I ran a test on the latest Version of Scriptrunner against a Software Project and it still appears to work.

1-Are you using valid link types names that exist on your system?

You define your specific link type names here:

def allowedOutwardTypes = ["blocks", "relates to", "causes"]
def allowedInwardTypes = ["is blocked by", "relates to", "is caused by"]

 

2-Are you running this behaviour as an initialiser script?

 

3-If you open the Browser Developer tools and go to the Console tab, then open the edit form window do you see something like this in the console output:

Screenshot 2020-07-05 at 22.12.29.png

Regards
Matthew

Is there a way that we can restrict how many of one linkType can exist on an issue. Say for example, I only want to allow the existence of 1 Parent/Child linkType - if one already exists, throw an error message and do not allow the second Parent/Child linkType?

Hi @Matthew Clark _Adaptavist_ ,

I tried tp use your script in intialiser behaviour, without success..1.JPG all of the links types are still displayed in here.

any idea?

this is my script:

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 = ["blocks", "relates to", "causes"]
def allowedInwardTypes = ["is blocked by", "relates to", "is caused by"]

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]
}

//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. I checked on the software screens, not checked service desk yet.

linkTypes.setFieldOptions(allowedLinks)

Hi Daniel


Unfortunately, Behaviours do not run on that screen. The script will only work for adding links on the Issue create/edit and transition screens.

The only alternative to stop users inputting data as they are entering it is to write your own JavaScript and load it in with ScriptRunners web resources feature.

Regards
Matthew

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you