Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,501,447
Community Members
 
Community Events
180
Community Groups

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

6 votes
Answer 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

Like David Harkins likes this

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 ,

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 ,

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

 

@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 Feb 25, 2021

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

@Matthew Clark Your script worked perfectly. Thanks.

Answer up-voted.

Like Matthew Clark likes this

@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 Mar 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

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

 

In the creation window it works perfect, thanks :)

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

Suggest an answer

Log in or Sign up to answer