Validator script to check status of all linked issues before allowing transition

Hi,

After searching around this site and Google and refering to the Script Runner confluence page I just can't get my validator script to work and need some help.

I am trying to implement a simple validator script that will check the following for the issue being transitioned:

1) there must be at least 1 linked issue with a name of Test

2) assuming statement 1 above is true then all linked issues with a name of Test must be in a specific status

The code I have so far is:

import com.atlassian.jira.ComponentManager
import org.apache.log4j.Category
import com.atlassian.jira.issue.link.IssueLink;
import com.opensymphony.workflow.InvalidInputException

log = Category.getInstance("com.onresolve.jira.groovy.LinkedIssues")

testlinkType = ["Test"]

linkMgr = ComponentManager.getInstance().getIssueLinkManager()

// We are looking for the Inward Link of "Is Tested By"
for (IssueLink link in linkMgr.getInwardLinks(issue.id)) {
    if ((testlinkType.contains(link.issueLinkType.name)))
    {
        if (!link.getSourceObject().getStatusObject() == "Template") {
            invalidInputException = new InvalidInputException("Linked issue " + link.getId() + " is not in a status of Template")
        }
    }
}

Any help would be greatly appreciated.

Thanks.

3 answers

1 accepted

I figured it out - I was not using the getName() method i.e. link.getSourceObject().getStatusObject().getName())

Would you mind posting the complete script? I'm looking to check status of linked issues as well.

I would like the complete script too since I'm trying to do the exact same thing. Could you post it?

Hi All,

Sorry for such a late response!

Here is the script I created in the end.

Steve

import com.atlassian.jira.ComponentManager
import org.apache.log4j.Category
import com.atlassian.jira.issue.link.IssueLink;
import com.opensymphony.workflow.InvalidInputException

log = Category.getInstance("com.onresolve.jira.groovy.LinkedIssues")
log.debug("issue key: " + issue.key)
passesCondition = true

testlinkType = ["Test"]
testlinkStatus = ["Template"]

linkMgr = ComponentManager.getInstance().getIssueLinkManager()

// Check for at least 1 inward Test issue
if (!linkMgr.getInwardLinks(issue.getId())*.issueLinkType.name.contains('Test')) {
    log.debug("no Test issue links exist")
    invalidInputException = new InvalidInputException("There must be at least 1 'is tested by' link")
    throw invalidInputException
//    return false
}

// We are looking for the Inward Link of "Is Tested By"
for (IssueLink link in linkMgr.getInwardLinks(issue.id)) {

    log.debug("linked issue type: " + link.issueLinkType.name)
    log.debug("linked issue key: " + link.getSourceObject().getKey())
    log.debug("linked issue status (name): " + link.getSourceObject().getStatusObject().getName())
    log.debug("linked issue status (id): " + link.getSourceObject().getStatusObject().getId())

    if ((testlinkType.contains(link.issueLinkType.name)))
    {
        log.debug("we have a Test issue")
// The issue needs to be in a Template status
        if (!link.getSourceObject().getStatusObject().getName().contains("Template")) {
            log.debug("issue " + link.getSourceObject().getKey() + " is not in Template state")
            invalidInputException = new InvalidInputException("Linked issue " + link.getSourceObject().getKey() + " is not in a status of Template")
            throw invalidInputException
        }
    }
}

Updated for JIRA 7-

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkManager
import org.apache.log4j.Category
import com.atlassian.jira.issue.link.IssueLink;
import com.opensymphony.workflow.InvalidInputException

def Category log = Category.getInstance("com.onresolve.jira.groovy.LinkedIssues")
log.debug("issue key: " + issue.key)
Boolean passesCondition = true

String testlinkType = ["Test Case"]
String testlinkStatus = ["Active"]

IssueLinkManager linkMgr = ComponentAccessor.getIssueLinkManager()

// Check for at least 1 inward Test issue
if (!linkMgr.getInwardLinks(issue.getId())*.issueLinkType.name.contains('Test Case')) {
    log.debug("no Test Case issue links exist")
    invalidInputException = new InvalidInputException("There must be at least 1 'executes' link")
    throw invalidInputException
//    return false
}

// We are looking for the Inward Link of "Is Tested By"
for (IssueLink link in linkMgr.getInwardLinks(issue.id)) {

    log.debug("linked issue type: " + link.issueLinkType.name)
    log.debug("linked issue key: " + link.getSourceObject().getKey())
    log.debug("linked issue status (name): " + link.getSourceObject().getStatus().getName())
    log.debug("linked issue status (id): " + link.getSourceObject().getStatus().getId())

    if ((testlinkType.contains(link.issueLinkType.name)))
    {
        log.debug("we have a Test Case issue")
    // The issue needs to be in an Active status
     if (!link.getSourceObject().getStatus().getName().contains("Active")) {
            log.debug("issue " + link.getSourceObject().getKey() + " is not in Active state")
            invalidInputException = new InvalidInputException("Linked issue " + link.getSourceObject().getKey() + " is not in a status of Active")
            throw invalidInputException
        }
    }
}

Hi, wonder if you can help? I need a script which validates the issue type before transition to next status.

So for example on transtion to "Review & Planning" the issue type must be set to "Bug", Change Control", "GAP", "Enhancement" or "Configuration".

 

Thanks!

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,863 views 12 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot