Get JIRA Service Desk internal comments in script runner

Deleted user April 9, 2015

Hello I am using script runner add-on and I have created a script listener to send a custom email when ever there is a comment on an issue.

I only want to send the email if the comment is a public comment (not internal). Below is my email template with an attempt that does not work (getRoleLevelId returns null always):

<html>
<body>
<%  def commentManager = componentManager.getCommentManager()
	def comments = commentManager.getComments(issue);
    
    // if the comment is internal dont print the reporter and request participants
    if(comments.last().getRoleLevelId() == 10101){
         out << "internal comment"
    }
	// else print the external customer who may have recieved the comments
	else {
		out << "People on the ticket who recieved an email of the comment:<br>"
		out << "To:<br>"
		out << "<b>" + issue.getReporterUser().getEmailAddress() + "</b><br>"
		out << "CCed:<br><b>"
		def cf = componentManager.getCustomFieldManager().getCustomFieldObjectByName("Request participants")
    	if(cf){
        
			def cfVal = issue.getCustomFieldValue(cf)
        	if(cfVal){
            
            	def size = cfVal.size()
            	if(size){
                
    				for( int i = 0; i < size; i++)
						out << cfVal.get(i).getEmailAddress() + "<br>"
            	}
        	}
    	}
	}
%>
</b><br>
Current Listed Company:<br> 
<b><% out << issue.getCustomFieldValue(componentManager.getCustomFieldManager().getCustomFieldObjectByName("Company")) %></b><br>
<br>Current Listed Location:<br>
<b><% out << issue.getCustomFieldValue(componentManager.getCustomFieldManager().getCustomFieldObjectByName("Location")) %></b><br>
<br>
Link to the ticket:<br> 
<a href=https://supportdesk.disti.com/browse/${issue.key}>${issue.key}</a><br> 
<br>   
    
<span style="font-size:26px"><b>Latest Comment:</b></span><br> 
<% if (lastComment)
    out << lastComment 
%>
    
<span style="font-size:26px"><b>Prior Comment:</b></span><br>
<% if (latestPriorComment)
    out << latestPriorComment 
%>
<span style="font-size:26px"><b>Description:</b></span><br>
<pre>${issue.description}</pre>
</body>
</html>

 

How can I tell if the last comment on an issue is internal or not?

2 answers

2 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 9, 2015

Service Desk uses entity properties for whether the comment is internal or not, amongst other things.

This is fortunate as it means we don't need to use the JSD API directly.

So you can use this code to to get whether any or each comment is internal or not:

import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import groovy.json.JsonSlurper

final SD_PUBLIC_COMMENT = "sd.public.comment"


def commentManager = ComponentAccessor.getCommentManager()
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)


def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
// get whether the comment is internal or not, null means no property found, maybe not an SD issue
def isInternal = { Comment comment ->
    def commentProperty = commentPropertyService.getProperty(user, comment.id, SD_PUBLIC_COMMENT).getEntityProperty().getOrNull()
    if (commentProperty) {
        def props = new JsonSlurper().parseText(commentProperty.getValue())
        props['internal']
    }
    else {
        null
    }
}


// Examples
log.debug "all external comments on ${issue.key}: " +  commentManager.getComments(issue).findAll { ! isInternal(it) }


commentManager.getComments(issue).each {
    log.debug("Comment on issue ${issue.key}, id: ${it.id}, internal: ${isInternal(it)}")
}

 

 

 

Deleted user April 9, 2015

Yes this is for Jira Service Desk. There are comments that can marked as internal which means only JIRA users can see them, not customers, but I do not know how to access the information through scripts. roleLevelId was just a guess at how Jira Service Desk internal comments work, but I dont think that is the correct way. I was expecting roleLevelId to contain information about the if the comment was "internal" or not.

Deleted user October 19, 2016

I'd like to use this solution, but in my implementation ScriptRunner says, that it's unable to resolve the class "com.atlassian.jira.bc.issue.comment.property.CommentPropertyService".
I think it's because of the experimental API, but every solution i tried failed.

Did anyone has got the same Problem or could help me to solve this?

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 19, 2016

You see that as a type checking error, but it should actually work if you let it execute... There is a known problem with the type checking and certain classes.

Deleted user October 24, 2016

My script crashes when it tries to execute the following line:

def commentProperty = commentPropertyService.getProperty(user, comment.id, SD_PUBLIC_COMMENT).getEntityProperty().getOrNull()

So it fails when I try to run a method from this class. The error looks like this:

Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script> groovy.lang.MissingPropertyException: No such property: commentPropertyService for class: com.custom.Script1543 at com.custom.Script1543.run(Script1543.groovy:77)

JIRA Version: 7.2.1 and ScriptRunner Version 4.3.9. Is it possible that it's not working because we run a test version?

Dmitry Fateev September 15, 2017

Jonas, it seems you forgot to import appropriate class and/or instantiate this class:

import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService;
...
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService.class)
congo bongo January 19, 2018

this is useless - i like to trigger a custom email too, whenever the comment made is NOT internal - however this groovy bogus seems to be way over the top... geeez

0 votes
Chris Knight July 27, 2015

Did you ever find an answer to this question? I'm trying to figure out the same thing.

Deleted user July 28, 2015

nope, the closest I got was in the REST API for the JSD, it will return some information on whether a particular comment is marked internal or not. I couldn't figure out a way to get the information in script runner. I guess you could do some kind of parsing from the resulting REST API output inside script runner, but gave up at that point

Chris Knight July 29, 2015

I got it working here: https://answers.atlassian.com/questions/22654312 Let me know and I'll share the code I'm using for the Condition.

Deleted user July 29, 2015

That's awesome! I was thinking of doing something like that. I'll try it out and mark this as the answer.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 25, 2015

you need to check the comment properties as in my answer.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 8, 2015

what's the condition you have come up with?

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 9, 2015

if you look at my code below it's not props[internal] - can you paste your full condition? What is the condition for, is the built-in script to send a custom email?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events