Need to require a link in workflow transition (groovy-script-runner)

I have not made any changes to Jira.

We have setup workflow for our Change Control project which should force the issue to link an issue - but I can't figure out how to do it. We need to force the user to select a RELATED link.

I have setup a validator to use Script Runner without success:

ScriptRunner workflow function - Simple scripted validator (condition apply).
issueLinkManager.getOutwardLinks(issue.getId())*.issueLinkType.name.contains('Relates')

I have also tried many other iterations of the word "RELATES:

Once I publish the workflow and test, I link an issue as RELATED to another issue but it never passesCapture.PNGes.

4 answers

1 accepted

0 vote

Use the simple scripted validator with the condition below to get the inward and outward links, it should be in that:

issueLinkManager.getIssueLinks(issue.getId())*.issueLinkType.name.contains('Relates')

or to only get the inward ones (which is likely your case with "Relates") use:

issueLinkManager.getInwardLinks(issue.getId())*.issueLinkType.name.contains('Relates')

Hope this helps,
Adam 

This worked:

issueLinkManager.getInwardLinks(issue.getId())*.issueLinkType.name.contains('Relates')

 

Thanks!

Try this code:

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

for(IssueLink link: ComponentAccessor.getIssueLinkManager().getIssueLinks(issue.getId())){
    if(link.getIssueLinkType().getName().equals("RELATED"))
        return true
}
return false;

It still doesn't pass after I added the new script you posted.

image2016-10-24 9:23:54.png

Could you provide exact name of this issue link type?

I haven't made any changes to this - so it's all stock:

image2016-10-24 9:42:4.png

I also tried changing this line to the exact case as the link name:

if(link.getIssueLinkType().getName().equals("Relates"))

Still won't pass.

Sorry, it is my mistake, try this code:

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

IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();

for(IssueLink link: issueLinkManager.getOutwardLinks(issue.getId())){
    if(link.getIssueLinkType().getName().equals("Relates"))
        return true
}

for(IssueLink link: issueLinkManager.getInwardLinks(issue.getId())){
    if(link.getIssueLinkType().getName().equals("Relates"))
        return true
}
return false;

Should this be doubled up like this?:

 

for(IssueLink link: issueLinkManager.getInwardLinks(issue.getId())){
    if(link.getIssueLinkType().getName().equals("Relates"))
        return true
}

Hm, you are right. In this case it is not requred to double

Same thing - will not pass

This code works for me:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
for(IssueLink link: ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())){
    if(link.getIssueLinkType().getName().equals("Relates"))
        return true
}
return false;

This will work for simple script condition.

For custom script condition it will be slightly more different.

I don't understand what I am doing wrong - I agree that your code should work - but after I add a related link I still get this:

image2016-10-26 8:1:47.png

Do you use this code for simple script condition?

Here are my steps:

image2016-10-26 8:3:9.png

image2016-10-26 8:3:35.png

image2016-10-26 8:3:58.png

image2016-10-26 8:4:46.png

What the heck!?!?

Could you check this validator on some issue with preview?

hmmm not sure how to do that - can you point me in the right direction?

Try this script into Script Console. Replace issueKey with key of tested issue:

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

Issue issue = ComponentAccessor.getIssueManager().getIssueObject("issueKey")

for(IssueLink link: ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())){
    if(link.getIssueLinkType().getName().equals("Relates"))
        return true
}
return false;

I changed issueKey to an issue that I know does not have a related link, run the preview and this is what I get:

image2016-10-26 9:19:5.png

Not much of any feedback from the script.

You need to run this script here: admin panel/plugins/scriptrunner/script console.

And as the result you will get true or false.

Something like this should do the job:

import com.atlassian.jira.bc.issue.link.IssueLinkService
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.IssueLinksSystemField

MutableIssue issue = issue
def value = (IssueLinksSystemField.IssueLinkingValue) issue.getModifiedFields().get("issuelinks").newValue

if (value) {
    IssueLinkService.AddIssueLinkValidationResult validationResult = value.validationResult
    return validationResult.linkType.name == "Relates" && validationResult.linkKeys
} else {
    return false
}

Same thing - does not pass

Recived this error:

image2016-10-24 10:37:57.png

Did you try saving and testing it? SR static checker might be weird sometimes.

What version of JIRA are you using?

I did not - will do now...I figured there was an error so I didn't move forward...

Our JIRA is locally hosted v7.2

same thing - does not pass

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

354 views 0 6
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you