Issue
I have a json object which I receive in my jenkins pipeline through a url and I parse it like this:
def url = 'http://localhost:8080/job/find_issues/22/cppcheck/all/api/json'
def json = new JsonSlurper().parseText( new URL(url).text )
Which has this format
{
"_class": "io.jenkins.plugins.analysis.core.restapi.ReportApi",
"issues": [],
"size": 100,
"toString": "- (-): 100 issues (0 duplicates)"
}
which can have a different number of issues inside the issues
list, each of them having this structure:
{
"addedAt": 0,
"authorEmail": "-",
"authorName": "-",
"baseName": "stat_obj.cpp",
"category": "",
"columnEnd": 0,
"columnStart": 0,
"commit": "-",
"description": "",
"fileName": "D:/Source/Game/objects/stat_obj.cpp",
"fingerprint": "D2CD3A23FB45D3A1F1C3CB8BE5241602",
"lineEnd": 5592,
"lineStart": 5592,
"message": "Unmatched '('. Configuration: 'DEBUGMAPEXPLORATION'.",
"moduleName": "",
"origin": "cppcheck",
"originName": "CPPCheck",
"packageName": "-",
"reference": "19",
"severity": "HIGH",
"toString": "D:/Source/Game/objects/stat_obj.cpp(5592,0): syntaxError: : Unmatched '('. Configuration: 'DEBUGMAPEXPLORATION'.",
"type": "syntaxError"
}
How can I access the Issues list and the iterate through each one of the issues and them through each of the issue keys? I have tried different ways, lastly I have this:
for (entry in json) {
if(entry.key == "issues")
{
for (issue in entry.value) {
entry.value.each{ key, value ->
if (key == "fileName"){
errorsList.add(value)
}
if (key == "lineStart"){
errorsList.add(value)
}
if (key == "message"){
errorsList.add(value)
}
if (key == "severity"){
errorsList.add(value)
}
if (key == "type"){
errorsList.add(value)
}
def msg = "New ERROR found in static analysis, TYPE OF ERROR ${errorsList[4]}, SEVERITY: ${errorsList[3]}, ERROR MESSAGE: ${errorsList[2]}, FILE ${errorsList[0]} AT LINE: ${errorsList[1]}"
println msg
errorsList.clear()
}
}
}
}
But when I call print msg
I have something like this:
17:13:09 New ERROR found in static analysis, TYPE OF ERROR null, SEVERITY: null, ERROR MESSAGE: null, FILE null AT LINE: null
It seems like I am over iterating but I do not get to see where is the mistake...
Solution
json.issues.each{issue->
def msg = "New ERROR found in static analysis, TYPE OF ERROR ${issue.type}"+
", SEVERITY: ${issue.severity}, ERROR MESSAGE: ${issue.message}"+
", FILE ${issue.fileName} AT LINE: ${issue.lineStart}"
println msg
}
Answered By - daggett