Skip to content

Commit 48b928d

Browse files
committed
Merge pull request #7 from ganncamp/SonarQube
Improved use of SonarQube
2 parents 3baccca + 768d99b commit 48b928d

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

runSonarQube.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
call mvn compile sonar:sonar -Dbuildtime.output.csv=true -Dbuildtime.output.csv.file=classes\out.csv
1+
call 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
22
call mvn validate -Ptime -Dexec.args="sonar"

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/SonarQubeLegacyReader.java

Lines changed: 13 additions & 14 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,25 +55,25 @@ 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
}
@@ -104,12 +104,12 @@ private TestCaseResult parseSonarIssue(Node flaw) {
104104
// case "Weak Cryptographic Hash" : return 328;
105105
// case "Weak Encryption" : return 327;
106106
// case "XPath Injection" : return 643;
107-
108107

109-
110-
111-
public static int cweLookup(String squidNumber) {
112-
switch( squidNumber ) {
108+
109+
110+
111+
public static int cweLookup(String squidNumber) {
112+
switch( squidNumber ) {
113113
case "S00105" : return 0000; //S00105-Replace all tab characters in this file by sequences of white-spaces.
114114
case "S106" : return 0000; //S00106-Replace this usage of System.out or System.err by a logger.
115115
case "S00112" : return 397; //S00112-Generic exceptions should never be thrown
@@ -159,7 +159,6 @@ public static int cweLookup(String squidNumber) {
159159
// System.out.println( "Failed to translate " + squidNumber );
160160
return -1;
161161
}
162-
162+
163163
}
164-
165-
164+

0 commit comments

Comments
 (0)