Script Listener - Migrate to cloud

Hi Team,

We are trying to migrate script listener from DC to cloud. We have a listener which will check the description of an “Epic”. If it contains an issue key of some pattern, for example CR-■■■, it will automatically add issue link between the current issue(epic) and CR-■■■ issue. Here is the working code for DC :

import com.atlassian.jira.component.ComponentAccessor
def applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def LINKID = 10000 // Clone link type, Change this if you want different link type
def SEQUENCE = 10001 // Don't change the default value
//def PATTERN = /(?:\s|^|\|)([crCR]+-[0-9]+)(?=\s|$|\|\.)/
def PATTERN = /(?:\s|^|\|)([crCR]+-[0-9]+)/
def issue = event.issue
String desc = issue.description
log.warn('Description: ' + desc)
def issuetype = issue.getIssueType().getName()
log.warn('Issue Type: ' + issuetype)

if (issuetype == "Epic") {
	def matcher = desc =~ PATTERN
	//??Add this to limit execution for changes to Description field only?
	//if(event?.getChangeLog()?.getRelated('ChildChangeItem').find {it.field == 'description'}) {
	while(matcher.find()) {
		def destIssue
    	log.warn('Match Group 0: ' + matcher.group(0))
    	log.warn('Match Group 1: ' + matcher.group(1))
    	try{
        	destIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(matcher.group(1).trim().toUpperCase())
	    	log.warn('Dest: ' + destIssue)
    	    ComponentAccessor.getIssueLinkManager().createIssueLink(issue.id, destIssue.id, LINKID, SEQUENCE, applicationUser)
    	} catch(Exception ex) {
        	log.warn("Error linking the issue." + Exception + ex)
	    }
	}
}

We are looking to achieve the same functionality in cloud. Any help is much appreciated.

Thanks,
Swathi

Hi Swathi,
Hope you are doing well. With Cloud, you need to work with Jira Cloud REST API to make this Listener work.

Sharing some samples from our Adaptavist Library for how it could look in Cloud.

You can likely define the constants along these lines;

def PATTERN = /(?:\s|^||)(DM-[0-9]+)/
def issueKey = issue.key
def issuetype = issue.fields.issuetype.name
def description = issue.fields.description

And then have something like…

if (issuetype == “Epic” && description) {
def matcher = description =~ PATTERN
while (matcher.find()) {
def matchedIssueKey = matcher.group(1).trim().toUpperCase()

or something…I’m not sure of the full process which you want to do, feel free to drop an email to customersuccess@scriptrunnerhq.com for one of us CSM’s to take a look with you, or you can open a support ticket with our Support Engineers to help on the more technical end.

Regards,
Sean