Validator for If Statement Multiple Runs, LINK and FIX VERSION, ScriptRunner Adaptavist

I have a validator that's supposed to do a check for

NOT Reopen Link Type

then

NOT [Base Link Type and Fix Version is Not Empty]

and if those conditions don't apply, then allow the transition without mandatory fields filled out.

It's running for only the first condition, not the second. It's not failing - just not running.

 

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager;
import com.atlassian.jira.issue.IssueFieldConstants;
import com.atlassian.jira.issue.fields.IssueLinksSystemField;
import com.opensymphony.workflow.InvalidInputException;
import webwork.action.ActionContext;

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def fixVersion= issue.getFixVersions()
def fields = []

def fieldManager = ComponentAccessor.getFieldManager()
def linksSystemField = fieldManager.getField("issuelinks") as IssueLinksSystemField

def request = ActionContext.getRequest()
if (request) {
    def params = request.getParameterMap()
    def issueLinkingValue = linksSystemField.getRelevantParams(params) as IssueLinksSystemField.IssueLinkingValue

// If issue is not REOPENED LINK TYPE, make all sorts of validations
if ( !issueLinkManager.getOutwardLinks(issue.getId())*.issueLinkType.name.contains('Reopen') && !issueLinkManager.getInwardLinks(issue.getId())*.issueLinkType.name.contains('Reopen'))  
    {
    fields.addAll(['BA Actual Hours', 'BA Analysis By', 'Bug Source', 'Complexity', 'Data Fix', 'Dev Estimate Hrs', 'QA Estimate Hrs', 'RCA Categories - BA', 'Release Notes', 'Stat Code Impact'])

    fields.each 
        {
		CustomField cf = customFieldManager.getCustomFieldObjectByName(it)
		if (cf && !issue.getCustomFieldValue(cf)) 
            {
			if (invalidInputException)
				invalidInputException.addError(cf.id, "Value missing!")
			else
				invalidInputException = new InvalidInputException(cf.id, "Value missing!")
			}
 		}       
	if (invalidInputException)
		invalidInputException.addError("The fields marked below must be filled before analysis is completed.")
};
else 
    // If issue is not BASE LINK TYPE, and FIXVERSION is not NULL, make all sorts of validations
if ( !issueLinkManager.getOutwardLinks(issue.getId())*.issueLinkType.name.contains('Ticket') & ! fixVersion == null && !issueLinkManager.getInwardLinks(issue.getId())*.issueLinkType.name.contains('Ticket') & ! fixVersion == null)  
    {
    fields.addAll(['BA Actual Hours', 'BA Analysis By', 'Bug Source', 'Complexity', 'Data Fix', 'Dev Estimate Hrs', 'QA Estimate Hrs', 'RCA Categories - BA', 'Release Notes', 'Stat Code Impact'])

    fields.each 
        {
		CustomField cf = customFieldManager.getCustomFieldObjectByName(it)
		if (cf && !issue.getCustomFieldValue(cf)) 
            {
			if (invalidInputException)
				invalidInputException.addError(cf.id, "Value missing!")
			else
				invalidInputException = new InvalidInputException(cf.id, "Value missing!")
			}
 		}       
	if (invalidInputException)
		invalidInputException.addError("The fields marked below must be filled before analysis is completed.")
}
else return true
}

2 answers

Hi Lacey, 

I would suggest to try and break your conditions into small in order to be easier for you (and me) to debug it and maybe also stick some log.debug messages in order to see where it goes.

Two observations in your second condition you have twice

! fixVersion == null

which can be replaced by one

fixVersion  

And also instead of checking bot inward and outward links you can check it there is a link type of a specific name. something like 

       issueLinkManager.getLinkCollectionOverrideSecurity(issue)?.linkTypes?.find {it.name.contains("Ticket")}

regards, Thanos

 

Removed due to user error of content.

Removing this as the thread is getting confusing (they really should allow for deletions...)

I now have a much bigger problem. It looks like we have been having background failures of a previously operational link validator. I opened a support ticket for it.

I gave up and opened another support case. I can't get this to work and my deadline is the end of this week. 

 

https://productsupport.adaptavist.com/servicedesk/customer/portal/2/SRJSUP-2075

Well, back to this again. It seems to not be working for link type, I'm not sure if it's working on the second condition of project key since it's not skipping fields for Base issue type or for Reopen.

 

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager;
import com.atlassian.jira.issue.IssueFieldConstants;
import com.atlassian.jira.issue.fields.IssueLinksSystemField;
import com.opensymphony.workflow.InvalidInputException;
import webwork.action.ActionContext;

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def fixVersion = issue.getFixVersions()
def fields = []

def fieldManager = ComponentAccessor.getFieldManager()
def linksSystemField = fieldManager.getField("issuelinks") as IssueLinksSystemField

def request = ActionContext.getRequest()
if (request) {
    def params = request.getParameterMap()
    def issueLinkingValue = linksSystemField.getRelevantParams(params) as IssueLinksSystemField.IssueLinkingValue

// If issue is not REOPENED LINK TYPE, make all sorts of validations
if ( (!  issueLinkManager.getLinkCollectionOverrideSecurity(issue)?.linkTypes?.find {it.name.contains("Reopen")}))  
    {
    fields.addAll(['BA Actual Hours', 'BA Analysis By', 'Bug Source', 'Complexity', 'Data Fix', 'Dev Estimate Hrs', 'QA Estimate Hrs', 'RCA Categories - BA', 'Release Notes', 'Stat Code Impact'])

    fields.each 
        {
  CustomField cf = customFieldManager.getCustomFieldObjectByName(it)
  if (cf && !issue.getCustomFieldValue(cf)) 
            {
   if (invalidInputException)
    invalidInputException.addError(cf.id, "Value missing!")
   else
    invalidInputException = new InvalidInputException(cf.id, "Value missing!")
   }
   }       
 if (invalidInputException)
  invalidInputException.addError("The fields marked below must be filled before analysis is completed.")
};
else 
    // If issue is not BASE LINK TYPE, and PROJECT is not BASE, make all sorts of validations
if ( (!  issueLinkManager.getLinkCollectionOverrideSecurity(issue)?.linkTypes?.find {it.name.contains("Base")} & ! (issue.projectObject.key in ['BOPT', 'CAT', 'CPPT', 'WCT', 'POLT', 'PAUTOT', 'HOMET'])))  
    {
    fields.addAll(['BA Actual Hours', 'BA Analysis By', 'Bug Source', 'Complexity', 'Data Fix', 'Dev Estimate Hrs', 'QA Estimate Hrs', 'RCA Categories - BA', 'Release Notes', 'Stat Code Impact'])

    fields.each 
        {
  CustomField cf = customFieldManager.getCustomFieldObjectByName(it)
  if (cf && !issue.getCustomFieldValue(cf)) 
            {
   if (invalidInputException)
    invalidInputException.addError(cf.id, "Value missing!")
   else
    invalidInputException = new InvalidInputException(cf.id, "Value missing!")
   }
   }       
 if (invalidInputException)
  invalidInputException.addError("The fields marked below must be filled before analysis is completed.")
}
else return true
}
import org.apache.log4j.Logger
import org.apache.log4j.Level
  
def log = Logger.getLogger("com.acme.ValidateLinks")
log.setLevel(Level.DEBUG)
  
log.debug "foo bar"

 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira Service Desk

Wy are we still using email for Service Desk workflows?

...attest to the experience of an urgent approval that gets lost in the boss’s inbox and requires that special “Please Approve” email or text message. In an age where we have distributed teams...

91 views 0 2
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