Scriptrunner - How to Link an Issue when a Jira Issue is mentioned in a comment

Nick September 20, 2018

Hi,

Looking for a way to automatically link any issue that is mentioned in a comment, in any thread across all projects.

 

E.g. User 5 comments on a Jira Issue with a link to Jira Issue 101A

Issue 101A is then linked from the original Issue

3 answers

1 vote
Orkun Gedik
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 20, 2018

Hello @Nick,

You can use script listener with MentionIssueCommentEvent. Let me know if you need more detail or help. 

 

Regards,

Orkun Gedik

Nick September 20, 2018

Thanks Orkun, and what code in the inline script part to add it as a Link?

Orkun Gedik
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 20, 2018

You are welcome @Nick.

You can use the following line to create link between issues. 

ComponentAccessor.getIssueLinkManager().createIssueLink(Long sourceIssueId, Long destinationIssueId, Long issueLinkTypeId, Long sequence, ApplicationUser remoteUser)

Source issue and destination issue are depends on how you want to link issues.  

Nick September 20, 2018

Thank you - what do you mean by "Source issue and destination issue are depends on how you want to link issues. 

 

Simply want a "relates to" link between Issues

Nick September 20, 2018

Apparently "ApplicationUser" in the above cannot be resolved

Orkun Gedik
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 20, 2018

What I meant is you can define your source issue and destination issue how link direction you want. Also what version of Jira you are using? If you are using version 6.X, you have to use "com.atlassian.crowd.embedded.api.User" instead of ApplicationUser. "user.getDirectoryUser()" returns the paramater you need.

Nick September 20, 2018

Latest version

I think it's "remoteuser" it doesn't like

Orkun Gedik
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 20, 2018

It must be ApplicationUser. You can check it out here.

Nick September 20, 2018

Hm, if I ignore the error and submit anyway, it doesn't run. "unable to resolve class ApplicationUser"

Orkun Gedik
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 20, 2018

You need to import ApplicationUser. Add the following line in your script.

import com.atlassian.jira.user.ApplicationUser
Nick September 20, 2018

Thanks so total script is

 

import com.atlassian.jira.user.ApplicationUser

ComponentAccessor.getIssueLinkManager().createIssueLink(Long sourceIssueId, Long destinationIssueId, Long issueLinkTypeId, Long sequence, ApplicationUser remoteUser)

I checked in ScriptRunner and it provides this error

The variable ComponentAccessor is undeclared

Nick September 21, 2018

@Orkun Gedik @Tarun Sapra this is amazing functionality when it's working, any ideas why the above would fail?

Thanks so much.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 21, 2018

Hello @Nick

You have to import the component accessor class as well

https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/component/ComponentAccessor.html

Add this in your code in starting of the class

import com.atlassian.jira.component.ComponentAccessor
Nick September 21, 2018

Thanks, now it's

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
ComponentAccessor.getIssueLinkManager().createIssueLink(Long sourceIssueId, Long destinationIssueId, Long issueLinkTypeId, Long sequence, ApplicationUser remoteUser)

 

It shows a [Static type checking] Cannot find matching method

But I assume it will work anyway?

Orkun Gedik
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 24, 2018

You may ignore that error, I assume it works. But the paramater that you are passing in createIssueLink() method is not initiliazed in your code (If you shared all script above).

Nick September 24, 2018

Thanks @Orkun so if I may ask what would be the final code for the script?

Much appreciated

Nick September 27, 2018

@Orkun Gedik @Tarun Sapra I wasn't able to get the able code working in its current form - as experienced Scriptrunner gurus would you happen to know what remains to be tweaked?

Nick October 3, 2018

I wanted to mark this one as Done as I thought it would be really useful for others, the current script is failing when it runs based off the code above, what is missing?

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 3, 2018

Hello @Nick

Can you please paste the complete code which you are using currently in your instance.

Nick October 3, 2018

Hey @Tarun Sapra

Code is:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
ComponentAccessor.getIssueLinkManager().createIssueLink(Long sourceIssueId, Long destinationIssueId, Long issueLinkTypeId, Long sequence, ApplicationUser remoteUser)
Nick October 10, 2018

@Tarun Sapra any ideas?

Nick October 20, 2018

Any @Tarun Sapra @Orkun Gedik any ideas so we can close this?

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 20, 2018

Hello @Nick

The approach suggested by @Orkun Gedik is useful for newly added issues in comments, but not if you want to link issues which are already added in the comments. Because the listener will be triggered based on the event MentionIssueCommentEvent only for new scenarios and not for existing tickets if you wish to link them.

Nick September 20, 2018

Thanks Tarun

Suggest an answer

Log in or Sign up to answer