Using Jira Script Runner's Escalation Service to send an email with aggregate issue information

Bryan Karsh August 20, 2018

 

Hi,

 

I have a script runner script that works fine in the script console. I am trying to get it to work on a schedule -- seems like the escalation service is the way to go. However -- it looks like the escalation service is set up to action on a single issue at a time, and not in aggregation. For example, I want to send a single email to each assignee -- and each of those assignees can have differing numbers of issues assigned to them. I find I am able to work around this by having the jql in the escalation service simply look for a single issue, and then run the real jql and logic in my script -- but this seems hacky. So in short, I am looking for the following tips:

1. A way for Script runner to run against jql on a schedule, and aggregate the results before sending them out to the associated assignees (one email per assignee).

2. Ideally a way to make the script handle multiple jql queries and email formats. Tips on configuration files and ways to slurp them in would be appreciated.  For example, the request I am working on has 4 or 5 different jql queries with different language and scheduling for each. I'd rather not cut and paste the same script again and again and have the query and email body be hard-coded in the script itself.

3. Related to #2 -- is there any suggestions on having a single escalation rule that say... runs every day -- and based on the jql and scheduling rules, will only fire emails applying to a rule that matches. For example -- let's say one jql rule runs every 2nd tuesday at 8:00am.  But another one runs daily at 3:00. It seems to me the configuratiaon file for each rule would have a section for cron scheduling, and the escalation rule should be smart enough to run every day or whatever, and compare the current time with the cron for each jql that I want to process. Make sense?

4. Also -- if there is a better tool to do this, I'm all ears. I just have this groovy snippet that works well.. so I was hoping not to have to recreate the wheel too much with say an external rest client.

Tips / Constructive criticism much appreciated and welcome. Thanks!

 

 


import com.atlassian.mail.Email
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter

def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlSearch = "project = foobar AND Updated < -2w AND resolution is empty AND status != Backlog ORDER BY status ASC"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// eventually this array will contain all the issues that returned from the JQL
def issues = []

// hashmap to store username and associated issue(s)


def map = [:].withDefault {
[]
} //The default value will be created every time a non-existing key is accessed.
// Need this to be able to dynamically expand/update map


SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues.collect { issueManager.getIssueObject(it.id) }
}


// We are breaking out the issues per assignee. Will use later when we email the assignees
issues.each {
def assignee = it.assigneeId

map[assignee].add(it)

}

// following section creates separate emails for each assignee from the map, including all related issues in the same email.

map.each { key, value ->
def assignee = key
def theirIssues = value as List


def assigneeEmail = ""
String cc = "someguy@somedomain.com"
String bcc = ""

def body = "The following issues not been updated in over two weeks. " +
"Please review and update your assigned issues with comments, updated status and (if available) a due date." +
"<br/><br/>" +
"<table>\n" +
" <tr>\n" +
" <th>Issuekey</th>\n" +
" <th>Summary</th>\n" +
" <th>Assignee</th>\n" +
" </tr>"



theirIssues.each {

assigneeEmail = it.assignee.emailAddress

body += " <tr>\n" +
" <td><a href=\"https://somejirainstance.com/browse/${it.key}\">${it.key} </a></td>\n" +
" <td>${it.getSummary()}</td>\n" +
" <td>${it.assigneeId}</td>\n" +
" </tr>"
}

body += "</table>\n" +
"\n" +
"<br></br>\n" +
"Regards, <br></br>\n" +
"\n" +
"Jira Automation"


sendEmail(assigneeEmail, cc, bcc, "The following issues need your assistance", body)


}

// Create an email
def sendEmail(String emailAddr, String cc, String bcc, String subject, String body ) {
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
def email = new Email(emailAddr)
email.setMimeType("text/html")
email.setSubject(subject)
email.setBody(body)
email.setCc(cc)
email.setBcc(bcc)
mailServer.send(email)
log.debug("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}


Hi

3 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.
August 21, 2018

Hello @Bryan Karsh

You dont need escalation services for that. If your script works correctly, use normal services that will run your script on schedule

https://scriptrunner.adaptavist.com/5.4.12/jira/services.html

Bryan Karsh August 21, 2018

Great -- thanks Mark. I'll try that.

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.
August 22, 2018

You re welcome! Let me know if something goes wrong :)

Bryan Karsh September 11, 2018

Worked like a charm! :)

Krishnanand Nayak
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.
December 20, 2018

@Mark Markov where would be a location to dump the file? This is how our structure looks like:

 Capture.PNG

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 4, 2019

Scriptrunner scripts go in [JIRA-HOME]/scripts ... your screenshot shows the [JIRA-INSTALL] directory.

0 votes
InterWorksS April 4, 2022

hI All,

 

Ho to add multiple users in cc, i tried this but only plemail is added in cc.

 

//Send email to notify HR and Marketing
def email=new Email(accounting)
email.setSubject("New Sponsorship Request has been approved!")
email.setCc(hrEmail)
email.setCc(marketingEmail)
email.setCc(plEmail)
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 4, 2019

I don't think you even need a script for that. Why not just use Filters and subscriptions?

For example, create a filter for issues not updated in 2 weeks and save it.

project = foobar  AND 
Updated < -2w AND
resolution is empty AND
status != Backlog AND
assignee = currentUser()
ORDER BY status ASC

Note the assignee=currentUser(). When you create a subscription and assign the subscription to your entire team (using a jira group), jira will, at the interval specified, evaluate that JQL for each user in the group and send them an email with the relevant results in a table.

  • Use the description field in the filter sharing screen to specify the message that will appear above the table. 
  • Specify and save filter-specific columns to control the columns displayed in the email
  • The saved filter's name will appear in the email subject.

Each saved filter can have different JQL, intervals and descriptions.

Bryan Karsh April 5, 2019

This would work too of course -- the only reason I went the script route was that the customer wanted custom language in the email instead of the boilerplate subscription mail.  This is what I had suggested to them at first. :)

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 5, 2019

I'm a big fan and use lots of custom emails using similar scripts.
But I thought some readers could benefit from the clarification.

Also, the boilerplate language can still be specified using the filter description (hidden with permission screen).

Like Bryan Karsh likes this

Suggest an answer

Log in or Sign up to answer