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.
A NumberTool instance that can be used to format number values.
In JMCF versions prior to 2.0.0, the issue variable was a wrapper around the Issue object that just implemented a get() method to access the value of any field, and the issueObject represented Jira's main Issue object. Now, these two variables are merged into one, issue. This variable now exposes all the methods of Jira's Issue interface as well as additional methods such as get(), getEpic() etc. You can still use the issueObject but it is deprecated and it will be removed in a future version.
The issue variable exposes the methods of the main Issue interface, including methods not native to Jira such as like get(), getLinkedIssues() etc. It points to the current issue being processed. You can access the fields of the issue by accessing the properties and methods of this variable.
For example: issue.get("priority").getName() returns the priority of the issue.
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.
To format it to a currency:
The value is formatted to $40.
To add an IMG tag to display an icon to the left of the number:
The textutils variable is a utilityobject 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.
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.
For example: Set a user field with the assignee and be warned when the issue is unassigned.
So when the issue is unassigned, the warning message is displayed in the atlassian-jira.log file.
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.
//Define a boolean variable to false
boolean isValue = false;
//Run a loop on all the current values of the field
if (it.getName == "2.0")
isValue = true;
getComponent(Class interface) is a global function to get a Component/Service from Jira or any loaded add-on. The function expects a Class interface as the parameter. Importing classes from third-party add-ons or some classes of Jira and its Java dependencies is not easy. You need to get the Class loader and find the class followed by getting the Component/Service. For example, to get the RapidViewServiceInterface you had to write the following code:
Note that you can also access the internal components/services that are registered as private (not "public") using this function. For example to get the DevStatusSummaryService from Jira's development integration plugin (which gives access to builds, commits, etc.)
secondsBetween(<Date from>, <Date to>) is a global function that returns a Long representing the number of seconds between two Date objects. It returnsnullif one of the two parameters isnull. For example:
returns the number of seconds between the issue creation and the due date.
secondsBetween(issue.created, new Date())
returns the number of seconds from the issue creation to now.
secondsBetween(Date from, Date to, String roundTo)
secondsBetween(<Date from>, <Date to>, String roundTo) is a global function that returns a Long representing the number of seconds between two Date objects optionally rounding the number of seconds to the nearest minute, hour, day or week. It returnsnullif one of the two parameters isnull.
<roundTo>: is either "max" or one of "weeks", "days", "hours", "minutes" (or their equivalent: "w", "d", "h", "m"). If the rounding is "max", it will be rounded to the largest unit reached by the duration. For example:
If secondsBetween(issue.created, issue.duedate) returns 2 hours 51 minutes,
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:
returns the number of days between the issue creation and the due date
workdaysBetween(issue.created, new Date())
returns the number of days from the issue creation to now.
asUser() is a simple global function that runs a code impersonating a user. During the execution of the code block, the current user will be set to the user with the specified username. The function expects the following:
username: Username of the user to impersonate
codeblock: Code block to run while impersonating a user
returns jdoe, regardless of who the current user is.
Note that the current user will be restored when the code block is exited.
asUser(ApplicationUser user) is a simple global function that runs a code impersonating a user. During the execution of the code block, the current user will be set to the specified user. The function expects the following: