C/C++教程

[转]Jira ScriptRunner Send Custom Email

本文主要是介绍[转]Jira ScriptRunner Send Custom Email,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本文转自:https://library.adaptavist.com/entity/send-custom-email

Overview

This snippet shows you how to send a custom email. You can use the method inside in any script designed to send an email.

Example

I want to send an email when an issue transitions from In progress to Done to update the customer on their issue status. I can use this snippet inside a post-function script.

Good to Know

  • An outgoing mail server needs to be enabled.
  • An SMTP mail server needs to be configured
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.mail.Email
import com.atlassian.jira.mail.settings.MailSettings
import com.atlassian.mail.MailException
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.plugin.util.ContextClassLoaderSwitchingUtil
import org.apache.log4j.Level
import org.apache.log4j.Logger

String sendEmail(String emailAddr, String subject, String body) {
    def logger = Logger.getLogger(getClass())
    logger.setLevel(Level.DEBUG)

    // Stop emails being sent if the outgoing mail server gets disabled (useful if you start a script sending emails and need to stop it)
    def mailSettings = ComponentAccessor.getComponent(MailSettings)
    if (mailSettings?.send()?.disabled) {
        return 'Your outgoing mail server has been disabled'
    }

    def mailServer = ComponentAccessor.mailServerManager.defaultSMTPMailServer
    if (!mailServer) {
        logger.debug('Your mail server Object is Null, make sure to set the SMTP Mail Server Settings Correctly on your Server')
        return 'Failed to Send Mail. No SMTP Mail Server Defined'
    }

    def email = new Email(emailAddr)
    email.setMimeType('text/html')
    email.setSubject(subject)
    email.setBody(body)
    try {
        // This is needed to avoid the exception about IMAPProvider
        ContextClassLoaderSwitchingUtil.runInContext(SMTPMailServer.classLoader) {
            mailServer.send(email)
        }
        logger.debug('Mail sent')
        'Success'
    } catch (MailException e) {
        logger.debug("Send mail failed with error: ${e.message}")
        'Failed to Send Mail, Check Logs for error'
    }
}

sendEmail('user@email.com', '<Your subject here>', '<Your body here>')

 

 

https://library.adaptavist.com/entity/notify-approvers-of-pending-approvals-via-email

Overview

This escalation service is used to trigger an email that notifies approvers of pending issues, once their email has been retrieved using the REST Endpoint.

Example

In this example, a reminder email is triggered, after the email addresses of an issue’s outstanding approvers were obtained via a REST Endpoint.

Good to Know

    • For Jira versions prior to 8.10.0, the additional Thread parameters are not required to send the email as shown in the example below:
      def email = new Email(emailAddress)
      email.setSubject(subject)
      email.setBody(body)
      email.setMimeType('text/html')
      mailServer.send(email)
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.mail.Email
import groovy.json.JsonSlurper
import groovy.xml.MarkupBuilder

def key = issue.key

def applicationProperties = ComponentAccessor.applicationProperties
def mailServerManager = ComponentAccessor.mailServerManager
def mailServer = mailServerManager.defaultSMTPMailServer

final def host = applicationProperties.getString('jira.baseurl')
final def restEndpointName = 'getApprovers'

def baseUrl = "${host}/rest/scriptrunner/latest/custom/${restEndpointName}?issueKey=${key}"
def response = baseUrl.toURL().text

def json = new JsonSlurper().parseText(response)
def artifacts = json.collect()

def emailBody = new StringWriter()
def html = new MarkupBuilder(emailBody)
def mkp = html.mkp
html.html {
    body {
        p {
            mkp.yield 'Awaiting your Approval for this Issue'
            a(href: "${host}/browse/${key}", key.toString())
        }
    }
}

artifacts.findAll {
    def emailAddress = it.toString()
    def subject = 'Approval Reminder'
    def body = emailBody.toString()
    def email = new Email(emailAddress)
    email.setSubject(subject)
    email.setBody(body)
    email.setMimeType('text/html')
    def threadClassLoader = Thread.currentThread().contextClassLoader
    Thread.currentThread().contextClassLoader = mailServer.class.classLoader
    mailServer.send(email)
    Thread.currentThread().contextClassLoader = threadClassLoader
}

 

 

https://library.adaptavist.com/entity/send-an-issues-change-history-to-watchers

Overview

Send an email to all watchers of an issue with the change history items in the last X days. Edit the number of days to go back by changing the lastDays variable. Modify the script to set the jiraIssue variable depending on where you add the script (for example, in a post function use issue.key instead of TEST-1).

Example

As a project manager, I want to keep track of all changes to my watched issues. I need to be up to date with who is working on an issue and what has been changed. I do not want to check each watched issue manually. I can use this script to send me an email with change history items weekly to keep informed.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.mail.Email
import groovy.xml.MarkupBuilder
import java.time.LocalDate
import java.time.format.DateTimeFormatter

final jiraIssue = ComponentAccessor.issueManager.getIssueByCurrentKey("TEST-1")
final lastDays = 2

static sendEmail(String emailAddr, String subject, String body) {
    def mailServer = ComponentAccessor.mailServerManager
            .defaultSMTPMailServer
    if (mailServer) {
        def email = new Email(emailAddr)
        email.setMimeType("text/html")
        email.setSubject(subject)
        email.setBody(body)
        mailServer.send(email)
    }
}

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)
builder.html {
    head {
        h1("Hi,")
    }
    body {
        p("I have some changes you might have missed.")
        def changeHistory = ComponentAccessor.changeHistoryManager.getChangeHistories(jiraIssue)
        p("Here is what changed:")
        changeHistory.each {
            def now = LocalDate.now()
            def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.[SSS][SS][S]")
            def dateToParse = it.timePerformed as String
            def parsedDate = LocalDate.parse(dateToParse, formatter)
            if (parsedDate < now.minusDays(lastDays)) {
                return
            }
            def change
            it.changeItems.each {
                change = "${it.field} changed from ${it.oldstring ?: 'nothing'} to ${it.newstring ?: 'nothing'}\n\t"
            }
            p("Date: ${parsedDate} | Author: ${it.authorDisplayName} | ${change}")
        }
    }
}
def watchers = ComponentAccessor.watcherManager.getWatchersUnsorted(jiraIssue)
watchers.each {
    sendEmail(it.emailAddress, "Changes", writer.toString())
}

 

这篇关于[转]Jira ScriptRunner Send Custom Email的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!