ScriptRunner listener - Send a custom email Condition & Configuration

Chandana August 2, 2021

Hi,

I am trying to create a listener that sends an email only when a given 'custom field value is not null' and any one of the following 3 conditions is true:

  1. Priority - When Priority of an issue is updated
  2. Status - When an issue is transitioned to another status
  3. Comments -When new comments are added or existing comments are edited or deleted.

ScriptRunner Condition & Configuration: 

issue.projectObject.key == 'XXXX' && issue.issueType.name in ['Story', 'Incident'] && cfValues['customfieldname'] != null &&

(changeItems.any { it.get('field')=='priority'} == true) ||  (changeItems.any { it.get('field') == 'status' } == true)||-- Code is working until here.

(changeItems.any { it.get('field') == 'comments' } == true)-- This is not yielding the correct results.

 

From the above code, I am able to successfully verify if the custom field value is not null and if there is a change in the priority field or status change for a given story or Incident.

However, unable to do the verify the changes for 'Comments'.

 

Any ideas on how to achieve this?

 

Thanks in advance!

 

Scriptrunner Condition & Configuration.JPG

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
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 2, 2021

I find that it's helpful to break conditions down rather than sting them all together.

Try something like this:

import com.atlassian.jira.event.comment.CommentCreatedEvent
import com.atlassian.jira.event.comment.CommentUpdatedEvent

def customFieldNameHasValue = cfValues['customfieldname'] != null
def priorityIsChanged = changeItems.any { it.get('field')=='priority'}
def statusIsChanged= changeItems.any { it.get('field')=='status} 
def commentIsAdded = event instanceof CommentCreatedEvent
def commentIsUpdated = event instanceof CommentUpdatedEvent

log.info "customFieldNameHasValue=$customFieldNameHasValue"
log.info "priorityIsChanged=$priorityIsChanged"
log.info "statusIsChanged=$statusIsChanged"
log.info "commentIsAdded=$commentIsAdded"
log.info "commentIsUpdated=$commentIsUpdated"

return customFieldNameHasValue && (priorityIsChanged || commentIsAdded || commentIsUpdated )

I'm not 100% sure on the commentIsAdded, but you should try it.

Chandana August 2, 2021

Thank you for your response! For Status, I figured out that below code works. I tried the comments part from your code and unfortunately, it does not work.

import com.atlassian.jira.component.ComponentAccessor

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

changeHistoryManager.getAllChangeItems(issue).find {

    it.field == "status" }

(changeItems.any { it.get('field')=='status'} == true)

Peter-Dave Sheehan
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 2, 2021

Which events did you configure your Scripted Listener to react to?

Chandana August 2, 2021

'All Issue Events' (Attached screenshot on the question for your reference!)

Peter-Dave Sheehan
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 2, 2021

You could try:

def customFieldNameHasValue = cfValues['customfieldname'] != null
def priorityIsChanged = changeItems.any { it.get('field')=='priority'}
def statusIsChanged= changeItems.any { it.get('field')=='status} 
def commentIsPresent = (event.comment)

log.info "customFieldNameHasValue=$customFieldNameHasValue"
log.info "priorityIsChanged=$priorityIsChanged"
log.info "statusIsChanged=$statusIsChanged"
log.info "commentIsPresent =$commentIsPresent
return customFieldNameHasValue && (priorityIsChanged || statusIsChanged || commentIsPresent )
Chandana August 2, 2021

Thank you again for your response! Tried below and still no luck with the comments.

def commentIsPresent = (event.comment)

  

Peter-Dave Sheehan
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 3, 2021

I found some typos in the last script I suggested.

Please try this again and report back with the content of the log for the execution that did not work.

def customFieldNameHasValue = cfValues['customfieldname'] != null
def priorityIsChanged = changeItems.any { it.get('field')=='priority'}
def statusIsChanged= changeItems.any { it.get('field')=='status'}
def commentIsPresent = (event.comment).asBoolean()

log.setLevel(org.apache.log4j.Level.INFO)
log.info "customFieldNameHasValue=$customFieldNameHasValue"
log.info "priorityIsChanged=$priorityIsChanged"
log.info "statusIsChanged=$statusIsChanged"
log.info "commentIsPresent =$commentIsPresent"

return customFieldNameHasValue && (priorityIsChanged || statusIsChanged || commentIsPresent )
Chandana August 11, 2021

Thank you! I tried this one too and no luck on the comments.

So, I figured out an alternative solution:

I created a separate listener for triggering email and picked 'Comment added' and 'Issue comment edited' events from the list of available events. This seems to be doing the work.

TAGS
AUG Leaders

Atlassian Community Events