Skip to main content
Example scripts
arrow icon
To homepage
Jira
Cloud icon
Cloud

Notify On Priority Change

Features
Listeners
Created 4 month(s) ago, Updated 2 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira
Language |
groovy
import groovy.xml.MarkupBuilder
import groovy.xml.XmlSlurper

def eventIssue = Issues.getByKey(issue.key as String)

Map priorityChange = changelog?.items.find { eventIssue.priority } as Map
if (!priorityChange) {
    logger.info("Priority was not updated")
    return;
}

def fromPriority = priorityChange.fromString as String
def toPriority = priorityChange.toString as String

logger.info("Priority changed from ${fromPriority} to ${toPriority}")

if (toPriority == "Highest") {
    def writer = new StringWriter()
    // Note that markup builder will result in static type errors as it is dynamically typed.
    // These can be safely ignored
    def markupBuilder = new MarkupBuilder(writer)
    markupBuilder.div {
        p {
            // update url below:
            a(href: "http://myjira.atlassian.net/issue/${issue.key}", issue.key)
            span(" has had priority changed from ${fromPriority} to ${toPriority}")
        }
        p("You're important so we thought you should know")
    }
    def htmlMessage = writer.toString()
    def textMessage = new XmlSlurper().parseText(htmlMessage).text()

    logger.info("Sending email notification for issue {}", issue.key)

    def recipients = [:]

    if (eventIssue.reporter != null) {
        recipients.reporter = true
    }
    if (eventIssue.assignee != null) {
        recipients.assignee = true
    }
    if (eventIssue.watches != null && eventIssue.votes != null) {
        recipients.watches = true
        recipients.votes = true
    }
    // If no recipients were added, log a message
    if (recipients.isEmpty()) {
        logger.info("No valid recipients found for issue {}", issue.key)
        return
    }
    // Add users/groups to ensure at least one recipient
    recipients.users = [[name: 'admin']]
    recipients.groups = [[name: 'jira-administrators']]

    def resp = post("/rest/api/2/issue/${issue.id}/notify")
            .header("Content-Type", "application/json")
            .body([
                    subject: 'Priority Increased',
                    textBody: textMessage,
                    htmlBody: htmlMessage,
                    to: recipients
            ])
            .asString()

    assert resp.status == 204
}
Having an issue with this script?
Report it here