Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Section


Column

Starting with version 2.0.0, the JMCF add-on comes with a Groovy script editor and tester in the configuration screens of the calculated custom field. This document details the Groovy script tester, its availability and features.

The Groovy script tester tool lets you test your written script against any issue. The main advantage of this tool is that you can quickly test and debug your script and make changes without having to actually save the custom field and recalculate the calculated field value.


Column
width50%


Panel
borderColorsilver
bgColor#f5f5f5
borderWidth1
borderStylesolid

On this page:

Table of Contents



...

The Groovy script tester in JMCF is available on the Custom field configuration screen when you write a Groovy script to return a value expected by the calculated custom field in the Groovy Formula for the calculated custom field in the Format Expression fieldfield.

Using the Groovy Script tester

...

After selecting issue against which the script should be tested (as explained above), click on Test. The following information will be displayed. This information can be used for debugging.

  1. Message: Success/error message based on the test result.

  2. Result type: Data type of the result.
  3. Result value: Value of the result.
  4. Stack: If an exception was thrown during the execution of the script, a stack trace will be displayed.
  5. Logs: Information logged using the log variable.

Example

Panel

Test a script that fetches returns the username of the user the issue is assigned to:

  • Add the Set field value post-function to a transition.
  • SelectGroovy expression as the Value type

    sum of two number fields:

    1. Locate the custom field on the Custom Fields administration page.
    2. Click on the cog wheel and click on Configure.
    3. In the Groovy editor, write the following lines of code:

      Code Block
      languagegroovy
      linenumberstrue
      log.warn(issue.get("assignee").getName())
      return (customfield_10114") + issue.get("assignee").getName())customfield_10150")


    4. Click on the Test Groovy Script button on the toolbar.
    5. Type an Issue key. The issue you pick must have a valid Assignee.
    6. Click on Test.
    7. The following result is displayed:Image Removed
    8. For the same script, select an issue which is unassigned and test the result (Repeat steps 4,5 and 6). Since the issue is unassigned, a null pointer exception occurs.
    Image Removed
    1. Image Added

    Debugging your script

    If you encounter an error during testing, you will need to debug your script. The Message, Stack and Log information displayed in the script tester result panel aids in debugging the script.

    Using the Message and Stack trace

    In the above example, after testing the script against an issue that is unassigned:encountering the error,

    • Identify the problem, from the Message that displayed a null pointer exception error and the Stack that displayed the line number on which the error occurred.
    • return (

      Correct the problem, using the safe navigation operator ? that avoids the null pointer exception.

      Code Block

       checking for null values and then returning the result.

    1. In the Groovy editor, write the following lines of code:

      Code Block
      languagegroovy
      linenumberstrue
      if (issue.get("customfield_10028") == null || issue.get("

    ...

    1. customfield_10006") == null)
          return null;
      else  
      return issue.get("customfield_10028") + issue.get("customfield_10006");


    2. Click on Test again.
    3. The following result is displayed:
      Image Added

    Using the log variable

    Using the log variable, you can debug your script, a step further, by adding information into the log. For example, while testing a condition in your script, if the result evaluates to false when you expect it to be true, you might want to know what values are being compared in your condition. In such cases, you can use the log variable to display the value.

    Note

    The log calls should always be used before the return statement because they would never get executed after it.

    ...

    Use log variable, in the above script to know the values of the custom fields.

    Code Block
    return (issue.get("assignee")? == "Scharlie")

    ...

    Identify the problem, using the log variable (as shown below) and check what values are being compared. 

    ...

    log.warn("Value of field 10028 is: "+issue.get("customfield_10028"));
    log.warn("

    ...

    Value 

    ...

    of 

    ...

    field 

    ...

    10006 

    ...

    is: "

    ...

    +

    ...

    issue

    ...

    .get("

    ...

    customfield_10006"));
    

    ...

    if (issue

    ...

    .get("

    ...

    customfield_10028") == null 

    ...

    Retest the scriptas explained above and verify the result in the script test panel. 

    Image Removed

    The script is comparing a user object to a String value.

    Correct the problem, by fetching the username and then comparing the values

    Code Block
    log.warn("Fetched user is" + " " + issue?.get("assignee"))
    return (issue?.get("assignee").getName() == "Scharlie")

    ...

    || issue.get("customfield_10006") == null)
        return null;
    else  
    return issue.get("customfield_10028") + issue.get("customfield_10006");

    Re-test the script, the following result is displayed.

    Image Added

    Remove your logging code, so that log entries don't get written to Jira's logfile each time the

    ...

    field is

    ...

    calculated.