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!
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:-
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.
2. As expected, the MOCK-2 issue is added to the issue link.
3. I then repeat the same test with issue MOCK-11.
4. As expected, the MOCK-11 issue is added to the issue link.
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
@Ram Kumar Aravindakshan _Adaptavist_
Thank you very much! It works like a charm.
I appreciate all the help and effort in the script.
Norm
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.