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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

0 votes

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)
}
}

@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.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

🌐 BigPicture-Marketplace Apps Atlympics

Hello Atlympics, here a market place app that's a game changer given the features and excellent support.  Link to the tool:  Bigpicture project management ppm plugin How did yo...

18 views 0 2
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you