/
Groovy Variables and Functions

Groovy Variables and Functions

When running Groovy scripts, JMWE makes contextual information available to your script through built-in variables, functions, the Component/Service Manager and Jira Standard Services. 

Note: you can also define custom variables in your Groovy script!

Using Groovy Variables and Functions

The variables are available anywhere that a Groovy editor is used. Additionally, clicking on the Globals tab provided under the Groovy editor displays the list of available variables and functions. These are categorized below based on the context.

Current issue

issue

The issue variable exposes the methods of the main Issue interface, including methods not native to Jira such as 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 name of priority of the issue. 

originalIssue

The originalIssue variable exposes the methods of the main Issue interface, including methods not native to Jira such as get()getLinkedIssues() etc. It points to the current Issue object, as it was before the transition started. You can access the fields of the issue by accessing the properties and methods of this variable. 

For example: originalIssue.get("assignee")?.name returns the assignee of the issue before the transition.

relatedIssue

The relatedIssue variable is similar to the issue variable except that it points to the related issue in the Groovy context. It is a synonym to the Variables and functions used in a Groovy expression#linkedIssue variable (is now obsolete) and is only available from post-functions that work on related issues. It is used to insert data of each related issue being processed by the post-function, in turn. You can access the fields of the related issue by accessing the properties and methods of this variable. 

For example: relatedIssue.get("status").getName() returns the status of the related issue in context.

linkedIssue

This variable is obsolete. Use relatedIssue variable instead.

The linkedIssue variable is similar to the Variables and functions used in a Groovy expression#issue variable except that it points to the linked issue in the Groovy context. You can access the fields of the linked issue by accessing the properties and methods of this variable. This is available in the post-functions that process the linked issues of the current issue. 

For example: linkedIssue.get("status").getName() returns the status of the linked issue

parentIssue

The parentIssue variable is similar to the Variables and functions used in a Groovy expression#issue variable except that it points to the parent issue in the Groovy context. You can access the fields of the parent issue by accessing the properties and methods of this variable. This is available in the post-functions that process the parent of the current issue.

For example: return(parentIssue?.get("assignee")?.getName()) returns the username of the user the parent issue is assigned to.

Execution context

currentUser

The currentUser variable holds an ApplicationUser representing the current user (i.e. the user triggering the transition) in the Groovy context. 

For example: currentUser.name returns the username of the current user.

transientVars

transientVars

Note: the elements listed below are available for all issue events except those related to issue watchers being added or deleted.

The transientVars variable is a Map of variables that provide important information about the current transition. 

These include the raw event object as well as variables for comment-related events, change history related events, worklog data, and new issue events, if any.

Note: the content of the raw event object will vary depending on the event! The list below is a sample of the data returned.

Example raw event object variables

issue: the issue id
user: the user who triggered the event
comment: the comment, if any, of the original event
subtasksUpdated: true/false flag on whether or not subtasks were updated
sendMail: true/false flag on whether or not email notifications were sent
time: the time of the original event

Comment-related event variables

comment: the comment body
roleLevel: role level id for the minimum role to see the comment (if any)
commentLevel: the group level required to see the comment (if any)
commentProperty: the entityProperty for the comment (if available)

Change history event variables

fieldChanges: a List<ChangeItemBean with all the changes contained in the event
originalAssigneeId: the old assignee, if changed by what triggered the event
oldStatus: the old status, if changed by what triggered the event

For example:

Comment added during the current transition:

transientVars.comment

Assign the linked issue to the assignee of the issue before the current transition

transientVars.originalAssigneeId

Copy the attachments of the issue to the newly created issue

import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.AttachmentManager AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager() def attach = issue.get("attachment") attach.each{ attachmentManager.copyAttachment(it,currentUser,transientVars.newIssue?.key) }

 

Html rendered comment added during the current transition:

Others

now

The now variable holds the current date and time, as a java.util.Date object.

today

The today variable contains a java.util.Date object representing the current date (without time) in the time zone of the current user. To get the current date in the time zone of a specific user, use new DateOnly(ApplicationUser user). For example: new DateOnly(issue.assignee) will return the current date in the time zone of the current issue’s Assignee.

currentValue

The currentValue variable holds the current value of the field being set by a post-function. This is applicable only in the Set field value and Set field value of linked issues post-functions. The data type of the variable depends on the field being set.

For example: 

currentValue returns a user object if the field being set is Assignee

currentValue returns a collection of versions if the field being set is Fix Version/s

newIssueKey

The newIssueKey variable holds the key of the newly created issue. This is applicable only in the Create/Clone issue post-function. The data type of the variable is a String.

For example:

newIssueKey returns the key of the newly created issue.

linkTypeName

The linkTypeName variable holds the name of the link type linking the current issue to the linked issue being considered for removal. This is applicable only in the Unlink issues from the current issue post-function. The data type of the variable is a String.

For example: 

linkTypeName returns the name of the first link type linking the current issue to the linked issue

textutils

The textutils variable is a utility object of class TextUtils providing useful methods to manipulate text and HTML. This is applicable only in the post-functions that use Groovy templating.

For example:

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.  

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.

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.

Deprecated variables

Variable name

Type

Description

Variable name

Type

Description

issueObject

Issue

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

linkedIssue

Issue

Deprecated. The linkedIssue variable is a synonym for the relatedIssue variable.