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

YaseenR August 23, 2017

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

4 answers

0 votes
Ufuk Uysal August 12, 2020

hi Joshua Yamdogo @ Adaptavist ,

Nice script for internal comments, many thanks. How can we change this script to Reporter?

 

 

 

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
Tomáš Vrabec August 13, 2020

What do you mean by "change to Reporter"? What you are expecting to be done by script? Maybe I can help, but have no idea what you are asking for. 

Ufuk Uysal August 21, 2020

@Tomáš Vrabec 

I am creating a clone of the record opened by the customer via the service desk into a different project.
If the customer comments on the clone I created, I want to send a warning e-mail to the assigned person via scriptrunner.

0 votes
Tomáš Vrabec June 21, 2019

Hi guys.

I ran into similar problem with one of my customer. Basically we got this problem https://jira.atlassian.com/browse/JSDSERVER-3410 and we are trying to prepare few PoC how to solve it.

One of them is disabled notification scheme + custom listener, which will trigger a notifications for:

  • Agent
  • Customer
  • Related users

And also will be capable to:

  • Distinguish between Internal and Public comment and behave appropriately

We were able to implement above mentioned code, but struggling with last point. Anyone could help with listener? Will give you credential recognition in article I will publish afterward. :-D 

0 votes
gustavo.pereira April 12, 2019

Hi Joshua Yamdogo @ Adaptavist 

Great this script works well for me. But one question: is there a way to ignore a comment when it has a pasted image?

0 votes
Joshua Yamdogo @ Adaptavist
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 28, 2017

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

Software Licenses September 7, 2017

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

Joshua Yamdogo @ Adaptavist
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 7, 2017

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?

Software Licenses September 7, 2017

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

Joshua Yamdogo @ Adaptavist
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 7, 2017

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
Like Ufuk Uysal likes this
Software Licenses September 7, 2017

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

Joshua Yamdogo @ Adaptavist
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 7, 2017

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()
Software Licenses September 8, 2017

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

Software Licenses September 12, 2017

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
TAGS
AUG Leaders

Atlassian Community Events