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