jiraatlassian-sourcetreeatlassian-plugin-sdkatlassian-crowdscriptrunner-for-jira

Not able to throw exceptions in scriptrunner


I am using the following postfunction to throw errors when the user enters incorrect input and I am not able to see any errors thrown when I try out my code on the UI of Jira. Here is my code, why are the exceptions not thrown even though my input values don't satisfy the requirements? I am supposed to click on MounaTransition and then I should have all the checks and nothing is done for the moment. How can I make this work?

enter image description here

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Logger
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.opensymphony.workflow.InvalidInputException;
import com.softwareag.jira.insight.helper.Helper;
import java.sql.ResultSet;
import java.util.Map;
import com.atlassian.jira.project.version.Version;
import com.atlassian.jira.workflow.function.issue.AbstractJiraFunctionProvider;
import com.opensymphony.module.propertyset.PropertySet;
import com.opensymphony.workflow.WorkflowException;
import groovy.sql.GroovyRowResult
import groovy.sql.*
import groovy.sql.Sql

def log = Logger.getLogger("atlassian-jira.log")



log.warn("HELLO")       


customDiagnoserId = produceCustomerDiagnoserID();


issue.setSummary(customDiagnoserId)
log.warn("MOUNA 2: "+customDiagnoserId)


def produceCustomerDiagnoserID(){

    
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def issueManager = ComponentAccessor.getIssueManager()



    def cf = customFieldManager.getCustomFieldObject("Summary")
    String summaryValue = issue.getSummary()


    CustomField productCodeField = customFieldManager.getCustomFieldObject("customfield_10240"); 
    String productCodeFieldValue =  issue.getCustomFieldValue(productCodeField);

    CustomField SubTypeField1 = customFieldManager.getCustomFieldObject("customfield_11580"); 
    String SubTypeFieldValue =  issue.getCustomFieldValue(SubTypeField1);

    String customDiagnoserId = "";

            customDiagnoserId += productCodeFieldValue + "_";
            log.warn("CAMELIA")     
            customDiagnoserId += getProductVersion() + "_";
            log.warn("YAS1")        
            customDiagnoserId += SubTypeFieldValue + "_";
            log.warn("YAS2")        
            customDiagnoserId += issue.getParentObject().getKey() + "_V";
            log.warn("YAS3")        
            customDiagnoserId += getCustomDiagnoserVersion(issue);
            log.warn("YAS4")        


    log.warn("MOUNA 1: "+customDiagnoserId)

    log.warn("Value: " +customDiagnoserId)  
    return customDiagnoserId
}
def getProductVersion(){
    def CUSTOM_DIAGNOSER_PRODUCT_VERSION_HAS_MULTIPLE_VALUES = "Fix Version/s has multiple values";
    log.warn("CAMELIA2")        

    if (0 < issue.getFixVersions().size() && issue.getFixVersions().size() < 2)
    {
                log.warn("CAMELIA3")        

                return issue.getFixVersions().toArray()[0].toString();
    }
    else
    {
                log.warn("CAMELIA4")        

                log.warn("---1 " + issue.getFixVersions().toArray()[0].toString())      
                throw new InvalidInputException("resolution", CUSTOM_DIAGNOSER_PRODUCT_VERSION_HAS_MULTIPLE_VALUES)
                log.warn("CAMELIA5")        


    }
}


def getCustomDiagnoserVersion(MutableIssue issue) throws Exception
    {



        CustomField customDiagnoserVersionField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11589"); 
        String customDiagnoserVersionFieldValue =  issue.getCustomFieldValue(customDiagnoserVersionField);

        def CUSTOM_DIAGNOSER_CUSTOM_DIAGNOSER_VERSION_FIELD_OUT_OF_BOUNDS = "Value of field ':1:' not in range 1..9999";
        def CUSTOM_DIAGNOSER_CUSTOM_DIAGNOSER_VERSION_FIELD_NOT_A_NUMBER = "Value of field ':1:' is not a number";

        try
        {

            def version = Integer.parseInt(customDiagnoserVersionFieldValue);
            if (0 < version && version < 10000)
            {
                return customDiagnoserVersionFieldValue;
            }
            else
            {
                log.warn("---2 ")       

                throw new InvalidInputException("resolution", CUSTOM_DIAGNOSER_CUSTOM_DIAGNOSER_VERSION_FIELD_OUT_OF_BOUNDS.replaceAll(":1:", customDiagnoserVersionField.getName()))


            }
        }
        catch (NumberFormatException exception)
        {
            log.warn("---3 ")       
            throw new InvalidInputException("resolution",CUSTOM_DIAGNOSER_CUSTOM_DIAGNOSER_VERSION_FIELD_NOT_A_NUMBER.replaceAll(":1:", customDiagnoserVersionField.getName()))


        }
    }


//void isUniqueCustomDiagnoserId(MutableIssue issue) throws Exception
//  {
//      DatabaseUtils database = new DatabaseUtils();
//      if(!database.connect())
//      {
//          String message = "Could not connect to Database!";
//          log.error(message);
//          throw new WorkflowException(message);
//      }
//      ResultSet rs = database.executeQuery(CUSTOM_DIAGNOSER_ID_IS_UNIQUE_SQL.replaceAll(":1:", getCustomDiagnoserId(issue)));
//      if(rs.next())
//      {
//          String issueKey = rs.getString("PKEY");
//          if (database != null) database.close();
//          throw new InvalidInputException(CUSTOM_DIAGNOSER_ID_IS_NOT_UNIQUE.replaceAll(":1:", getCustomDiagnoserId(issue)).replaceAll(":2:", issueKey));
//      }
//      if (database != null) database.close();
//  }

Solution

  • In ScriptRunner, there are different types like Post Function, Validator, Condition, etc.

    I am supposed to click on MounaTransition and then I should have all the checks

    So, if you want to do some checks about the issue (and I assume that if the checks failed then you want to block transition) you should use Validator.

    In Jira, edit the workflow of desired project and select "Add new validator" -> "Custom script validator" and then your "throws" will work like a charm.

    For the further info, you can check the ScriptRunner documentation.