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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


Scriptrunner JQL Search Cache



I am attempting to create a script to verify the health of our email system. We have a number of mail handlers and so I am creating a Scriptrunner job to automate the following health check process.

Send email -> Wait -> Search for issue -> Delete Issue

The search for issue is a loop that runs a jql every X seconds for X minutes. If the issue never arrives then an error is logged.


The Problem: The JQL query never seems to find the issue. When I search in the UI, I can find the issue. When I create a search to find an issue that is already in the system, it will find and delete the issue. However it seems the script cannot find issues that are created after the script has started. What can I do?


// Import required classes
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.Email;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.DeleteValidationResult
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import java.time.LocalDateTime;
import java.sql.Timestamp;
import org.apache.log4j.Logger
import org.apache.log4j.Level

// Script Configuration
final int retrySeconds = 30
final int retryAttempts = 20

// Classes
class Mailbox {
String emailAddress;
String project;
long emailSent;
long emailFound;

// Methods
def sendEmail(String emailAddr, String subject, String body){
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()

if (mailServer){
Email email = new Email(emailAddr)

final Long testTime = Timestamp.valueOf(
String subjectKey = "Automated Test: ${testTime}"
List<Mailbox> mailboxes = []

Mailbox newMailbox = new Mailbox(emailAddress: "INSERT_EMAIL_HERE", project: "INSERT_PROJECT_KEY")

mailboxes += newMailbox"Automated Email Health: Beginning Health Checks on ${mailboxes.size()} with key ${testTime}")

mailboxes.each{ mailbox ->
mailbox.emailSent = testTime
sendEmail(mailbox.emailAddress, subjectKey, "This is an automated test message.")
log.debug("Automated Email Health: Sending email to ${mailbox.emailAddress} with key ${testTime}")

IssueService issueService = ComponentAccessor.getIssueService()
SearchService searchService = ComponentAccessor.getComponent(SearchService)
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

boolean success = false
int retries = 0
List<Mailbox> failures = []

while (!success && retries < retryAttempts){
Thread.sleep(retrySeconds * 1000)
success = true
failures = []

log.debug("Automated Email Health: Beginning attempt ${retries}")

mailboxes.each{ mailbox ->
if (!mailbox.emailFound){
def query = jqlQueryParser.parseQuery("project = '${mailbox.project}' AND creator = shadmin AND created >= -30m AND summary ~ '${subjectKey}'")
def results =, query, PagerFilter.getUnlimitedFilter())
if ( results.getTotal() < 1 ){
success = false;
failures += mailbox
log.debug("Automated Email Health: Did not find issue for project ${mailbox.project} on attempt ${retries}")
}else if ( results.getTotal() == 1 ){
mailbox.emailFound = Timestamp.valueOf(
def documentIssue = results.getResults()[0]
DeleteValidationResult deleteValidationResult = issueService.validateDelete(user,
if (deleteValidationResult.isValid()){
log.debug("Automated Email Health: Deleting issue ${documentIssue.key}")
ErrorCollection deleteResult = issueService.delete(user, deleteValidationResult);
if (deleteResult.hasAnyErrors()){
log.error("Automated Email Health: Errors while trying to delete issue.")
log.error("Automated Email Health: Unable to delete issue. Reason: ${deleteValidationResult.getErrorCollection()}")
log.error("Automated Email Health: More than one issue found matching '${query}' this could mean that the mail handler is duplicating emails")

if (!success){
log.error("Automated Email Health: Failed to retrieve emails for one or more mailboxes")
failures.each{ mailbox ->
log.error("Automated Email Health: Unable to retrieve email for ${mailbox.project} from ${mailbox.emailAddress}")

int completionTime = (int) ((Timestamp.valueOf( - testTime) / 60)"Automated Email Health: Completed Health Checks in ${completionTime} seconds")

return "Success: ${success} in ${completionTime} seconds"


1 answer

Suggest an answer

Log in or Sign up to answer

if you use

log.error "project = '${mailbox.project}' AND creator = shadmin AND created >= -30m AND summary ~ '${subjectKey}'"

 before search result. and copy in jql search in ui is it correct search?

AUG Leaders

Atlassian Community Events