Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

3 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

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

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