Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Help write event listener for "release date" in ScriptRunner

Pete P
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.
June 14, 2018

I want to receive an email when a version "Release Date" is added or changed.  I found an example for "user" added that should work in a similar fashion.  Can anyone let me know what I need to modify this to suit my application?

 

import com.atlassian.crowd.event.user.UserCreatedEvent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.mail.Email
import com.atlassian.mail.queue.SingleMailQueueItem

def event = event as UserCreatedEvent

def email = new Email("someuser@example.com")
email.setSubject("User created")
email.setBody("User: ${event.user.displayName} created...")
// email.setMimeType("text/html") // for messages with an html body

SingleMailQueueItem item = new SingleMailQueueItem(email)
ComponentAccessor.getMailQueue().addItem(item)

2 answers

1 accepted

1 vote
Answer accepted
Mark Markov
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.
June 14, 2018

Hello @Pete P

Like this


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.mail.Email
import com.atlassian.mail.queue.SingleMailQueueItem

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Release Date"}

if (change) {
log.debug "Value changed from ${change.oldstring} to ${change.newstring}"
// your actions if the field has changed
def email = new Email("someuser@example.com")
email.setSubject("Release date updated")
email.setBody("New release date: ${change.newstring}")
// email.setMimeType("text/html") // for messages with an html body

SingleMailQueueItem item = new SingleMailQueueItem(email)
ComponentAccessor.getMailQueue().addItem(item)
}

Listener must associated with Issue Update event

Pete P
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.
June 14, 2018

Thanks for quick work Mark!  Will this also trigger when new version "Release Date" is added?  I will test it out now!

Mark Markov
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.
June 14, 2018

This should be triggered by any changes in Release Date added or changed

Pete P
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.
June 14, 2018

This is what I have so far, but it is not doing anything.  Not sure if I have all the "Events" specified I need

 

Capture.PNG

Pete P
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.
June 14, 2018

I am not sure if I need to do any admin configurations to get the emails sending or not.  

Pete P
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.
June 15, 2018

@Mark Markov, were you able to test this in your environment?  I don't get any errors when adding the script however I don't get emails when the version release date is changed or a new version with release date is added.

Pete P
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.
June 15, 2018

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.event.project.VersionCreateEvent.getChangeLog() is applicable for argument types: () values: []
 at Script8.run(Script8.groovy:5)

Pete P
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.
June 15, 2018

I have also associated the listener with Issue Update event, I didn't notice that earlier.

Mark Markov
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.
June 15, 2018

Sorry @Pete P, I'm misuderstood you from the start, think that need some listener for custom field update, not Version Releases. Ok. 

For your case you need two events VersionCreateEvent and VersionUpdateEvent

and here the script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.project.VersionCreateEvent
import com.atlassian.jira.event.project.VersionUpdatedEvent
import com.atlassian.mail.Email
import com.atlassian.mail.queue.SingleMailQueueItem

def email = new Email("some@example.com")

if (event instanceof VersionUpdatedEvent){
def event = event as VersionUpdatedEvent
email.setSubject("Version updated")
email.setBody("Version release date: ${event.version.releaseDate}")
// email.setMimeType("text/html") // for messages with an html body
SingleMailQueueItem item = new SingleMailQueueItem(email)
ComponentAccessor.getMailQueue().addItem(item)
}
else if (event instanceof VersionCreateEvent){
def event = event as VersionCreateEvent
email.setSubject("Version created")
email.setBody("Version release date: ${event.version.releaseDate}")
// email.setMimeType("text/html") // for messages with an html body
SingleMailQueueItem item = new SingleMailQueueItem(email)
ComponentAccessor.getMailQueue().addItem(item)
}

 I've tested it on my environment and it works! Hope this help you.

Like Claus Conrad likes this
Pete P
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.
June 15, 2018

Thank you @Mark Markov, it works perfectly :)

Deleted user March 10, 2021

I scoured the internet to find this critical piece of information.

if (event instanceof VersionUpdatedEvent){
def event = event as VersionUpdatedEvent

having the event recasted to a VersionUpdatedEvent allowed me to then use "getOriginalVersion" which i needed in order to determine what values changed.

THANK YOU! 

Pete P
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.
March 11, 2021

@[deleted] , that sounds very interesting.  Please share your code.  Are you able to determine which project the version was associated to as well as the individual who made the change?

Currently I get a generic email that a version was added or updated and I need to check my dashboard to try and figure out which one.

Deleted user March 11, 2021

The relevance of this is that when the underlying name of the Release (aka Version) changes, we want any issues that have it in the Affected Version/s or Fix Version/s to to get updated so a downstream system knows to re-pull their info.  There are also some custom field values we update if the Start date or Release date for a Release changes.

I'm sure you can get to the project info somewhere within all the objects this is accessing but I don't really need that info.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.project.VersionUpdatedEvent
import com.atlassian.jira.event.project.VersionDeleteEvent
import com.atlassian.jira.issue.comments.CommentManager

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def customFieldManager = ComponentAccessor.getCustomFieldManager()
Collection fixStartDateCF = customFieldManager.getCustomFieldObjectsByName( "Start date (Fix)" )
Collection fixReleaseDateCF = customFieldManager.getCustomFieldObjectsByName( "Release date (Fix)" )
Collection affectsStartDateCF = customFieldManager.getCustomFieldObjectsByName( "Start date (Affects)" )
Collection affectsReleaseDateCF = customFieldManager.getCustomFieldObjectsByName( "Release date (Affects)" )
CustomField fixStartDate = fixStartDateCF[0]
CustomField fixReleaseDate = fixReleaseDateCF[0]
CustomField affectsStartDate = affectsStartDateCF[0]
CustomField affectsReleaseDate = affectsReleaseDateCF[0]

if ( event instanceof VersionDeleteEvent )
{
def version = event.getVersion()

log.warn( "version deleted ${version.name}, ${version.id}")
}
else
{


def version = event.getVersion()
def String eventVersionName = version.name
log.warn "event for ${eventVersionName} with id ${version.id}"

// To see if the name of the version changed we have to convert the
// event to the proper type in order to use the getOriginalVersion method
def updateEvent = event as VersionUpdatedEvent
def prevVersionName = updateEvent.getOriginalVersion().getName()
def boolean nameChanged = false
def String nameChangedComment
if ( eventVersionName != prevVersionName )
{
nameChanged = true
nameChangedComment = "An associated Release name changed from '${prevVersionName}' to '${eventVersionName}' [This is an automatically generated comment]"
log.warn "Name changed from ${prevVersionName} to ${eventVersionName}"
}

// Create a query to get all the issues having this version
def query = jqlQueryParser.parseQuery("fixVersion = ${version.id} or affectedVersion = ${version.id}")

def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

log.warn("Total issues: ${search.total}")

search.results.each { documentIssue ->
def issue = issueManager.getIssueObject(documentIssue.id)
def String issueFixVersion = issue.fixVersions[0]
def String issueAffectedVersion = issue.affectedVersions[0]
def updateFlag = false

log.warn(issue.key + " " + issue.summary + " " + issueFixVersion + " " + issueAffectedVersion )

log.warn("comparing |${issueFixVersion}| with |${eventVersionName}|")
if ( eventVersionName == issueFixVersion )
{
log.warn "need to update fix version dates"
issue.setCustomFieldValue( fixStartDate, version.startDate )
issue.setCustomFieldValue( fixReleaseDate, version.releaseDate )
updateFlag = true
}
else
{
log.warn "no need to update fix version dates"
}

log.warn("comparing |${issueAffectedVersion}| with |${eventVersionName}|")
if ( eventVersionName == issueAffectedVersion )
{
log.warn "need to update affects version dates"
issue.setCustomFieldValue( affectsStartDate, version.startDate )
issue.setCustomFieldValue( affectsReleaseDate, version.releaseDate )
updateFlag = true
}
else
{
log.warn "no need to update affects version dates"
}

if ( nameChanged )
{
CommentManager commentManager = ComponentAccessor.getCommentManager()
commentManager.create( issue, user, nameChangedComment, true )
}

if ( updateFlag )
{
issueManager.updateIssue( user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

if ( nameChanged || updateFlag )
{
issueIndexingService.reIndexIssueObjects([issue])
}
}
}

Pete P
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.
March 11, 2021

Perfect, thank you @[deleted]  for posting this.  The template is very useful and obviously a lot of work went into it 👍😃

0 votes
Alexey Matveev
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.
June 14, 2018

Hello,

Go to add-ons->ScriptRunner-> Script Listeners and create the Send a custom email listener. It would look like this:

Untitled.png

 

Pete P
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.
June 14, 2018

ah, VersionReleaseEvent.  I'll add that as well

Pete P
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.
June 14, 2018

did not help, still not working.  

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events