Skip to content

Commit

Permalink
Fix data generation for policy evaluation summary table
Browse files Browse the repository at this point in the history
Fixes JENKINS-43048

Signed-off-by: Swathi Gangisetty <swathi@anchore.com>
  • Loading branch information
Swathi Gangisetty committed Mar 22, 2017
1 parent 564a085 commit a069e51
Showing 1 changed file with 62 additions and 17 deletions.
79 changes: 62 additions & 17 deletions src/main/java/com/anchore/jenkins/plugins/anchore/BuildWorker.java
Expand Up @@ -204,37 +204,82 @@ public GATE_ACTION runGates() throws AbortException {
console.logDebug("Parsing and summarizing gate output in " + jenkinsGatesOutputFP.getRemote());
if (jenkinsGatesOutputFP.exists() && jenkinsGatesOutputFP.length() > 0) {
JSONObject gatesJson = JSONObject.fromObject(jenkinsGatesOutputFP.readToString());
// Populate once and reuse
int numColumns = 0, repoTagIndex = -1, gateNameIndex = -1, gateActionIndex = -1;

if (gatesJson != null) {
JSONArray summaryRows = new JSONArray();
for (Object imageKey : gatesJson.keySet()) {
JSONObject content = gatesJson.getJSONObject((String) imageKey);
if (null != content) {
JSONObject result = content.getJSONObject("result");
if (null != result) {

// populate data from header element once, most likely for the first image
if (numColumns <= 0 || repoTagIndex < 0 || gateNameIndex < 0 || gateActionIndex < 0) {
JSONArray header = result.getJSONArray("header");
if (null != header) {
numColumns = header.size();
console.logDebug("Found " + numColumns + " column titles in gate output");
for (int i = 0; i < header.size(); i++) {
switch (header.getString(i)) {
case "Repo_Tag":
repoTagIndex = i;
break;
case "Gate":
gateNameIndex = i;
break;
case "Gate_Action":
gateActionIndex = i;
break;
default:
break;
}
}
} else {
console.logWarn("\'header\' element not found in gate output, skipping summary computation for " + imageKey);
continue;
}
} else {
// indices have been populated, reuse it
}

if (numColumns <= 0 || repoTagIndex < 0 || gateNameIndex < 0 || gateActionIndex < 0) {
console.logWarn(
"Either \'header\' element has no columns or column indices (for Repo_Tag, Gate, Gate_Action) not "
+ "initialized, skipping summary computation for " + imageKey);
continue;
}

JSONArray rows = result.getJSONArray("rows");
if (null != rows) {
int stop = 0, warn = 0, go = 0;
String repoTag = null;

for (int i = 0; i < rows.size(); i++) {
JSONArray row = rows.getJSONArray(i);
if (row.size() == 6 && !row.getString(2).equals("FINAL")) {
if (row.size() == numColumns) {
if (Strings.isNullOrEmpty(repoTag)) {
repoTag = row.getString(1);
repoTag = row.getString(repoTagIndex);
}
switch (row.getString(5)) {
case "STOP":
stop++;
break;
case "WARN":
warn++;
break;
case "GO":
go++;
break;
default:
break;
if (!row.getString(gateNameIndex).equals("FINAL")) {
switch (row.getString(gateActionIndex)) {
case "STOP":
stop++;
break;
case "WARN":
warn++;
break;
case "GO":
go++;
break;
default:
break;
}
}
} else {
console.logWarn("Expected " + numColumns + " elements but got " + row.size() + ", skipping row " + row
+ " in summary computation for " + imageKey);
}
}

Expand All @@ -247,15 +292,15 @@ public GATE_ACTION runGates() throws AbortException {
summaryRows.add(summaryRow);

} else { // rows object not found
console.logWarn("\'rows\' element not found in gate output for " + imageKey + ", moving on");
console.logWarn("\'rows\' element not found in gate output, skipping summary computation for " + imageKey);
continue;
}
} else { // result object not found, log and move on
console.logWarn("\'result\' element not found in gate output for " + imageKey + ", moving on");
console.logWarn("\'result\' element not found in gate output, skipping summary computation for " + imageKey);
continue;
}
} else { // no content found for a given image id, log and move on
console.logWarn("No mapped object found in gate output for " + imageKey + " in gate output, moving on");
console.logWarn("No mapped object found in gate output, skipping summary computation for " + imageKey);
continue;
}
}
Expand Down

0 comments on commit a069e51

Please sign in to comment.