Skip to content

Instantly share code, notes, and snippets.

@Kingkha
Created April 1, 2020 09:50
Show Gist options
  • Select an option

  • Save Kingkha/44e0319a39ac0c9529a3a97d091239ab to your computer and use it in GitHub Desktop.

Select an option

Save Kingkha/44e0319a39ac0c9529a3a97d091239ab to your computer and use it in GitHub Desktop.
import groovy.json.JsonOutput
import static groovy.io.FileType.FILES
pipeline {
agent any
stages {
stage('Build') {
steps {
catchError {
sh '$WORKSPACE/run_collection.sh'
}
script {
//def data = readJSON file: 'newman/report.json'
//data = data['run']['timings'].toString()
def map = new HashMap<String, HashMap>()
def mapError = new HashMap<String, String>()
def filenames = findFiles(glob: 'newman/*.xml')
echo "${filenames}"
filenames.each { collection ->
def xml = readFile "${env.WORKSPACE}/${collection}"
new XmlSlurper().parseText(xml).toList().collect { testSuite ->
def nodes = testSuite.testsuite.toList()
def innerMap = new HashMap<String, HashMap>()
def suiteErrors = 0
for (int j = 0; j < nodes.size(); j++){
def attributes = nodes.get(j).attributes()
def detailMap = new HashMap<String, Double>()
def name = attributes.get('name')
def time = Double.parseDouble(attributes.get('time'))
def tests = Double.parseDouble(attributes.get('tests'))
def failures = Double.parseDouble(attributes.get('failures'))
def errors = Double.parseDouble(attributes.get('errors'))
detailMap.put('time', time)
detailMap.put('tests', tests)
detailMap.put('failures', failures)
detailMap.put('errors', errors)
suiteErrors += errors + failures
innerMap.put("${name}", detailMap)
}
def suiteName = testSuite.attributes().get('name')
map.put("${suiteName}", innerMap)
if (suiteErrors > 0) {
mapError.put("${suiteName}", suiteErrors)
currentBuild.result = 'FAILURE'
}
}
}
def res = new groovy.json.JsonBuilder(map).toString()
echo "${res}"
if (mapError.size() != 0) {
def errorMsg = ""
mapError.each {k, v ->
errorMsg += "$k: $v "
}
env.ERROR_MSG = errorMsg
}
logstash {
currentBuild.description="${res}"
}
}
}
}
}
post {
always {
junit 'newman/*.xml'
}
unstable {
slackSend (channel: '#notifications', color: '#0000FF', message: "UNSTABLE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
success {
slackSend (channel: '#notifications', color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
failure {
script {
if (env.ERROR_MSG.contains("cic")) {
echo "cic error"
slackSend (channel: '#cic-notifications', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME}' Failures: ${env.ERROR_MSG} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
slackSend (channel: '#notifications', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME}' Failures: ${env.ERROR_MSG} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
slackSend (channel: '#error-notifications', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME}' Failures: ${env.ERROR_MSG} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment