Workflow Validation Script

I have a Workflow validation script as below:

def found = ((issueLinkManager.getOutwardLinks(issue.id).any{it.destinationObject.issueTypeObject.name != "Epic"}) && (issueLinkManager.getOutwardLinks(issue.id).any {it.destinationObject.statusObject.name != 'Closed'}))
return !found

What it does is to prevent issue transition to a specific Status when the linked issues other than Linked Epic has Status not Closed.

I want to include a Status called Completed in it i.e The issue can transition if the Linked issue is either in Closed or Completed Status.The other part of the validator remains the same.

How can I achieve that?

3 answers

0 votes
Vasiliy Zverev Community Champion Jan 17, 2016

Try this code:

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

IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();
def found = ((issueLinkManager.getOutwardLinks(issue.id).any{it.destinationObject.issueTypeObject.name != "Epic"})
          && (  (issueLinkManager.getOutwardLinks(issue.id).any {it.destinationObject.statusObject.name == 'Closed'})
          ||     (issueLinkManager.getOutwardLinks(issue.id).any {it.destinationObject.statusObject.name == 'Completed'}      )
                )
            )

return !found

This does not work.It works for first level i.e. Closed but for the second it is not validating as expected.In my linked issue there is a ticket of Status Completed which ideally should let the main ticket to go to next status but its not.

Vasiliy Zverev Community Champion Jan 18, 2016

Lets try to understand each other.

We have some issue and several linked issues. If one of NOT Epic issues is into Closed or Completed status this transition should be done. 

Is it correct?

Consider my main issue is Release and the Release has many Defects Stories and Release Linked to it.The requirement is until all the linked issue(apart from Epic,Epic can be at any Status) are in either Completed or Closed the main Release ticket Cannot be marked to some specific Status say Closed.

Hope that Clarifies.

Can you please provide any solution?

Vasiliy Zverev Community Champion Jan 18, 2016

Try this one:

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

IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();
String[] allowedStatuses = {"closed"; "Completed"};

for(Issue curIssue: issueLinkManager.getOutwardLinks(issue.id)){
    if(curIssue.getIssueTypeObject().getName() != "Epic"){
        if( !allowedStatuses.contains(curIssue.getStatusObject().getName()))
            return false;
    }
}

return true;

As i mentioned that i tried with the below mentioned code and it worked but there seems to be an issue when there are no linked issue to the main ticket yet it is throwing exception.How can i get rid of that keeping the functionality of my code same basically it should by pass the validation when no linked issues are there.Code implemented is as below:

def found = false;
if ((issueLinkManager.getOutwardLinks(issue.id).any{it.destinationObject.issueTypeObject.name != "Epic"})
          &&   (issueLinkManager.getOutwardLinks(issue.id).any {it.destinationObject.statusObject.name == 'Closed'}) )
          found = true;
 else if((issueLinkManager.getOutwardLinks(issue.id).any{it.destinationObject.issueTypeObject.name != "Epic"})
          &&   (issueLinkManager.getOutwardLinks(issue.id).any {it.destinationObject.statusObject.name == 'COMPLETED'}))
          found = true;
return found

I have something similar; my script looks for outward links of type "dependency", and prevents the transition if there isn't one that is unresolved.  Your case sounds similar; I had to make certain I was looking at the status of the specific link(s) of interest, while you are checking against type and status separately, which I think could be an issue.  For example, if you have two outward links where one is an Open Epic, and the other is a Closed Whatever, your "found" condition will still be met.  I think you want:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager;
 
IssueLinkManager issueLinkManager = ComponentManager.getInstance().getIssueLinkManager();
 
Boolean found = false;
for (IssueLink link in issueLinkManager.getOutwardLinks(issue.id)) {
        if (link.getIssueLinkType().getName()!="Epic") {
                if(link.getDestinationObject().getStatusObject().getName() != "Closed" &&  link.getDestinationObject().getStatusObject().getName() != "Completed") {
                        found = true;
                }
        }
}
 
return !found;

The above code will require that all non-Epic outward links (if any) be completed/closed.  If you want just one, then use:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager;
 
IssueLinkManager issueLinkManager = ComponentManager.getInstance().getIssueLinkManager();
 
Boolean found = false;
for (IssueLink link in issueLinkManager.getOutwardLinks(issue.id)) {
        if (link.getIssueLinkType().getName()!="Epic") {
                if(link.getDestinationObject().getStatusObject().getName() == "Closed" ||  link.getDestinationObject().getStatusObject().getName() == "Completed") {
                        found = true;
                }
        }
}
 
return found;

This version will also require that there be at least one non-Epic link.

I tried with the below code and it worked

 

 

def found = false;
if ((issueLinkManager.getOutwardLinks(issue.id).any{it.destinationObject.issueTypeObject.name != "Epic"})
          &&   (issueLinkManager.getOutwardLinks(issue.id).any {it.destinationObject.statusObject.name == 'Closed'}) )
          found = true;
 else if((issueLinkManager.getOutwardLinks(issue.id).any{it.destinationObject.issueTypeObject.name != "Epic"})
          &&   (issueLinkManager.getOutwardLinks(issue.id).any {it.destinationObject.statusObject.name == 'COMPLETED'}))
          found = true;
return found

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Monday in Jira Service Desk

Frequently Asked Questions for Jira Service Desk (AKA JSD FAQ)

Just the FAQs! Welcome to the Community, and this Focused-FAQ! Here, we've pulled together some of the most frequently asked questions associated with Jira Service Desk (JSD). We hope ...

113 views 0 11
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