Using Groovy scripts in Jira Software/Core or Service Management (Server/Data Center) adds a huge amount of new possibilities in customizing workflows, enhancing the user experience and automating processes. The most powerful App in this area is ScriptRunner for Jira. Out of the box it allows you to use Groovy scripts in:
Behaviours (manipulating field behaviour on create, edit and transition screens)
Custom JQL Functions
Fragments (manipulating Jira’s user interface)
and some useful built-in scripts
By default Jira’s data model is quite static. Also you need to be Jira Administrator to modify fields. In combination with other apps you are able to provide a completely new Jira experience. For example, using the Jira app Insight - Asset Management you can define asset objects and access them in your scripts. By that you can make Jira behave more dynamic.
How to use this blog article?
In this article I will share script snippets for ScriptRunner to handle some common user related use cases I got in touch with. I don’t go into the details of the use cases. The purpose of the snippets is to get you started. Use copy & paste at your own discretion.
Import packages
First of all you import all the packages you’ll need in your script context. For debugging it’s helpful to import org.apache.log4j.Logger classes to be able to write into the Jira logs. You will soon recognize that developing in the integrated ScriptRunner script console without custom logging is quite shitty.
For providing detailed debug information I recommend to use the appropriate log4j log level log.debug(”custom debugging message”). Whether the log.debug statement will appear in the logs, depends on the configured log level of your Jira instance. There is even a webinterface for configuring log levels in Jira Server and Data Center.
Getting the issue object
ScriptRunner scripts can be used in different contexts (listeners, postfunctions, …). How to access the issue object depends on the context of your script. Here some common examples.
Getting the issue object in a listener script
Getting the issue object in script postfunctions
In a ScriptRunner script postfunction you don’t need to fetch the issue object of the current issue. It already exists as a prepopulated variable with name issue. You can directly access it to e.g. get the issue key by using issue.getKey().
Getting the issue object by the issue key
Independent of the context, you can always get the issue object by the issue key.
User related script examples
Now, everything is prepared and you can start using the following script snippets. They’re all tested in Jira 8.5.x (Long Term Support release) with ScriptRunner 6.15.0 and Insight - Asset Management 8.6.15.
Setting approvers by group membership in Jira Service Management
Getting Jira groups from Insight object attributes of the type Group
Seting an issue comment with specific user context e.g. a system user
Further Groovy script use cases within Jira / Jira Service Management
You can cover quite a distance using this approach. Just to give you some inspiration, here is a couple of other interesting ScriptRunner and Insight Groovy script use cases:
Simple password generator within transition post function
Access databases to acquire external information and bring them into Jira
Call a REST interface from workflow transition post functions and handle its response
Using Insight objects linked to an issue or issuetype to:
provide texts, comparable to canned responses, in any textfield e.g. resolution text or processing hints without buying another app