Link Issue mentioned in Jira comment with Scriptrunner Listener

Norm Moore April 5, 2023

Hey All,

I'm looking for some scripting help with a Scriptrunner Listener script. I'm in need of a full listener script that will create an issue link when an issue is mentioned in the comments of the current issue. 

I found another article but it was a snippet of the script and some of the groovy wasn't valid. 

Any help and/or guidance would be greatly appreciated.

Cheers!

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 7, 2023

Hi @Norm Moore

For your requirement, you can try something like this:-

import com.adaptavist.hapi.jira.issues.Issues
import com.atlassian.jira.issue.MutableIssue
import org.apache.commons.lang3.StringUtils

def issue = event.issue as MutableIssue
def lastComment = issue.comments.last()
def resultBuilder = new StringBuilder()

if (lastComment.body.contains("${issue.projectObject.key}-")) {
def filter1 = issue.projectObject.key.charAt(0)
def filter2 = StringUtils.substringAfter(lastComment.body,"${filter1}").split("\\s+")
resultBuilder.append(filter1).append(filter2.first())
}

def linkedIssue = Issues.getByKey(resultBuilder.toString())
issue.link('blocks', linkedIssue)

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Listener Configuration:-

listener_config.png

For the sample code above to work, please ensure you have updated your ScriptRunner plugin to the latest release, i.e. 8.0.0.

With the latest release, you will be able to make use of ScriptRunner's HAPI feature, which helps to simplify the code.

Below are a couple of test screenshots for your reference:-

1. First, I added a comment and included an issue key. In the sample below, I have entered MOCK-2 for the issue key. I then click the add button.

image1.png

2. As expected, the MOCK-2 issue is added to the issue link.

image2.png

3. I then repeat the same test with issue MOCK-11.image3.png

 

4. As expected, the MOCK-11 issue is added to the issue link.image4.png

Note: Please ensure that you refresh the page after adding the comment to display the linked issue.

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram 

Norm Moore April 10, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 

Thank you very much! It works like a charm. 

I appreciate all the help and effort in the script. 

Norm

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.11
TAGS
AUG Leaders

Atlassian Community Events