Versions Compared

Key

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

When running Groovy scripts, JMWE makes contextual information available to your script through built-in variables, functions, Component/Service Manager and Jira Standard Services. This document details them. Note that you can also define custom variables in your Groovy script.

On this page:

Table of Contents
minLevel2
maxLevel34

Available Variables

The variables are available in the:

...

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. 

...

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.

...

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.

linkedIssue

Issue

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

Note

In JMWE versions prior to 5.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.

Functions

The functions are available in the:

...

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.

Examples:

  • Fetch the first ticket of a specific project that has the same Cascading value set as of the current issue

    Code Block
    languagegroovy
    issues = jqlSearch("project=TP and Cascade in CascadeOption(${issue.get("Cascade")?.get(null)?.getValue()},${issue.get("Cascade")?.get("1")?.getValue()})",1)


  • Fetch the first 10 issues that have the same Fix Version/s as the first Fix Version/s of the current issue.

    Code Block
    languagegroovy
    def versions = issue.get("fixVersions")
    def issues = []
    if (versions){
    issues = jqlSearch("fixVersion = ${versions.first()}", 10)
    }
    issues

...

workdaysBetween(<Date from>, <Date to>) is a global function that returns a Long representing the number of work days (excluding Saturdays and Sundays) between two Date objects. It returns null if one of the two parameters is null. 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.

...

Code Block
getOrganization('Appfire').usersInOrganization

getTransitionAttachments()

getTransitionAttachments is a global function that returns the Attachments that have been added on the transition or issue creation screen. The function returns a List<Attachment>.

Accessing any Component/Service Manager

...

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. Until 5.1.0 importing classes from third party add-ons or some classes of Jira and its Java dependencies was not easy. You had 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:

Code Block
languagegroovy
import com.atlassian.jira.component.ComponentAccessor
def classLoader = issue.get("Rank").getClass().getClassLoader()
def RapidViewServiceIntf = classLoader.findClass("com.atlassian.greenhopper.service.rapid.view.RapidViewService")
def RapidViewService = ComponentAccessor.getOSGiComponentInstanceOfType(RapidViewServiceIntf)

...

You can access a few Standard Jira's services from the Groovy editor. Hover over any button to get the information, click on it to insert it into the editor. 

Jira Standard Service

Returns

Example

AttachmentManager

Jira's AttachmentManager service. Manages all attachment related tasks in JIRA, which involves retrieving an attachment, creating an attachment and deleting an attachment.

Copy attachments from the current issue to issue whose key is TEST-123

ComponentAccessor.attachmentManager.copyAttachments(issue, currentUser , "TEST-123" )

ChangeManager

Returns Jira's ChangeHistoryManager service, which manages the change history of issues.ChangeHistoryManager

To get the change history of the current issue:

ComponentAccessor.changeHistoryManager.getChangeHistories(issue)

ConstantsManager

Returns Jira's ConstantsManager service, which manages issue types, statuses, priorities and resolutions

To get all the Resolutions of the instance

ComponentAccessor.constantsManager.resolutionObjects

GroupManager

Returns Jira's GroupManager service, which manages user groups.

To get all the users in a group

ComponentAccessor.groupManager.getUsersInGroup("jira-administrators")

IssueLinkManager

Returns Jira's IssueLinkManager service, which manages issue links.

To check whether Issue Linking is currently enabled in Jira

ComponentAccessor.issueLinkManager.isLinkingEnabled()

IssueManager

Returns Jira's IssueManager service, which, in particular, allows you to load issues from keys.

To get the Issue that has the given key

ComponentAccessor.issueManager.getIssueObject("TEST-123")

IssueSecurityLevelManager

Returns Jira's IssueSecurityLevelManager service.

To get the different levels of security that the current user can see across all projects.

ComponentAccessor.issueSecurityLevelManager.getAllSecurityLevelsForUser(currentUser)

JiraAuthenticationContext

Returns Jira's JiraAuthenticationContext service. The JiraAuthenticationContext is used for tracking a user's session in JIRA and all it's custom parameters, such as Locale and I18n.

To get the logged in user

ComponentAccessor.jiraAuthenticationContext.loggedInUser

JiraDurationUtils

Returns Jira's JiraDurationUtils service, which is reponsible for printing durations in various formats.


ProjectComponentManager

Returns Jira's ProjectComponentManager service, which manages project Components.

To find components by the lead

ComponentAccessor.projectComponentManager.findComponentsByLead(currentUser.name)

ProjectManager

Returns Jira's ProjectManager service

To get all projects ordered by name:

ComponentAccessor.projectManager.projectObjects

ProjectRoleManager

Returns Jira's ProjectRoleManager service, which allows you to check role membership.

To get project role object by name

getComponent(com.atlassian.jira.security.roles.ProjectRoleManager).getProjectRole("Administrators")

UserManager

Returns Jira's UserManager service.

To get a user by username

ComponentAccessor.userManager.getUserByName("jdoe")

UserPropertyManager

Returns Jira's UserPropertyManager service, which gives access to user properties.

To get user property set of the current user

ComponentAccessor.userPropertyManager.getPropertySet(currentUser)

UserUtil

Returns Jira's UserUtil service, which provides user management capabilities.

To get a list of Jira Admin users:

ComponentAccessor.userUtil.jiraAdministrators

VersionManager

Returns Jira's VersionManager service, which manages project Versions.

To get all the released versions including archived

ComponentAccessor.versionManager.getAllVersionsReleased(true)

...