Skip to content

Instantly share code, notes, and snippets.

@SamGajdos
Created April 3, 2025 16:40
Show Gist options
  • Select an option

  • Save SamGajdos/0b3f885d09d1c0166d5f280720dc6092 to your computer and use it in GitHub Desktop.

Select an option

Save SamGajdos/0b3f885d09d1c0166d5f280720dc6092 to your computer and use it in GitHub Desktop.
Print CSV data with failed tests from Broker QE test suite
import jenkins.model.Jenkins
import com.cloudbees.hudson.plugins.folder.Folder
import hudson.model.Job
import groovy.json.JsonSlurper
def prefix = "7.11"
def targetValue = "7.11.8.CR1"
def targetOpValue = "7.11.8.OPR.1.CR1"
def testSuitepattern = "claire/operator" // Could be for example: "ats-test-suite/main-tests"
def jobsWithTests = []
def jsonSlurper = new JsonSlurper()
def testFailures = [:] // Map to track failures per test
def configurations = []
def lastMatchingBuild
def matchingBuilds = []
def jenkinsInstance = Jenkins.getInstanceOrNull()
if (jenkinsInstance) {
jenkinsInstance.allItems.findAll { job ->
job.fullName.startsWith(prefix) && !(job instanceof Folder) && !job.fullName.toLowerCase().contains("custom") && !job.fullName.toLowerCase().contains("trigger") && job.fullName.toLowerCase().contains(testSuitepattern)
}.each { job ->
if (job instanceof Job) {
matchingBuilds = job.builds.findAll { build ->
build.getAction(hudson.model.ParametersAction)?.parameters?.any { param ->
(param.name == "ARTEMIS_VERSION" || param.name == "BROKER_VERSION") && param.value == targetValue ||
param.name == "OPERATOR_VERSION" && param.value == targetOpValue
}
}
if (!matchingBuilds.isEmpty()) {
def latestBuild = matchingBuilds[0]
matchingBuilds.each { build ->
if (build.timestamp > latestBuild.timestamp) {
latestBuild = build
}
}
lastMatchingBuild = latestBuild
}
if (lastMatchingBuild != null) {
def url = "${lastMatchingBuild.absoluteUrl}testReport/api/json"
try {
def connection = new URL(url).openConnection()
connection.setRequestMethod("GET")
connection.connect()
if (connection.responseCode == 200) {
def jsonResponse = jsonSlurper.parse(connection.inputStream)
if (jsonResponse.suites != null) {
def config = job.fullName.split("/").drop(2).join("/")
configurations.add(config)
testFailures[config] = [:]
jsonResponse.suites.each { suite ->
suite.cases.each { testCase ->
if (testCase.status == "FAILED") {
def testName = "\"" + testCase.className + "." + testCase.name + "\""
if (!testFailures[config].containsKey(testName)) {
testFailures[config][testName] = 0
}
testFailures[config][testName] += 1
}
}
}
jobsWithTests << lastMatchingBuild
}
}
} catch (Exception e) {
println(e)
println "Failed to fetch test results for ${job.fullName}"
}
}
}
}
}
// Generate CSV Output
def csvOutput = new StringBuilder()
csvOutput.append("Failing Test,Count," + configurations.join(",") + "\n")
def allTestNames = testFailures.values().collectMany { it.keySet() }.unique()
allTestNames.each { testName ->
def row = [testName]
def totalCount = 0
configurations.each { config ->
def count = testFailures[config].get(testName, 0)
row << count.toString()
totalCount += count
}
if (totalCount > 0) {
row.add(1, totalCount.toString())
csvOutput.append(row.join(",") + "\n")
}
}
// Print the CSV data to the console
println csvOutput.toString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment