Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor build scan table #545

Merged
merged 2 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,43 @@

import org.jfrog.build.api.util.Log;
import org.jfrog.build.client.artifactoryXrayResponse.*;
import org.jfrog.build.extractor.scan.Severity;

import java.util.*;

import static org.jfrog.build.api.util.CommonUtils.emptyIfNull;

/***
* Helper for printing build scan results as a violations table to log.
* Helper for printing build scan results as violations tables to log.
*/
@SuppressWarnings("unused")
public class BuildScanTableHelper {
private ArtifactoryXrayResponse scanResult;
private Map<Severity, Set<BuildScanTableElement>> table;
private Log log;
private int longestDisplayName = 0;
private String tableFormat = "";
public String TABLE_HEADLINE = "Xray Scan Summary:";
public List<String> TABLE_HEADERS = Arrays.asList("#", "Component", "Severity", "Type");

SecurityViolationsTable securityViolationsTable;
LicenseViolationsTable licenseViolationsTable;

@SuppressWarnings("unused")
public void PrintTable(ArtifactoryXrayResponse scanResult, Log log) {
RobiNino marked this conversation as resolved.
Show resolved Hide resolved
this.scanResult = scanResult;
this.log = log;
securityViolationsTable = new SecurityViolationsTable(log);
licenseViolationsTable = new LicenseViolationsTable(log);
generateResultTable();
updateTableFormat();
print();
printTables();
}

/***
* Prints the generated build scan table to log.
* Table is rendered with the table format.
* Prints the generated violations tables to log.
*/
private void print() {
int line = 1;
Severity[] severities = Severity.values();

// Print table headline.
log.info(TABLE_HEADLINE);
// Print column headers.
printLine(TABLE_HEADERS.toArray());

// Print lines of violations by descending severity.
for (int i = severities.length - 1; i >= 0; i--) {
Severity severity = severities[i];
Set<BuildScanTableElement> elements = table.get(severity);
if (elements == null) {
continue;
}
for (BuildScanTableElement element : elements) {
printLine(line, element.getFileDisplayName(), severity.getSeverityName(), element.getIssueType());
line++;
}
}
private void printTables() {
RobiNino marked this conversation as resolved.
Show resolved Hide resolved
securityViolationsTable.printTable();
log.info("");
}

private void printLine(Object... args) {
log.info(String.format(tableFormat, args));
}

/***
* Updates table format after longestDisplayName is known.
* Format aligns elements to the left.
* Padding on a column must be longer than the longest element in that column.
*/
private void updateTableFormat() {
// Index (assuming 5 digits is sufficient).
tableFormat = "%-6s"
// Display name (plus space).
+ "%-" + (longestDisplayName + 5) + "s"
// Severity (Longest is 'Information').
+ "%-15s"
// Type (Longest is 'Security').
+ "%-10s";
licenseViolationsTable.printTable();
}

/***
* Loops over all alerts and adds infected files with required information.
*/
private void generateResultTable() {
table = new HashMap<>();
for (Alert alert : emptyIfNull(scanResult.getAlerts())) {
for (Issue issue : emptyIfNull(alert.getIssues())) {
for (ImpactedArtifact impactedArtifact : emptyIfNull(issue.getImpactedArtifacts())) {
Expand All @@ -94,23 +50,21 @@ private void generateResultTable() {
}
}

/**
* Add a violation to the corresponding table.
*
* @param issue Issue that caused violation.
* @param infectedFile Infected file.
*/
private void addElement(Issue issue, InfectedFile infectedFile) {
// Create table element.
Severity severity = Severity.fromString(issue.getSeverity());
BuildScanTableElement buildScanTableElement = new BuildScanTableElement(infectedFile.getDisplayName(), infectedFile.getSha256(),
issue.getType(), issue.getSummary(), issue.getDescription());

// Add element to table.
Set<BuildScanTableElement> elements = table.get(severity);
if (elements == null) {
elements = new HashSet<>();
}
elements.add(buildScanTableElement);
table.put(severity, elements);

// Update longest display name if longer.
if (infectedFile.getDisplayName() != null && infectedFile.getDisplayName().length() > longestDisplayName) {
longestDisplayName = infectedFile.getDisplayName().length();
String violationType = issue.getType();
switch (violationType) {
case "Security":
securityViolationsTable.addElement(issue, infectedFile);
break;
case "License":
licenseViolationsTable.addElement(issue, infectedFile);
break;
RobiNino marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.jfrog.build.extractor.buildScanTable;

import org.jfrog.build.api.util.Log;
import org.jfrog.build.client.artifactoryXrayResponse.InfectedFile;
import org.jfrog.build.client.artifactoryXrayResponse.Issue;

import java.util.Objects;

class LicenseViolationsTable extends ScanTableBase {
RobiNino marked this conversation as resolved.
Show resolved Hide resolved
LicenseViolationsTable(Log log) {
super(log);
}

String getHeadline() {
return "License Compliance Violations";
}

String[] getHeaders() {
return new String[]{"#", "Severity", "Component"};
}

String getTableFormat() {
return super.getFormatBase(longestDisplayName);
}

String getEmptyTableLine() {
return "No license compliance violations were found";
}

void addElement(Issue issue, InfectedFile infectedFile) {
// Create table element.
LicenseTableElement element = new LicenseTableElement(infectedFile.getDisplayName(), infectedFile.getSha256(),
issue.getSummary(), issue.getDescription());
super.addElement(table, issue, element);
// Update the longest display name if longer.
if (infectedFile.getDisplayName() != null && infectedFile.getDisplayName().length() > longestDisplayName) {
longestDisplayName = infectedFile.getDisplayName().length();
}
}

void printTable() {
super.printTable(table);
}

static class LicenseTableElement extends TableElementBase {
RobiNino marked this conversation as resolved.
Show resolved Hide resolved
LicenseTableElement(String fileDisplayName, String fileSha256,
String issueSummary, String issueDescription) {
super(fileDisplayName, fileSha256, issueSummary, issueDescription);
}

@Override
Object[] getLineArgs(int line, String severityName) {
return new Object[]{line, severityName, this.getFileDisplayName()};
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LicenseTableElement that = (LicenseTableElement) o;
return Objects.equals(fileDisplayName, that.fileDisplayName) &&
Objects.equals(fileSha256, that.fileSha256) &&
Objects.equals(issueSummary, that.issueSummary) &&
Objects.equals(issueDescription, that.issueDescription);
}

@Override
public int hashCode() {
return Objects.hash(fileDisplayName, fileSha256, issueSummary, issueDescription);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.jfrog.build.extractor.buildScanTable;

import org.jfrog.build.api.util.Log;
import org.jfrog.build.client.artifactoryXrayResponse.Issue;
import org.jfrog.build.extractor.scan.Severity;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Base class for violations tables printed by {@link BuildScanTableHelper}
*/
abstract class ScanTableBase {
Log log;
int longestDisplayName = 0;
final Map<Severity, Set<TableElementBase>> table = new HashMap<>();

ScanTableBase(Log log) {
this.log = log;
}

abstract String getHeadline();

abstract String[] getHeaders();

abstract String getTableFormat();

abstract String getEmptyTableLine();

String getFormatBase(int longestDisplayName) {
// Index (assuming 5 digits is sufficient).
return "%-6s"
// Severity (Longest is 'Information').
+ "%-14s"
// Display name (plus space).
+ "%-" + (longestDisplayName + 3) + "s";
}

void printFormattedLine(Object... args) {
log.info(String.format(getTableFormat(), args));
}

void addElement(Map<Severity, Set<TableElementBase>> table, Issue issue, TableElementBase element) {
Severity severity = Severity.fromString(issue.getSeverity());
Set<TableElementBase> elements = table.get(severity);
if (elements == null) {
elements = new HashSet<>();
}
elements.add(element);
table.put(severity, elements);
}

void printTable(Map<Severity, Set<TableElementBase>> table) {
int line = 1;
Severity[] severities = Severity.values();

// Print table headline.
log.info(getHeadline());

// If table is empty, print the no violations found line and return.
if (table.isEmpty()) {
log.info(getEmptyTableLine());
log.info("");
return;
}

// Print column headers.
printFormattedLine((Object[]) getHeaders());

// Print lines of violations by descending severity.
for (int i = severities.length - 1; i >= 0; i--) {
Severity severity = severities[i];
Set<TableElementBase> elements = table.get(severity);
if (elements == null) {
continue;
}
for (TableElementBase element : elements) {
printFormattedLine(element.getLineArgs(line, severity.getSeverityName()));
line++;
}
}
log.info("");
}

/**
* Base class for elements of the violations tables
*/
abstract static class TableElementBase {
public final String fileDisplayName;
// Following fields are for the set's uniqueness only:
public final String fileSha256;
public final String issueSummary;
public final String issueDescription;

public TableElementBase(String fileDisplayName, String fileSha256,
String issueSummary, String issueDescription) {
this.fileDisplayName = fileDisplayName;
this.fileSha256 = fileSha256;
this.issueSummary = issueSummary;
this.issueDescription = issueDescription;
}

String getFileDisplayName() {
return fileDisplayName == null ? "" : fileDisplayName;
}

abstract Object[] getLineArgs(int line, String severityName);
}
}
Loading