Script Runner - Event Listeners - Check if a comment is internal and reporter is part of a group

Hi , 

 

I am trying to create a script listener to get around this : Internal Comments not notifying Agents - https://jira.atlassian.com/browse/JSDSERVER-2418

 

The reason we need this is that we have a team that is on the road most of the day and they would like to keep a track of what happens on their tickets via mail.

 

I have got the first part of the script working where emails are sent to Agents but I need to only fire off the event if the last comment is Internal

import com.atlassian.jira.component.ComponentAccessor
def groupManager = ComponentAccessor.getGroupManager()
     groupManager.isUserInGroup(issue.reporter, 'jira-servicedesk-users')

 

Thanks in advance 

Yas

1 answer

Hi Yaseen,

We have an example in our documentation about checking to see if a comment is internal or not. Could you take a look at it and see if it is of any help?

https://scriptrunner.adaptavist.com/latest/jira/recipes/misc/jira-service-desk.html#_from_an_event_listener

Hi Joshua , 

 

Thank you so much for this, Its the right road.

 

Unfortunately, i am getting this error:

Cannot invoke method getBindings() on null object.

I cant seem to attach the full error but I have provided a screen grab Internal Comments error.JPG

 

 

Thank you so much again.

 

Yas

Hi,

Could you post the exact script you are using in your listener?

Let me see if I understand your use-case:

Problem: Internal comments do not notify agents

Solution:

  1. Create a listener to watch for comments
  2. If the last comment is internal, send an email to the agents. If we the last comment is NOT internal, do not send anything.

Does that sound correct?

Hi Joshua,

 

Absolutely correct - 

  1. Create a listener to watch for comments
  2. If the last comment is internal, send an email to the agents. If we the last comment is NOT internal, do not send anything.

I am using this script :

import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService 
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.comments.Comment
import groovy.json.JsonSlurper
 
final SD_PUBLIC_COMMENT = "sd.public.comment" 
def event = event as IssueEvent
def user = event.getUser()
def comment = event.getComment()
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def isInternal = { Comment c -> def commentProperty = commentPropertyService.getProperty(user, c.id, SD_PUBLIC_COMMENT) .getEntityProperty().getOrNull()

if (commentProperty) {
def props = new JsonSlurper().parseText(commentProperty.getValue())
(props['internal'] as String).toBoolean()
}
else { null }
}
if (comment) {
return isInternal(comment) }
return false

Hi,

I have tested the script and it works for me with 100% success. It looks like you have some formatting issues in that script you posted. Is that how the script shows up in your inline script editor? For instance, you have two import statements on the same line in that script.

Could you perhaps try copying and pasting this script and replacing the script you were using?

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

final SD_PUBLIC_COMMENT = "sd.public.comment"

def event = event as IssueEvent
def user = event.getUser()
def comment = event.getComment()
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)

def isInternal = { Comment c ->
def commentProperty = commentPropertyService.getProperty(user, c.id, SD_PUBLIC_COMMENT)
.getEntityProperty().getOrNull()

if (commentProperty) {
def props = new JsonSlurper().parseText(commentProperty.getValue())
props['internal'].toBoolean()
}
else {
null
}
}

if (comment) {
log.debug(comment)
return isInternal(comment)
}
return false

Hi Joshua,

 

Its all one script , It was just split for some reason in the post.

 

I am quite sure I am doing something wrong , I have attached a .gif of the steps I take to get there.

 

I cant thank you enough for helping out here.

 

Regards

YasInternal comments Jira.gif

Hi Yas,

After setting up a script listener that sends a custom email, I was able to reproduce this issue. I can see that you will get the "unable to invoke method getBindings()" error.

I think that the error you're receiving has to soley do with the "Preview Issue Key" functionality, not the script itself. I can't quite remember what the issue is with "Preview Issue Key", but I know that it does not work under certain circumstances.

Instead of trying to preview an issue, I would just try to test it on an actual issue. Make an internal comment and see if the listener works. You might edit the listener temporarily to send the email to your address only, so your agents don't get any test emails.

I have tested this script with my own mail server and can see that it does work:

Screen Shot 2017-09-07 at 11.35.12 AM.pngI also see in your picture that you have changed the script to:

(props['internal'] as String).toBoolean()

I would not bother doing that. It's a waste of processing power to do that casting. 

This will give you a static type checking error in the inline editor, but the script will still work.

props['internal'].toBoolean()

Thanks so much Joshua, I will test this over the weekend.

Hi Joshua,

 

The code worked perfectly.

Thank you so much, you have been really awesome helping to get this done.

 

Kind Regards

Yaya

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

181 views 0 5
Read article

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