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

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

4 votes
Answer 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 Sign up to answer
Community showcase
Posted in Jira

Calling all Jira Cloud users! Give us feedback on our exploration of a new navigation.

Hi everyone! My name’s Matt and I’m a product manager at Atlassian. I work in the navigation & findability space for all our Jira Cloud products. We’ve been working on trying to improve the exp...

1,053 views 16 12
Join discussion

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