2
2
3
3
import org .jfrog .build .api .util .Log ;
4
4
import org .jfrog .build .client .artifactoryXrayResponse .*;
5
- import org .jfrog .build .extractor .scan .Severity ;
6
-
7
- import java .util .*;
8
5
9
6
import static org .jfrog .build .api .util .CommonUtils .emptyIfNull ;
10
7
11
8
/***
12
- * Helper for printing build scan results as a violations table to log.
9
+ * Helper for printing build scan results as violations tables to log.
13
10
*/
14
11
@ SuppressWarnings ("unused" )
15
12
public class BuildScanTableHelper {
16
13
private ArtifactoryXrayResponse scanResult ;
17
- private Map <Severity , Set <BuildScanTableElement >> table ;
18
14
private Log log ;
19
- private int longestDisplayName = 0 ;
20
- private String tableFormat = "" ;
21
- public String TABLE_HEADLINE = "Xray Scan Summary:" ;
22
- public List <String > TABLE_HEADERS = Arrays .asList ("#" , "Component" , "Severity" , "Type" );
15
+
16
+ SecurityViolationsTable securityViolationsTable ;
17
+ LicenseViolationsTable licenseViolationsTable ;
23
18
24
19
@ SuppressWarnings ("unused" )
25
- public void PrintTable (ArtifactoryXrayResponse scanResult , Log log ) {
20
+ public void printTable (ArtifactoryXrayResponse scanResult , Log log ) {
26
21
this .scanResult = scanResult ;
27
22
this .log = log ;
23
+ securityViolationsTable = new SecurityViolationsTable (log );
24
+ licenseViolationsTable = new LicenseViolationsTable (log );
28
25
generateResultTable ();
29
- updateTableFormat ();
30
- print ();
26
+ doPrintTables ();
31
27
}
32
28
33
29
/***
34
- * Prints the generated build scan table to log.
35
- * Table is rendered with the table format.
30
+ * Prints the generated violations tables to log.
36
31
*/
37
- private void print () {
38
- int line = 1 ;
39
- Severity [] severities = Severity .values ();
40
-
41
- // Print table headline.
42
- log .info (TABLE_HEADLINE );
43
- // Print column headers.
44
- printLine (TABLE_HEADERS .toArray ());
45
-
46
- // Print lines of violations by descending severity.
47
- for (int i = severities .length - 1 ; i >= 0 ; i --) {
48
- Severity severity = severities [i ];
49
- Set <BuildScanTableElement > elements = table .get (severity );
50
- if (elements == null ) {
51
- continue ;
52
- }
53
- for (BuildScanTableElement element : elements ) {
54
- printLine (line , element .getFileDisplayName (), severity .getSeverityName (), element .getIssueType ());
55
- line ++;
56
- }
57
- }
32
+ private void doPrintTables () {
33
+ securityViolationsTable .printTable ();
58
34
log .info ("" );
59
- }
60
-
61
- private void printLine (Object ... args ) {
62
- log .info (String .format (tableFormat , args ));
63
- }
64
-
65
- /***
66
- * Updates table format after longestDisplayName is known.
67
- * Format aligns elements to the left.
68
- * Padding on a column must be longer than the longest element in that column.
69
- */
70
- private void updateTableFormat () {
71
- // Index (assuming 5 digits is sufficient).
72
- tableFormat = "%-6s"
73
- // Display name (plus space).
74
- + "%-" + (longestDisplayName + 5 ) + "s"
75
- // Severity (Longest is 'Information').
76
- + "%-15s"
77
- // Type (Longest is 'Security').
78
- + "%-10s" ;
35
+ licenseViolationsTable .printTable ();
79
36
}
80
37
81
38
/***
82
39
* Loops over all alerts and adds infected files with required information.
83
40
*/
84
41
private void generateResultTable () {
85
- table = new HashMap <>();
86
42
for (Alert alert : emptyIfNull (scanResult .getAlerts ())) {
87
43
for (Issue issue : emptyIfNull (alert .getIssues ())) {
88
44
for (ImpactedArtifact impactedArtifact : emptyIfNull (issue .getImpactedArtifacts ())) {
@@ -94,23 +50,20 @@ private void generateResultTable() {
94
50
}
95
51
}
96
52
53
+ /**
54
+ * Add a violation to the corresponding table.
55
+ *
56
+ * @param issue Issue that caused violation.
57
+ * @param infectedFile Infected file.
58
+ */
97
59
private void addElement (Issue issue , InfectedFile infectedFile ) {
98
- // Create table element.
99
- Severity severity = Severity .fromString (issue .getSeverity ());
100
- BuildScanTableElement buildScanTableElement = new BuildScanTableElement (infectedFile .getDisplayName (), infectedFile .getSha256 (),
101
- issue .getType (), issue .getSummary (), issue .getDescription ());
102
-
103
- // Add element to table.
104
- Set <BuildScanTableElement > elements = table .get (severity );
105
- if (elements == null ) {
106
- elements = new HashSet <>();
107
- }
108
- elements .add (buildScanTableElement );
109
- table .put (severity , elements );
110
-
111
- // Update longest display name if longer.
112
- if (infectedFile .getDisplayName () != null && infectedFile .getDisplayName ().length () > longestDisplayName ) {
113
- longestDisplayName = infectedFile .getDisplayName ().length ();
60
+ Issue .IssueType issueType = issue .getIssueType ();
61
+ if (issueType == Issue .IssueType .SECURITY ) {
62
+ securityViolationsTable .addElement (issue , infectedFile );
63
+ } else if (issueType == Issue .IssueType .LICENSE ) {
64
+ licenseViolationsTable .addElement (issue , infectedFile );
65
+ } else {
66
+ throw new IllegalArgumentException (String .format ("Illegal issue type '%s'. Expecting either 'Security' or 'License'" , issue .getType ()));
114
67
}
115
68
}
116
69
}
0 commit comments