Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Copy comment to linked issue with event listeners

Pumper1 January 5, 2018

I have two projects: Software and service desk, and wont to copy customer coment from SD to Software. how i can do it with event listeners? all issues have links.

3 answers

Suggest an answer

Log in or Sign up to answer
1 vote
Brian Alden April 2, 2019

Here's what I have so far for the before-mentioned comment event listener idea. It worked in my dev environment last week, but since changing the link type it broke (the comment is copied back to the service desk issue causing a feedback loop). Still fine-tuning. Let me know if you find what I'm missing.

import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.user.ApplicationUser
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def commentManager = ComponentAccessor.getCommentManager()
def issue = event.issue as MutableIssue

// gather the original author and comment body from the original issues comment

def lastcomment = event.getComment()
String lclc = lastcomment.getBody().toLowerCase()
def commentBody = lastcomment.getBody()
def commentAuthor = lastcomment.getAuthorApplicationUser()

// get original issue linked issues
issueLinkManager.getInwardLinks(issue.getId()).each {issueLink ->;
if (issueLink.issueLinkType.name == "Service Desk") {

//if the last comment author is not your automation account (if you have an account that responds to the customer on other listeners) then
if(lastcomment && !(commentAuthor.getDisplayName().equals("AutomationAccount"))) {


// create comment on the linked issue and fire the comment event on that issue
def modCommentBody = "*{color:#3b7fc4}FROM SERVICE DESK (DO NOT REPLY HERE):{color}*\n" + commentBody
commentManager.create(issueLink.getSourceObject(), commentAuthor.getName(), modCommentBody , true)
}
}
}

 

Brian Alden April 3, 2019

Found my problem - change getDestinationObject() to getSourceObject(). So you'll need to check the link type direction for your setup. If you use Outward, use Destination; if you use Inward, use Source. Previous comment corrected.

Dante Labate April 27, 2019

@Brian Alden 

 

I have 2 projects (one SD e the outher Software)

SD = Project A
Soft = Project B

I have managed through your script to copy the comments made in project A to project B.

I'm having a hard time doing the reverse, copying from Project B to Project A

In my theory, it would just change the project the listener is checking, but nothing happens ..
Would you help me?

Brian Alden April 27, 2019

@Dante Labate 

When you make the reverse listener, you need to flip 'getSourceObject()' to 'getDestinationObject()' - this is in reference to the issue link direction.

Another thing to keep in mind is that the comment author must have comment permission in the project you are copying to, in this case your SD project. And if the user commenting in the software project doesn't have an Agent license, their comment will be made on the SD project as an internal comment (not visible to the portal).

Good luck!

Dante April 27, 2019

@Brian Alden 

Thank you!

I was able to create the reverse.

In the Project SD listener for Project Soft I had to use if to check if the last comment was from my jirabot user.

In the Project Soft listener for Project SD, I removed this check.

Now it's working perfectly!

again, thank you!

Ollie Peacey March 8, 2023

Hi @Brian Alden apologies to bring up an old thread but I don't suppose you have a copy of your script to use in Jira Cloud? Thanks

0 votes
Daniel Westgate June 6, 2019

Can script runner copy comments from linked issues, across different projects?

Ivan Tovbin
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.
June 6, 2019

Yes, as long as those projects are within a single Jira instance. If not you'll have to use REST API to access issues from another Jira.

Daniel Westgate June 6, 2019

That's grand, thank you.

We are using the Jira cloud hosted solution, is that going to cause us an issue achieving this?

0 votes
Ivan Tovbin
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.
January 6, 2018

It is my understanding that the issue you want your comment to copy to is linked to the source issue, yes? Also, every single comment should be copied? If that is the case, then the algorithm is pretty easy:

1) Create a custom listener that would trigger on 'Issue commented' event.

2) Write a logic so that only specific issues are processed (i.e. issues from your SD project)

3) Get the body of the last added comment and use it to create a new comment in a linked issue.

jirajester July 13, 2018

Cool... @Ivan Tovbin, where/how do you set up step 3?

TAGS
AUG Leaders

Atlassian Community Events