I've setup a Custom listener to copy comments between our Service Desk and Dev project. The commenting works fine from the Dev project to the Service Desk issue but the reverse does not. I get no errors, it just doesn't update.
I am using the same code on either side, two listeners, just referencing the Linked item Key.
I also tried changing the link types on the one not working from outward to inward but still no luck.
Here is the code, anyone have any thoughts:
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
// gather the original author and comment body from the original issues comment
def newComment = event.getComment()
def originalAuthor = newComment.getAuthorApplicationUser()
def commentBody = newComment.getBody()
// get original issue's linked issues
linker.getOutwardLinks( issue.getId() ).each {
if (it.destinationObject.getProjectObject().getKey() == 'IN')
{
if( commentBody != "" )
{
// create comment on the linked issue and fire the comment event on that issue
commentMgr.create(it.getDestinationObject(), originalAuthor.getName(), commentBody , true)
}
}
}
Hi @Jon Starbird,
@Elifcan Cakmak is right. If comment author is a customer (and doesn't have an access to DEV project), originalAuthor can't comment.
I also tried changing the link types on the one not working from outward to inward but still no luck.
If you link issues from DEV to SD, the above code will copy comments from DEV to SD (outward). But in order to copy comments from SD to DEV, you have to use inward link type.
Please check;
def authContext = ComponentAccessor.getJiraAuthenticationContext();
authContext.setLoggedInUser(ComponentAccessor.getUserManager().getUserByKey("admin"));
ApplicationUser adminUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
linker.getInwardLinks(issue.getId()).each {
if (it.sourceObject.getProjectObject().getKey().equals('DEV PROJECT KEY')){
if( commentBody != "" ){
commentMgr.create(it.getDestinationObject(), adminUser.getName(), commentBody , true)
}
}
}
I've used Inward as well , same results, just doesn't work. No errors.
Permissions are not the issue.
Adding some logging it isn't seeing that Comment is made. The event fires but comment body is empty for some reason.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I did notice that Scriptrunner has an event type of ServiceDeskCommentEvent, when I added that I get an error:
2019-01-17 10:25:25,816 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.servicedesk.internal.comment.event.ServiceDeskCommentEventImpl, file: <inline script> groovy.lang.MissingMethodException: No signature of method: com.atlassian.servicedesk.internal.comment.ServiceDeskCommentImpl.getBody() is applicable for argument types: () values: [] Possible solutions: getAt(java.lang.String), getClass(), notify(), every() at com.atlassian.jira.issue.comments.Comment$getBody$0.call(Unknown Source) at Script406.run(Script406.groovy:20)
Could the issue be how you are supposed to get Comments from Service Desk?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
One change I did try from your suggestion was to change the getprojectobject().getkey from a destinationobject to a sourceobject like so:
def linker = ComponentAccessor.getIssueLinkManager()
def attachmentMgr = ComponentAccessor.getAttachmentManager()
def commentMgr = ComponentAccessor.getCommentManager()
// gather the original author and comment body from the original issues comment
def newComment = event.getComment()
def originalAuthor = event.getUser()
def commentBody = newComment.getBody()
// get original issue's linked issues
linker.getInwardLinks(issue.getId() ).each {
if (it.sourceObject.getProjectObject().getKey() == 'IN')
{
if( commentBody != "" )
{
// create comment on the linked issue and fire the comment event on that issue
commentMgr.create(it.getDestinationObject(), originalAuthor.getName(), commentBody , true)
}
}
}
However, this caused a stackoverflow and ended up creating many dupe comments in the same parent SD issue instead of the linked Clones issue.
The issues I am trying to add a comment two were Cloned from the SD issue originally.
As I said the Users making the comments do have permissions to add comments to the other project since we only use SD internally.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Accepting your answer as yours got me the closest.
Your answer did need one correct, it should be like this:
def linker = ComponentAccessor.getIssueLinkManager()
def attachmentMgr = ComponentAccessor.getAttachmentManager()
def commentMgr = ComponentAccessor.getCommentManager()
// gather the original author and comment body from the original issues comment
def newComment = event.getComment()
def originalAuthor = event.getUser() //newComment.getAuthorApplicationUser()
def commentBody = newComment.getBody()
// get original issue's linked issues
def links = linker.getInwardLinks(issue.getId())
for (link in links) {
if (link.sourceObject.getProjectObject().getKey() == 'IN')
{
if( commentBody != "" )
{
// create comment on the linked issue and fire the comment event on that issue
commentMgr.create(link.getSourceObject(), originalAuthor.getName(), commentBody , true)
}
}
}
Needed to change that last getDestinationObject to getSourceObject and then it would update the IN issue correctly.
Still have an issue though, since I need comments to go back and forth between them I have two listeners and they are triggering each other. So as I add the comment to one it triggers the other, goes on until it crashes.
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.
Hello,
Did you try to debug the code? For example, you can print the originalAuthor to the screen in order to see if the script can actually catch the user who made the comment. You say script works from Software project to Service Desk project but not working vice versa. Could it be because the user who comments on Service Desk Project is in Service Desk Team or a customer and is not in the application users of Jira Software? That was the first thing that came to my mind.
Regards,
Elifcan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the reply but permissions are fine, I had checked that.
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.