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

Lacey McDonnell May 9, 2017

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

0 votes
Lacey McDonnell June 7, 2017

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"

 

0 votes
Thanos Batagiannis _Adaptavist_
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.
May 10, 2017

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

 

Lacey McDonnell May 10, 2017

Removed due to user error of content.

Lacey McDonnell May 10, 2017

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

Lacey McDonnell May 10, 2017

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.

Lacey McDonnell June 7, 2017

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

Suggest an answer

Log in or Sign up to answer