Skip to content

Commit d9e2052

Browse files
committed
Improved use of SonarQube
* Since issues are paginated by the server, paginated retrieval add * Set flags in analysis to skip some analysis steps we don't care about here * Since issues aren't available from the server immediate after client-side analysis is "done" (analysis report must be integrated) added the same kind of wait loop used internally in automated situations
1 parent 03816ce commit d9e2052

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

runSonarQube.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
mvn compile sonar:sonar -Dbuildtime.output.csv=true -Dbuildtime.output.csv.file=classes/out.csv
1+
mvn compile sonar:sonar -Dbuildtime.output.csv=true -Dbuildtime.output.csv.file=classes/out.csv -Dsonar.scm.disabled=true -Dsonar.skipDesign=true -Dsonar.cpd.exclusions=**/*.java -Dsonar.importSources=false -Dsonar.exclusions=**/*.xml
2+
done=false
3+
while [ "$done" != "true" ]
4+
do
5+
done=$(curl -sb -H "Accept: application/json" http://localhost:9000/api/analysis_reports/is_queue_empty)
6+
done
27
mvn validate -Ptime -Dexec.args="sonar"

src/main/java/org/owasp/benchmark/score/WriteTime.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import javax.xml.parsers.DocumentBuilder;
1616
import javax.xml.parsers.DocumentBuilderFactory;
1717

18+
import org.json.JSONArray;
1819
import org.json.JSONObject;
1920
import org.json.XML;
2021
import org.owasp.benchmark.helpers.PropertiesManager;
@@ -227,9 +228,27 @@ public void resultsFileName(String tool, String benchmarkVersion,
227228
}
228229

229230
public void writeSonarResults() {
231+
232+
int page = 1;
233+
int total = 1;
234+
JSONArray issues = new JSONArray();
235+
JSONObject json = null;
236+
230237
try {
231-
JSONObject json = new JSONObject(
232-
getSonarResults("http://localhost:9000"));
238+
239+
while (issues.length() < total) {
240+
json = new JSONObject(getSonarResults("http://localhost:9000", page));
241+
total = (int) json.get("total");
242+
243+
JSONArray issueSubset = json.getJSONArray("issues");
244+
for (int i = 0; i < issueSubset.length(); i++) {
245+
issues.put(issueSubset.get(i));
246+
}
247+
page++;
248+
}
249+
250+
json.put("issues", issues);
251+
233252
String xml = XML.toString(json);
234253
java.io.FileWriter fw = new java.io.FileWriter(SONAR_FILE);
235254
fw.write(xml);
@@ -240,10 +259,10 @@ public void writeSonarResults() {
240259
}
241260
}
242261

243-
public static String getSonarResults(String sonarURL) {
262+
public static String getSonarResults(String sonarURL, int page) {
244263
StringBuffer response = new StringBuffer();
245264
try {
246-
String url = sonarURL + "/api/issues/search?resolved=false";
265+
String url = sonarURL + "/api/issues/search?resolved=false&ps=500&p=" + page;
247266
URL obj = new URL(url);
248267
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
249268
con.setRequestMethod("GET");

src/main/java/org/owasp/benchmark/score/parsers/SonarReader.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public TestResults parse(File f) throws Exception {
4040
String fixed = "<sonar>" + new String(bytes, "UTF-8") + "</sonar>";
4141
InputSource is = new InputSource(new ByteArrayInputStream( fixed.getBytes() ) );
4242
Document doc = docBuilder.parse(is);
43-
43+
4444
TestResults tr = new TestResults( "SonarQube" ,false,TestResults.ToolType.SAST);
4545

4646
NodeList rootList = doc.getDocumentElement().getChildNodes();
@@ -55,32 +55,32 @@ public TestResults parse(File f) throws Exception {
5555
}
5656
return tr;
5757
}
58-
58+
5959
private TestCaseResult parseSonarIssue(Node flaw) {
6060
TestCaseResult tcr = new TestCaseResult();
6161
String rule = getNamedChild("rule", flaw).getTextContent();
6262
tcr.setCWE( cweLookup( rule.substring( "squid:".length() ) ) );
63-
63+
6464
String cat = getNamedChild("message", flaw).getTextContent();
6565
tcr.setCategory( cat );
66-
66+
6767
tcr.setConfidence( 5 );
6868

6969
tcr.setEvidence( cat );
7070

7171
String testfile = getNamedChild("component", flaw).getTextContent().trim();
7272
testfile = testfile.substring( testfile.lastIndexOf('/') +1 );
73-
if ( testfile.startsWith( "Benchmark" ) ) {
73+
if ( testfile.matches( "BenchmarkTest\\d+.java" ) ) {
7474
String testno = testfile.substring( "BenchmarkTest".length(), testfile.length() -5 );
7575
tcr.setNumber( Integer.parseInt( testno ) );
76-
return tcr;
76+
return tcr;
7777
}
7878
return null;
7979
}
8080

8181

82-
private int cweLookup(String squidNumber) {
83-
switch( squidNumber ) {
82+
private int cweLookup(String squidNumber) {
83+
switch( squidNumber ) {
8484
case "S00105" : return 0000; //S00105-Replace all tab characters in this file by sequences of white-spaces.
8585
case "S106" : return 0000; //S00106-Replace this usage of System.out or System.err by a logger.
8686
case "S00112" : return 397; //S00112-Generic exceptions should never be thrown
@@ -130,7 +130,6 @@ private int cweLookup(String squidNumber) {
130130
// System.out.println( "Failed to translate " + squidNumber );
131131
return -1;
132132
}
133-
133+
134134
}
135-
136-
135+

0 commit comments

Comments
 (0)