You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
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.
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
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)
}
}
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.
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.