Skip to content

Instantly share code, notes, and snippets.

@fraszczakszymon
Created April 8, 2016 10:13
Show Gist options
  • Save fraszczakszymon/5f1be18de8ae2ffd232eb1714e0e6f35 to your computer and use it in GitHub Desktop.
Save fraszczakszymon/5f1be18de8ae2ffd232eb1714e0e6f35 to your computer and use it in GitHub Desktop.
GrabResolver(name='restlet.org', root='http://maven.restlet.org')
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
@Grab( 'joda-time:joda-time:2.3' )
import groovy.util.XmlParser
import groovy.json.JsonBuilder
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
import org.joda.time.*
import org.joda.time.format.*
import static groovyx.net.http.Method.POST
if (args.length < 2) {
throw new RuntimeException('Missing arguments.')
}
File resultsFile = new File(args[1])
if (!resultsFile.exists()) {
throw new RuntimeException('File with results does not exist.')
}
def testingResults = new XmlParser().parseText(resultsFile.getText('UTF-8'))
Date date = new Date()
LocalDateTime localDate = new LocalDateTime()
def url = 'http://datalog-s5:9200/logstash-'+date.format('YYYY.MM.dd')+'/logs/'
def failedTests = []
testingResults.suite.test.each { test ->
test.class.each { testClass ->
testClass.'test-method'
.findResults { testMethod ->
if (testMethod.'@status' == 'FAIL') {
failedTests.add([
'class': testClass,
'method': testMethod
])
}
}
}
}
def json = new JsonBuilder();
json {
'@message' 'Jenkins build failed - ' + args[0]
'@severity' 'warning'
'@timestamp' localDate.toString()
'@context' {
'failed' testingResults.'@failed'.toInteger()
'passed' testingResults.'@passed'.toInteger()
'skipped' testingResults.'@skipped'.toInteger()
'duration-ms' testingResults.suite.'@duration-ms'.text()
'exceptions' failedTests.collect { test ->
[
'class': test.class.'@name',
'method': test.method.'@name',
'message': test.method.exception.message.text().trim()
]
}
}
}
def http = new HTTPBuilder(url)
http.request(POST) {
body = json.toString()
requestContentType = ContentType.JSON
response.success = { resp ->
println "Succeed: ${resp.status}"
}
response.failure = { resp ->
println "Failed: ${resp.status}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment