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

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

Bryan Karsh September 6, 2018

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.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import groovy.json.JsonOutput
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.*
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("MissingSLARecalculationService")
log.setLevel(Level.DEBUG)



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

def to = "me@somedomain.com" // 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 = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())

issues = searchResult.issues.collect { issueManager.getIssueObject(it.id) }
}

/* 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)
email.setMimeType("text/html")
email.setSubject(subject)
email.setBody(mailBody)
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")
}
}

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events