How to programmatically fire a generic event?

I know I can generate an issue-related event with IssueEventManager (IssueEventManager (Atlassian JIRA 7.7.1 API)).

But how can I define a non-issue related custom event and fire it programmatically?

What sort of events do you want to fire? You want to fire non-issue Jira events, or you want to create your own events for your own code and listen for them in your own code?

I have a ScriptRunner Job that’d need to send email. I also want to take advantage of using a Listener to send said email, because I can use a email template.

So to connect them, I’ll raise a custom event in the Job. But the Job is not related any issue, so I cannot use the IssueEventManager method.

I find the custom event types I can create in System | Events page are issue-related. How can I create a non-issue-related custom event via that page?

Or is there a way to use ScriptRunner’s email template programmatically? I don’t want to perform the substitution myself.

i would probably structure things in groovy instead. there is no extended non-issue system in jira.

./com/mycompany/mail/SendEmail.groovy
package com.mycompany.mail

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.mail.queue.SingleMailQueueItem
import com.atlassian.jira.mail.Email
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMultipart
import groovy.xml.MarkupBuilder

class SendEmail {
    def mailQueue = ComponentAccessor.mailQueue
    
    void send(def from, def to, def subject, def contextObject) {
        def writer = new StringWriter()
        def html = new MarkupBuilder(writer)

        html.table {
            tr {
              td 'Title'
              td contextObject.title
            }
            tr {
              td 'Description'
              td contextObject.description
            }
        }

        def email = new Email(to)

        email.setFrom from
        email.setReplyTo from
        email.setSubject subject

        def textBody = new MimeBodyPart()
        textBody.setContent writer.toString()

        def mailMultipart = new MimeMultipart()
        mailMultipart.addBodyPart textBody

        email.setMultipart mailMultipart
        mailQueue.addItem(new SingleMailQueueItem(email))
    }
}
./com/mycompany/job/MyScheduledJob.groovy
import com.mycompany.mail.SendEmail

def context = [
    title:'Job', 
    description:'This was sent from a Scheduled Job'
]
def sendEmail = new SendEmail()

sendEmail.send('no-reply@example.com', 'example@example.com', 'Sent from a Job', context)
sendEmail.send('no-reply@example.com', 'another@example.com', 'Sent from a Job', context)

./com/mycompany/listener/MyEventListener.groovy
import com.mycompany.mail.SendEmail

def context = [
    title:'Listener', 
    description:'This was sent from an Event Listener'
]
def sendEmail = new SendEmail()

sendEmail.send('no-reply@example.com', 'example@example.com', 'Sent from a Listener', context)
sendEmail.send('no-reply@example.com', 'another@example.com', 'Sent from a Listener', context)
1 Like

Thanks for the tip about Groovy’s MarkupBuilder, didn’t know about it.

1 Like