Versions Compared

Key

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

When running Groovy scripts, JMCF makes contextual information available to your script through built-in variables When running Groovy scripts, JMCF makes contextual information available to your script through built-in variables and functions. This document details them. Note that you can also define custom variables in your Groovy script.

On this page:

Table of Contents

Variables availability

...

Variable name

Type

Description

Variables and functions used in a Groovy script#issue

Issue

The issue variable points to the current issue being processed.

Variables and functions used in a Groovy script#value

String

The value of the calculated field

Variables and functions used in a Groovy script#textutils

TextUtils

textutils is a utility object of class TextUtils providing useful methods to manipulate text and HTML

Variables and functions used in a Groovy script#log

Logger

The log variable is a Logger instance that writes into atlassian-jira.log (useful for debugging)

Variables and functions used in a Groovy script#numberTool

NumberTool

NumberTool instance that can be used to format number values.

...

The value represents the value of the calculated field returned by the Groovy script in the custom field configuration. This is applicable only while customizing the display of a Calculated (Scripted) Number custom field type to format it using the Variables and functions used in a Groovy script#numberTool variable. The data type of the variable is a Double.  

...

The numberTool variable is a NumberTool instance that can be used to format the value of the calculated field returned by the Groovy script in the custom field configuration. This is applicable only while customizing the display of a Calculated (Scripted) Number custom field type to format it.  

For example: If the value of a calculated number custom field is 22 you can format it using the numberTool variable available in the Groovy editor of the Format Expression field.

...

The value is formatted to $40.

To add an IMG tag to display an icon to the left of the number:

Code Block
"<img src='/images/icons/priority_trivial.gif'> "+numberTool.format(value);

...

The textutils variable is a utility object of class TextUtils providing useful methods to manipulate text and HTML

...

textutils.noNull(issue.get("description")) + issue.key returns a text avoiding null in case there is no Description of the issue.

log

The log variable is a Logger instance that is used to output information like errors and warnings into the atlassian-jira.log file located in your Jira home directory. You can also use the log variable to output data to the script tester result panel during script development and debugging. There are five logging levels available in log4j, and they are all output to the script tester result panel. However, by default, only WARN and ERROR level logs are output to the atlassian-jira.log file, so you should only use log.warn(...) and log.error(...) for run-time logging (as opposed to development-time logging). To see other levels in atlassian-jira.log, you can raise the logging level for the com.innovalog package.  

...

So when the issue is unassigned, the warning message is displayed in the atlassian-jira.log file.

Custom variables

In addition to the above variables, you can also define your own variables in the Groovy script.

For example, Condition to check whether the Fix Version/s has a particular version.

...

Variable name

Type

Description

issueObject

Issue

Deprecated. The issueObject variable is a synonym for the issue variable.

Functions

Functions availability

The functions in JMCF are available on the custom field configuration screen when you write a Groovy script in the Groovy Formula field to return a value expected by a calculated custom field.

...

Function Name

Returns

Description

jqlSearch("<JQL expression>", <maxResults>)

List<Issue>

Search for issues using JQL

Variables and functions used in a Groovy script#getComponent(Class interface)

Component/Service

Global function to get a Component/Service from Jira or any loaded add-on


jqlSearch("<JQL expression>", <maxResults>)

jqlSearch("<JQL expression>", <maxResults>)is a simple function that you can use to search for issues using a JQL. The function expects the following:

  • JQL expression: JQL query

  • maxResults: maximum number of issues to return

When you pass a valid JQL query and number of issues, the function returns a List<Issueof issues that match the JQL.

Example: Calculate the Story points of all issues of a specific project and display them on the current issue as Total Story points.

...

workdaysBetween(<Date from>, <Date to>) is a global function that returns aLongrepresenting the number of work days (excluding Saturdays and Sundays) between two Dateobjects. It returnsnullif one of the two parameters isnull. For example:

  • Code Block
    languagegroovy
    workdaysBetween(issue.created, issue.duedate)

    returns the number of days between the issue creation and the due date

  • Code Block
    languagegroovy
    workdaysBetween(issue.created, new Date()) 

    returns the number of days from the issue creation to now.

...

(warning) Note that the current user will be restored when the code block is exited.

getOrganization(String organizationNameOrID)

getOrganization is a global function that returns a Jira Service Management Organization from its name or ID.

For example:

Code Block
getOrganization("Appfire")

returns the Organization named “Appfire”.

Code Block
getOrganization("5")

return the Organization with ID 5.

getUsersInOrganization(CustomerOrganization organization)

getUsersInOrganization is a global function that returns the users that belong to an organization. The function returns a Set<ApplicationUser> and expects a CustomerOrganization object.

For example:

Code Block
getUsersInOrganization(getOrganization("Appfire"))

returns the users that belong to the Appfire organization.