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

Mike Henry October 24, 2016

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 votes
Answer accepted
adammarkham
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.
October 26, 2016

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 

Mike Henry October 26, 2016

This worked:

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

 

Thanks!

0 votes
Mike Henry October 24, 2016

Recived this error:

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

Alexey Belostotskiy October 25, 2016

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

What version of JIRA are you using?

Mike Henry October 25, 2016

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

Mike Henry October 25, 2016

same thing - does not pass

0 votes
Alexey Belostotskiy October 24, 2016

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
}
Mike Henry October 25, 2016

Same thing - does not pass

0 votes
Vasiliy Zverev
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.
October 24, 2016

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;
Mike Henry October 24, 2016

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

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

Vasiliy Zverev
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.
October 24, 2016

Could you provide exact name of this issue link type?

Mike Henry October 24, 2016

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

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

Mike Henry October 24, 2016

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

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

Still won't pass.

Vasiliy Zverev
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.
October 25, 2016

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;
Mike Henry October 25, 2016

Should this be doubled up like this?:

 

for(IssueLink link: issueLinkManager.getInwardLinks(issue.getId())){
    if(link.getIssueLinkType().getName().equals("Relates"))
        return true
}
Vasiliy Zverev
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.
October 25, 2016

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

Mike Henry October 25, 2016

Same thing - will not pass

Vasiliy Zverev
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.
October 25, 2016
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.

Mike Henry October 26, 2016

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

Vasiliy Zverev
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.
October 26, 2016

Do you use this code for simple script condition?

Mike Henry October 26, 2016

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!?!?

Vasiliy Zverev
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.
October 26, 2016

Could you check this validator on some issue with preview?

Mike Henry October 26, 2016

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

Vasiliy Zverev
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.
October 26, 2016

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;
Mike Henry October 26, 2016

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.

Vasiliy Zverev
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.
October 26, 2016

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

And as the result you will get true or false.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events