1
1
package org .owasp .benchmark .report .sonarqube ;
2
2
3
+ import static java .lang .String .join ;
4
+ import static java .nio .charset .Charset .defaultCharset ;
5
+ import static org .apache .commons .io .FileUtils .writeStringToFile ;
6
+ import static org .apache .commons .io .IOUtils .readLines ;
7
+
3
8
import com .fasterxml .jackson .core .JsonProcessingException ;
4
9
import com .fasterxml .jackson .databind .ObjectMapper ;
5
- import org .owasp .benchmark .report .sonarqube .dto .SonarQubeResult ;
6
-
7
- import javax .xml .parsers .DocumentBuilderFactory ;
8
10
import java .io .File ;
9
11
import java .io .IOException ;
10
12
import java .net .HttpURLConnection ;
15
17
import java .util .List ;
16
18
import java .util .Set ;
17
19
import java .util .function .Consumer ;
18
-
19
- import static java .lang .String .join ;
20
- import static java .nio .charset .Charset .defaultCharset ;
21
- import static org .apache .commons .io .FileUtils .writeStringToFile ;
22
- import static org .apache .commons .io .IOUtils .readLines ;
20
+ import javax .xml .parsers .DocumentBuilderFactory ;
21
+ import org .owasp .benchmark .report .sonarqube .dto .SonarQubeResult ;
23
22
24
23
public class SonarReport {
25
24
private static final String SONAR_USER = "admin" ;
@@ -30,9 +29,8 @@ public class SonarReport {
30
29
31
30
private static final int PAGE_SIZE = 500 ;
32
31
33
- private static final String sonarAuth = Base64 .getEncoder ()
34
- .encodeToString ((SONAR_USER + ":" + SONAR_PASSWORD )
35
- .getBytes ());
32
+ private static final String sonarAuth =
33
+ Base64 .getEncoder ().encodeToString ((SONAR_USER + ":" + SONAR_PASSWORD ).getBytes ());
36
34
37
35
private static final ObjectMapper objectMapper = new ObjectMapper ();
38
36
@@ -42,55 +40,56 @@ public static void main(String[] args) throws Exception {
42
40
List <String > hotspots = new ArrayList <>();
43
41
44
42
forAllPagesAt (
45
- "issues/search?componentKeys=" + SONAR_PROJECT + "&types=VULNERABILITY&&rules=" + allJavaRules ,
46
- (result -> issues .addAll (result .issues ))
47
- );
43
+ "issues/search?componentKeys="
44
+ + SONAR_PROJECT
45
+ + "&types=VULNERABILITY&&rules="
46
+ + allJavaRules ,
47
+ (result -> issues .addAll (result .issues )));
48
48
forAllPagesAt (
49
- "hotspots/search?projectKey=" + SONAR_PROJECT ,
50
- (result -> hotspots .addAll (result .hotspots ))
51
- );
49
+ "hotspots/search?projectKey=" + SONAR_PROJECT ,
50
+ (result -> hotspots .addAll (result .hotspots )));
52
51
53
52
writeStringToFile (
54
- new File ("results/" + resultFilename () + ".json" ),
55
- formattedJson (issues , hotspots ),
56
- defaultCharset ()
57
- );
53
+ new File ("results/" + resultFilename () + ".json" ),
54
+ formattedJson (issues , hotspots ),
55
+ defaultCharset ());
58
56
}
59
57
60
58
private static String resultFilename () throws Exception {
61
59
return "Benchmark_" + benchmarkVersion () + "-sonarqube-v" + apiCall ("server/version" );
62
60
}
63
61
64
62
private static String benchmarkVersion () throws Exception {
65
- return DocumentBuilderFactory
66
- .newInstance ()
67
- .newDocumentBuilder ()
68
- .parse (new File ("pom.xml" ))
69
- .getElementsByTagName ("version" )
70
- .item (0 )
71
- .getTextContent ();
63
+ return DocumentBuilderFactory .newInstance ()
64
+ .newDocumentBuilder ()
65
+ .parse (new File ("pom.xml" ))
66
+ .getElementsByTagName ("version" )
67
+ .item (0 )
68
+ .getTextContent ();
72
69
}
73
70
74
71
private static Set <String > allJavaRules () throws IOException {
75
72
Set <String > javaRuleIds = new HashSet <>();
76
73
77
- forAllPagesAt ("rules/search" , (result ) -> result
78
- .rules
79
- .stream ().filter (rule -> rule .ruleId .startsWith ("java:" ))
80
- .forEach (rule -> javaRuleIds .add (rule .ruleId )));
74
+ forAllPagesAt (
75
+ "rules/search" ,
76
+ (result ) ->
77
+ result .rules .stream ()
78
+ .filter (rule -> rule .ruleId .startsWith ("java:" ))
79
+ .forEach (rule -> javaRuleIds .add (rule .ruleId )));
81
80
82
81
return javaRuleIds ;
83
82
}
84
83
85
- private static void forAllPagesAt (String apiPath , Consumer <SonarQubeResult > pageHandlerCallback ) throws IOException {
84
+ private static void forAllPagesAt (String apiPath , Consumer <SonarQubeResult > pageHandlerCallback )
85
+ throws IOException {
86
86
int pages ;
87
87
int page = 1 ;
88
88
89
89
do {
90
- SonarQubeResult result = objectMapper .readValue (
91
- apiCall (apiPath + pagingSuffix (page , apiPath )),
92
- SonarQubeResult .class
93
- );
90
+ SonarQubeResult result =
91
+ objectMapper .readValue (
92
+ apiCall (apiPath + pagingSuffix (page , apiPath )), SonarQubeResult .class );
94
93
95
94
pages = (result .paging .resultCount / PAGE_SIZE ) + 1 ;
96
95
@@ -114,11 +113,17 @@ private static String apiCall(String apiPath) throws IOException {
114
113
return join ("\n " , readLines (connection .getInputStream (), defaultCharset ()));
115
114
}
116
115
117
- private static String formattedJson (List <String > issues , List <String > hotspots ) throws JsonProcessingException {
118
- String sb = "{\" issues\" :[" + join ("," , issues ) + "],\" hotspots\" :[" + join ("," , hotspots ) + "]}" ;
116
+ private static String formattedJson (List <String > issues , List <String > hotspots )
117
+ throws JsonProcessingException {
118
+ String sb =
119
+ "{\" issues\" :["
120
+ + join ("," , issues )
121
+ + "],\" hotspots\" :["
122
+ + join ("," , hotspots )
123
+ + "]}" ;
119
124
120
125
return objectMapper
121
- .writerWithDefaultPrettyPrinter ()
122
- .writeValueAsString (objectMapper .readValue (sb , Object .class ));
126
+ .writerWithDefaultPrettyPrinter ()
127
+ .writeValueAsString (objectMapper .readValue (sb , Object .class ));
123
128
}
124
129
}
0 commit comments