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

Hi Team , i have created a custom listener but getting some error message in the script

shrikant maheshwari July 16, 2021

Hi Team,

I want to copy the public comments only between the linked issues. I have written a script to copy all the comments but how to just copy the comments only when they are made public. 

Also i a getting a syntax error in the script , please find the below code, could you please suggest what is wrong in the script also what changes needs to be made in order to only copy the public comments. 

Error Message - "The variable [event] is undeclared", pfa screen shot for the same.listener - copy comments.PNG

SCRIPT STARTED -

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.component.ComponentAccessor;

def event = event as IssueEvent

def comment = event.getComment()

def issue = event.getIssue()


CommentManager commentManager = ComponentAccessor.getCommentManager()
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

String commentBody = comment.getBody()

issueLinkManager.getOutwardLinks(issue.getId()).each {IssueLink issueLink ->
commentManager.create(issueLink.getDestinationObject(), loggedInUser, commentBody, false)
}

issueLinkManager.getInwardLinks(issue.getId()).each {IssueLink issueLink ->
commentManager.create(issueLink.getSourceObject(), loggedInUser, commentBody, false)
}

SCRIPT End

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 16, 2021

Do you get any errors in the log when the script executes?

Static Type Checking errors are not always fatal. In this case, the editor (is this the script file editor or the listener script editor field?) may not be aware of the built-in variable that the listener scriptrunner context.

Other times, the static type checking errors can happen when certain variables are declared as groovy object (using def) and the editor can't confirm what type it is to check against some methods that expect certain types. Often, code with these errors will compile and execute without errors.

 

To check if a comment is internal add this to the top of your script:

import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import groovy.json.JsonSlurper

def
commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
final def SD_PUBLIC_COMMENT = "sd.public.comment"

def isInternal = { Comment c ->
def commentProperty = commentPropertyService.getProperty(loggedInUser, c.id, SD_PUBLIC_COMMENT).getEntityProperty().getOrNull()
if (commentProperty) {
def props = new JsonSlurper().parseText(commentProperty.getValue())
props['internal']?.toBoolean()
} else {
null
}
}

 And then call this closure with

if(!isInternal(comment){}

 Full script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import groovy.json.JsonSlurper
final def SD_PUBLIC_COMMENT = "sd.public.comment"

def event = event as IssueEvent

def comment = event.getComment()

def issue = event.getIssue()

def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
CommentManager commentManager = ComponentAccessor.getCommentManager()
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def isInternal = { Comment c ->
def commentProperty = commentPropertyService.getProperty(loggedInUser, c.id, SD_PUBLIC_COMMENT).getEntityProperty().getOrNull()
if (commentProperty) {
def props = new JsonSlurper().parseText(commentProperty.getValue())
props['internal']?.toBoolean()
} else {
null
}
}
if(!isInternal(comment)){
String commentBody = comment.getBody()
issueLinkManager.getOutwardLinks(issue.getId()).each {IssueLink issueLink ->
commentManager.create(issueLink.getDestinationObject(), loggedInUser, commentBody, false)
}

issueLinkManager.getInwardLinks(issue.getId()).each {IssueLink issueLink ->
commentManager.create(issueLink.getSourceObject(), loggedInUser, commentBody, false)
}
}
shrikant maheshwari July 22, 2021

@Peter-Dave Sheehan 

Thanks a lot peter, the script is working now.

But it is copying the comments in both the scenarios whether it is a public comment or Internal comment.

I want to only copy the public comments from one issue to its linked issue and not the internal comments, could you please help in achieving this.

TAGS
AUG Leaders

Atlassian Community Events