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)
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
Thanks for quick work Mark! Will this also trigger when new version "Release Date" is added? I will test it out now!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This should be triggered by any changes in Release Date added or changed
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is what I have so far, but it is not doing anything. Not sure if I have all the "Events" specified I need
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am not sure if I need to do any admin configurations to get the emails sending or not.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have also associated the listener with Issue Update event, I didn't notice that earlier.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you @Mark Markov, it works perfectly :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@[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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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])
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Perfect, thank you @[deleted] for posting this. The template is very useful and obviously a lot of work went into it 👍😃
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
Go to add-ons->ScriptRunner-> Script Listeners and create the Send a custom email listener. It would look like this:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
ah, VersionReleaseEvent. I'll add that as well
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
did not help, still not working.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.