Copy comments between linked issues in two different projects

Nina Zolotova September 7, 2018

Hello everyone.

I have two projects - SD and MA. On a particular step of SD workflow issues are cloned to project MA.

I want to sync comments between an original issue and a cloned one, so I've written a listener.

If I add a comment to project MA issue, this code works correctly (copies comment from MA to SD):

import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.AttachmentManager
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 linker = ComponentAccessor.getIssueLinkManager()
def attachmentMgr = ComponentAccessor.getAttachmentManager()
def commentMgr = ComponentAccessor.getCommentManager()
def issue = event.issue as MutableIssue


def newComment = event.getComment()
def originalAuthor = newComment.getAuthorApplicationUser()
def commentBody = newComment.getBody()

linker.getOutwardLinks( issue.getId() ).each {
if( commentBody != "" )
{
commentMgr.create(it.getDestinationObject(), originalAuthor.getName(), commentBody , true)
}
}
 

For copying comments from SD to MA I only changed getOutwardLinks to getInwardLinks

import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.AttachmentManager
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 linker = ComponentAccessor.getIssueLinkManager()
def attachmentMgr = ComponentAccessor.getAttachmentManager()
def commentMgr = ComponentAccessor.getCommentManager()
def issue = event.issue as MutableIssue


def newComment = event.getComment()
def originalAuthor = newComment.getAuthorApplicationUser()
def commentBody = newComment.getBody()

linker.getInwardLinks( issue.getId() ).each {
if( commentBody != "" )
{
commentMgr.create(it.getDestinationObject(), originalAuthor.getName(), commentBody , true)
}
}

and it doesn't work at all. It adds comment to SD-issue more than 100 times, but doesn't copy it to MA-issue...

Please help me to fix it.

1 answer

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

Hello @Nina Zolotova

Can you add some logs in the script so that trouble shooting becomes easier, also I guess these 2 are separate listeners.

For copying from SD to MA, can you test with getInwardLinks and see what the result is as the listener if configured for only SD project then it should do the trick.

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 7, 2018

What you can also try is use "getLinkCollection" instead of inward and outward links

 

https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/issue/link/IssueLinkManager.html#getLinkCollection-com.atlassian.jira.issue.Issue-com.atlassian.jira.user.ApplicationUser-

And you will get LinkCollection

https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/issue/link/LinkCollection.html

And then just use .each opetaror and add the comments on the linked issue which have the project key as that of the target project. 

Suggest an answer

Log in or Sign up to answer