Get JIRA Service Desk internal comments in script runner

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

1 accepted

This widget could not be displayed.

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

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

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.

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

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

what's the condition you have come up with?

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?

This widget could not be displayed.

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

 

 

 

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.

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?

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.

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?

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)

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

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in Teamwork

What teamwork quotes inspire you?

Hey everyone! My name is Natalie and I'm an editor of the Atlassian Blog and I've got a question for you: What's your favorite quote about teamwork?  We've compiled a list here, along with...

176 views 18 7
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you