ScriptRunner Behaviour to limit or restrict link types

Chase Brown December 12, 2019

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

7 votes
Answer accepted
Matthew Clark
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 13, 2019

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

Chase Brown December 13, 2019

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

Matthew Clark
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 13, 2019

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

Like David Harkins likes this
Chase Brown December 13, 2019

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

Matthew Clark
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 13, 2019

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
Chase Brown December 13, 2019

That worked perfectly! Thank you for your support!

Lokesh July 2, 2020

Hi @Matthew Clark ,

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]
Matthew Clark
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 5, 2020

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

Shawna Morris August 28, 2020

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?

Dan27 September 24, 2020

Hi @Matthew Clark ,

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)

Matthew Clark
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 24, 2020

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

Moses Thomas
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2021

 

@Matthew Clark   Hello,  in your  code  the  mapping direction;  I have my code  without even reducing the LinkTpes  Issue with set Link type on the create issue transition Behaviours    even when i extended my code to  define  the direction,

I want to have the mapping below  and it does not  work i get the below error after hitting  the create transition is it some kind  of  bug with the behaviors  ?  only one way is working strange :/

def allowedLinks =  inwardAllowedLinks << outwardAllowedLinks   

ERROR

We can't create this issue for you right now, it could be due to unsupported content you've entered into one or more of the issue fields. If this situation persists, contact your administrator as they'll be able to access more specific information in the log file.

Kind regards,

Moses

Moses Thomas
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2021

@Matthew Clark  I So tried it in DEV environment -  Script runner Version  6.16.0  it is  working perfect !   interesting !

Rahul Savaikar March 8, 2021

@Matthew Clark Your script worked perfectly. Thanks.

Answer up-voted.

Like Matthew Clark likes this
Matthew Clark
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2021

@Moses Thomas,

That error does not look like it is related to the behaviours script. If the behaviour script was adding incorrect values for the `issuelinks-linktype` or `issuelinks-issues` fields, you should see an error on the field itself in the create form window when you click create.

The error may be something to do with your workflow functions for the project you are creating issues in.

If you still have issues on your non-dev server, you might want to try and create a test project with a simple workflow to see if the same problem still happens.

Regards
Matthew

Moses Thomas
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 25, 2021

Hello Matthew,

I have fixed my problem it was not issue with workflow,  it worked in another version of script runner in DEV,.

 

Thanks,

Moses

Jnr November 5, 2021

@Matthew Clark any news/ideas about limiting types in the "Links" window?

 

In the creation window it works perfect, thanks :)

Matthew Clark
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 18, 2021

Hi Jnr

I am happy the solution works for you on the create form.

Unfortunately, behaviours still do not trigger on the Link modal screen.
I think it is a valid use case that others would also want, so I have created a feature request for it here.

 

Also I wanted to mention that the script is added to our library here now, so it is in a more official place with all our other useful scripts.

 

Regards

Matthew

Like Jnr likes this
Slava Gefen May 8, 2023

Hey @Matthew Clark thanks for the script!

Unfortunately, behaviours still do not trigger on the Link modal screen.
I think it is a valid use case that others would also want, so I have created a feature request for it here.

Maybe it's somehow possible to hide this button via ScriptRunner?

With kind regards
Slava

Matthew Clark
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 11, 2023


Hi Slava

You can hide the link button in the More menu and the entire linking module that shows on the issue view page with our "Hide UI Element Built-In Script" web-fragment. However, you cannot easily stop a user from editing the links from the '.' or 'gg' menu or the 'l' keyboard shortcut.

Here is the doc on our fragment feature here

And the options to enter into the "Hide what" field are:

`com.atlassian.jira.plugin.system.issueoperations:link-issue`
`com.atlassian.jira.jira-view-issue-plugin:linkingmodule`

So you can half hide it, but unfortunately not a complete solution.

Kind regards
Matthew

Slava Gefen May 14, 2023

Thanks @Matthew Clark it works indeed! 🙏

Azfar Masut
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2023

if you faced any error in the newer scriptrunner version for the last line, change it to:

//FROM:
linkTypes.setFieldOptions(allowedLinks)


//TO:
linkTypes.setFieldOptions(allowedLinks as
Map<String,String>)
Like Ismael Jimoh likes this

Suggest an answer

Log in or Sign up to answer