Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to Create a ScriptRunner Job to email when filter results = 0

Stephen Higgins May 1, 2020

I am trying to find a way to create a scheduled ScriptRunner job that will send a customized email notification to a JIRA group when the results from a shared filter return 0 rows.  I tried just using the native JIRA filter subscriptions, but there isn't an option to ONLY send when 0 results. Any help would be appreciated. 

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Lasse Langhorn
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.
May 1, 2020

Hi,

If you have Scriptrunner installed you can go to Jira Administration > Manage apps > Scriptrunner > Jobs > Click Create jobs > Click Custom  scheduled job.

  • Write a note
  • Choose the script user
  • Enter the cron expression that you want to use
  • Modify the script below to suit your needs (filterId, sendToEmail and jiraGroups)

Have not tried the script below but I think it will get you an idea.

Pseudo Groovy code below:

package dk.langhornweb

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

def filterId = 10L
def sendToEmail = "to-email@mail.com"
def jiraGroups = ["jira-software-users"]

def searchService = ComponentAccessor.getComponent(SearchService)
def jiraAuthContext = ComponentAccessor.getJiraAuthenticationContext()
def searchRequestManager = ComponentAccessor.getComponent(SearchRequestManager.class)
def searchRequest = searchRequestManager.getSearchRequestById(filterId)
def user = jiraAuthContext.getLoggedInUser()

def searchResult = searchService.search(user, searchRequest.query, PagerFilter.unlimitedFilter)
if (searchResult.issues.size() == 0) {
def bcc = buildBcc(sendToEmail, jiraGroups)
sendEmail(sendToEmail, bcc, "Jira filter size is 0", "This is the mail body")
}

// Build email bcc
def buildBcc(defaultEmail, groups) {
def bcc = defaultEmail
def groupManager = ComponentAccessor.getGroupManager()
def i = 0
while ( i < groups.size()) {
def members = groupManager.getUsersInGroup(groups[i])
def j = 0
while ( j < members.size()) {
bcc = bcc + "," + members[j].getEmailAddress()
j++
}
i++
}
return bcc
}

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

Regards

Lasse Langhorn

Stephen Higgins May 5, 2020

Thank you for your help Lasse. I am receiving the following errors when trying to setup the job as you have noted. Not sure if I am missing something fundamental here. 

emailScriptError01.JPG

Lasse Langhorn
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.
May 6, 2020

Hi @Stephen Higgins

I have created an improved version of the script.

package dk.langhornweb

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.mail.Email
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.mail.server.SMTPMailServer

def filterId = 10L
def sendToEmail = "to-email@mail.com"
def jiraGroups = ["jira-software-users"]

def searchService = ComponentAccessor.getComponent(SearchService)
def jiraAuthContext = ComponentAccessor.getJiraAuthenticationContext()
def searchRequestManager = ComponentAccessor.getComponent(SearchRequestManager.class)
def searchRequest = searchRequestManager.getSearchRequestById(filterId)
def user = jiraAuthContext.getLoggedInUser()

def searchResult = searchService.search(user, searchRequest.query, PagerFilter.unlimitedFilter)
if (searchResult.results.size() == 0) {
String bcc = buildBcc(sendToEmail, jiraGroups)
sendEmail(sendToEmail, bcc, "Jira filter size is 0", "This is the mail body")
}

// Build email bcc
def buildBcc(def defaultEmail, List<String> groups) {
def bccList = [defaultEmail]
def groupManager = ComponentAccessor.getGroupManager()
groups.each { group ->
Collection<ApplicationUser> memberList = groupManager.getUsersInGroup(group)
memberList.each { member ->
bccList.add(member.getEmailAddress())
}
}
return bccList.join(",")
}

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

Hope this will work.

Regards

Lasse Langhorn

Stephen Higgins May 8, 2020

@Lasse Langhorn it works like a charm! Thank you for your help on this item!

Lasse Langhorn
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.
May 9, 2020

Hi @Stephen Higgins

No problem.

Glad to help.

Regards

Lasse Langhorn

Ricardo Martinez August 31, 2022

Hi

Has anyone adapted this script to Jira Cloud?

 

Thanks in davance

0 votes
Ricardo Martinez August 31, 2022

Hi

Has anyone adapted this script to Jira Cloud?

 

Thanks in advance

TAGS
AUG Leaders

Atlassian Community Events