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

Steve Richards
Contributor
April 17, 2013

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

4 votes
Answer accepted
Steve Richards
Contributor
April 17, 2013

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

Trevor Hunt
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.
September 18, 2013

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

Ramiro Pointis
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.
December 22, 2013

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

Steve Richards
Contributor
January 26, 2014

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
        }
    }
}

0 votes
Richard Oliver
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 26, 2017

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!

0 votes
Chris Duffy
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 14, 2017

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
        }
    }
}

Suggest an answer

Log in or Sign up to answer