Part 2 How to deploy Adaptivist ScriptRunner scripts and objects

In the first part of this article we created several scripts, packaged them into a plugin, installed the plugin and called one of the scripts from the Script Console.

In this part of the article we will try to deploy ScriptRunner objects such as a Listener, a REST endpoint, a Script fragment and a Scripted field.

You can take the source code of the plugin here.

Create a Listener, a REST endpoint and a Script fragment

Let's first create a listener, a REST endpoint and a script fragment in our dev environment. 

The first listener is a custom listener:

custom listenwe.pngThe second listener is the Adds the current user as a watcher listener:

list.pngThe REST endpoint looks like this:

rest.pngThe Script fragment looks like this:

webitem.pngAfter we created all the items we need to add them to our plugin so that the items would be installed after our plugin installation.

Go to Add-ons->Built-In Scripts->Configuration Exporter and choose all our created objects.

push.pngPush the Run button and you will see something like this:

 push2.jpg

Copy all the text in the selected red rectangle and add it to the src/main/resources/scriptrunner.yaml file. The file will look like this:

!descriptor
fragmentConfigItems:
- FIELD_DO_WHAT: NAVIGATE
FIELD_KEY: ru-matveev-alexey-web-item
FIELD_LINK_CONDITION:
- ''
- ''
FIELD_LINK_DESTINATION: ''
FIELD_MENU_LABEL: ''
FIELD_NOTES: Web Item
FIELD_SECTION: add-attachments-link
FIELD_STYLE_CLASS: ''
FIELD_WEIGHT: ''
canned-script: com.onresolve.scriptrunner.canned.jira.fragments.CustomWebItem
id: '520053084'
restConfigItems:
- FIELD_INLINE_SCRIPT: ''
FIELD_NOTES: REST endpoint
FIELD_SCRIPT_FILE: ru/matveev/alexey/main/rest/rest.groovy
canned-script: com.onresolve.scriptrunner.canned.common.rest.CustomRestEndpoint
id: '-168713291'
scriptListeners:
- FIELD_FUNCTION_ID: cf09831f83bc75ec27076557034b952dfc727040
FIELD_INLINE_SCRIPT: ''
FIELD_LISTENER_NOTES: Custom Listener
canned-script: com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener
clazz: ru/matveev/alexey/main/listeners/listener.groovy
events:
- 1
id: '-586588827'
params: '{"FIELD_LISTENER_NOTES":"Custom Listener","projects":"","events":"1","FIELD_INLINE_SCRIPT":"","clazz":"ru/matveev/alexey/main/listeners/listener.groovy","FIELD_FUNCTION_ID":"cf09831f83bc75ec27076557034b952dfc727040","canned-script":"com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener","id":"-268926325"}'
projects:
- ''
- FIELD_CONDITION: []
FIELD_FUNCTION_ID: ''
FIELD_LISTENER_NOTES: Add the current user as a watcher
canned-script: com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.AddWatcher
events:
- 1
params: '{"FIELD_LISTENER_NOTES":"Add the current user as a watcher","projects":"","events":"1","FIELD_CONDITION":["",""],"FIELD_FUNCTION_ID":"","canned-script":"com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.AddWatcher"}'
projects:
- ''

Add the scripted field 

Unfortunately, the Export Configuration option can not export scripted fields. That is why we need to create scripted fields in our code. We will use the Upgrade Task feature of Jira. The Upgrade Task lets you launch a piece of code on plugin startup. An upgrade task will be executed only if it has never been executed yet. You need to make a class, which will implement the PluginUpgradeTask interface. Also this class must be exported as a public service.

First we create the src/main/groovy/ru/matveev/alexey/scriptedfields/AbstractUpgradeTask.groovy file (this file is taken for the sample ScriptRunner plugin):

package ru.matveev.alexey.scriptedfields

import com.atlassian.plugin.osgi.util.OsgiHeaderUtil
import groovy.util.logging.Log4j;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

@Log4j
abstract class AbstractUpgradeTask {
public String getPluginKey() {
Bundle bundle = FrameworkUtil.getBundle(AbstractUpgradeTask.class);
return OsgiHeaderUtil.getPluginKey(bundle);
}
}

This class implements the getPluginKey method, which is one of the methods of the PluginUpgradeTask interface. This method returns the plugin key.

Then we create src/main/groovy/ru/matveev/alexey/scriptedfields/CreateScriptFieldUpgradeTask.groovy(this script was taken from the sample ScriptRunner plugin):

package ru.matveev.alexey.scriptedfields

import com.atlassian.jira.issue.context.GlobalIssueContext
import com.atlassian.plugin.spring.scanner.annotation.export.ExportAsService
import com.atlassian.sal.api.message.Message
import com.atlassian.sal.api.upgrade.PluginUpgradeTask
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.test.ScriptFieldCreationInfo
import groovy.util.logging.Log4j

import javax.inject.Named

@Log4j
@Named
@ExportAsService
class CreateScriptFieldUpgradeTask extends AbstractUpgradeTask implements PluginUpgradeTask {


@Override
int getBuildNumber() {
return 1
}

@Override
String getShortDescription() {
return "This upgrade task creates a scripted field"
}

@Override
Collection<Message> doUpgrade() throws Exception {
def scriptFieldCreation = ScriptFieldCreationInfo.Builder.newBuilder()
.setName("TestScriptFieldSimpleNumberX")
.setSearcherKey(ScriptRunnerImpl.PLUGIN_KEY + ":exactnumber")
.setTemplate("float")
.setContexts([GlobalIssueContext.instance])
.setScriptFile("ru/matveev/alexey/main/scriptedfields/scriptedfield.groovy")
.build()
scriptFieldCreation.create()
return null
}

}

This class is created as a public service, it extends the AbstractUpgradeTask and implements the PluginUpgradeTask. The doUpgrade method creates our custom field.

Test the plugin

You can open Terminal and execute atlas-run or you can install a Jira instance and add the plugin. As a result you will see the that all our object were created.

Our scripted field:

scriptedfield.jpgOur listeners:

listeners.jpgOur REST endpoint:

 rest.jpgOur Web fragment:

scriptfra.jpgWe can see all our object, which means that we successfully created all the ScriptRunner objects.

Now you can deploy our scripts and objects to any Jira environment by installing the developed in this article plugin.

10 comments

Gonchik Tsymzhitov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 14, 2018

Hi! 

Thanks for share your knowledge! 

 

Cheers,

Gonchik Tsymzhitov

Matt Doar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 14, 2018

This is very useful for Jira admins of Jira instances with a lot of ScriptRunner configuration. I'm glad to see the links to the Adaptavist doc and source pages

Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 17, 2018

I love the power that ScriptRunner adds to Jira. Thanks for the article.

Ollie Guan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 20, 2018

Thanks for share your knowledge! 

Deleted user July 17, 2018

Hello, I don't really understand how can I deploy my scriptedfields.

I need to edit the file CreateScriptFieldUpgradeTask.groovy and put in the doUpgrade method all of my scripted fields (more than 20...) ?

Deleted user August 1, 2018

Up

Deleted user November 21, 2018

Thanks for sharing! But I have some follow up questions in this post. Can you maybe provide some help?

gesundes February 8, 2021

Great article! Here I found the example of a scripted field creation but could someone provide an example of how to create behaviours via code?

As I understand it's similar to the code for scripted fields but I couldn't find any example.

Boaz April 22, 2021

According to the documentation and Adaptavist support it is currently not possible to create behaviours via code, but they can be created and mapped via the REST API described here https://docs.adaptavist.com/sr4js/6.23.0/features/behaviours/migrating-behaviours 

Suresh Sakhare
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 22, 2021

@Alexey Matveev :  Thanks for sharing this information. Is it similar for Confluence Scriptrunner? 

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events