Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Sanity check for script runner service that fixes issues with missing SLA

Hi guys,


Our Jira instance gets a lot of webhook traffic and heavily integrates with several external tools. Not sure exactly why, but occasionally some issues that should get SLAs don't. Race condition maybe? Guessing about 5-10 per day out of several thousand issues created. Anyway, this service (see below) uses script runner which in turn calls the rest api for forcing an SLA recalculation on these issues. It seems to work great. I have it run every five minutes.

While this works, I wanted someone to look it over, and suggest ways it could be better.  I couldn't find an internal api for forcing an sla recalculation for example. Also there's an area that is rather ugly where I massage the data returned from Script Runner so that it works with the rest api body. Any suggestions to improve would be appreciated.

Otherwise, hopefully this will help folks who have a similar edge case.


import com.atlassian.mail.Email
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import groovy.json.JsonOutput
import static*
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("MissingSLARecalculationService")

def serviceUser = "automation-user" // who the service runs as

def to = "" // who gets emails if any issues with missing SLAs are repaired.
String cc = ""
String bcc = ""

try {

def issueManager = ComponentAccessor.getIssueManager()

def searchService = ComponentAccessor.getComponent(SearchService)

def jqlSearch = "<whatever query to isolate issues to reprocess>"

def userUtil = ComponentAccessor.getUserUtil()

def user = userUtil.getUserByKey(serviceUser)
assert user // can't find the user specified

// array to contain issues returned from JQL
def issues = []

SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)

if (parseResult.isValid()) {

def searchResult =, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())

issues = searchResult.issues.collect { issueManager.getIssueObject( }

/* Need to refactor this bit -- it's ugly.
Basically needed to get what's returned from jql in script-runner into json format. Kept running into variations of this error:
"Can not deserialize instance of java.util.ArrayList out of VALUE_STRING'. But what's here works at least.

if (issues.size() >= 1) {

def issues2 = issues.toListString()

def issues3 = issues2[1..-2].split(', ')

def jsonList = JsonOutput.toJson(issues3)

String mailBody = recalcSLA(jsonList) ?: ' '

sendEmail(to, cc, bcc, "The following issues were missing SLAs, and have been recalculated.", mailBody.toString())

log.debug "The following issues were missing SLAs, and have been recalculated: \n" + mailBody

} else {

log.debug "SLA check complete. Everything looks good."


catch (e) {

log.error "There was a problem executing the SLA recalculation service. Please review: \n" + e.stackTrace


def recalcSLA(String list) {

HTTPBuilder http = new HTTPBuilder('http://localhost:8081/rest/servicedesk/1/servicedesk/sla/admin/task/destructive/reconstruct?force=false')

http.request(POST) {

requestContentType = contentType.JSON

request.addHeader("authorization: Basic <BASE 64 encoded credentials>", "ContentType: application/json")

body = list

response.success = { resp, JSON ->

return JSON


response.failure = { resp, JSON ->

return JSON




def sendEmail(String to, String cc, String bcc, String subject, String mailBody) {
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
def email = new Email(to)
log.debug("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")

0 answers

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

Do you use Jira Cloud with Microsoft Teams?

Hi, Jira users! Do you use Jira alongside Microsoft Teams? We want to hear how you’ve used the power of Jira Cloud and Microsoft Teams (via the Jira Cloud for Microsoft Teams app) to achieve a team...

113 views 0 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you