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

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

Avatar

1 badge earned

Collect

Participate in fun challenges

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

Challenges
Coins

Gift kudos to your peers

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

Recognition
Ribbon

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!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,390
Community Members
 
Community Events
176
Community Groups

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.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

Atlassian Community Events