@@ -50,10 +50,11 @@ public void checkServerTrusted(java.security.cert.X509Certificate[] certs, Strin
5050 }
5151 }
5252
53- public static JSONObject fetchDataFromURL (String url , String call , String token , String projectKey ) throws IOException , InterruptedException {
53+ public static JSONObject fetchDataFromURL (String url , String call , String token , String projectKey , String branch ) throws IOException , InterruptedException {
5454 HttpClient client = createUnsafeHttpClient ();
5555 String encodedProjectKey = URLEncoder .encode (projectKey , StandardCharsets .UTF_8 );
56- String fullURL = String .format ("%s%s%s" , url , call , encodedProjectKey );
56+ String encodedBranch = URLEncoder .encode (branch , StandardCharsets .UTF_8 );
57+ String fullURL = String .format ("%s%s%s&branch=%s" , url , call , encodedProjectKey , encodedBranch );
5758
5859
5960 HttpRequest request = HttpRequest .newBuilder ()
@@ -75,20 +76,21 @@ public static JSONObject fetchDataFromURL(String url, String call, String token,
7576
7677 @ SuppressWarnings ("empty-statement" )
7778 public static void main (String [] args ) throws IOException {
78- if (args .length < 3 ) {
79- System .err .println ("Usage: java -jar target/pdf-generator-1.0-SNAPSHOT-jar-with-dependencies.jar <apiUrl> <authToken> <project>" );
79+ if (args .length < 4 ) {
80+ System .err .println ("Usage: java -jar target/pdf-generator-1.0-SNAPSHOT-jar-with-dependencies.jar <apiUrl> <authToken> <project> <branch> " );
8081 System .exit (1 );
8182 }
8283 String apiUrl = args [0 ];
8384 String authToken = args [1 ];
8485 String project = args [2 ];
86+ String branch = args [3 ];
8587
8688 // We create the initial pdf
8789 PDFReportWriter pdf = new PDFReportWriter ();
8890 JSONObject data = null ;
8991 JSONArray dataArray = null ;
9092 try {
91- data = fetchDataFromURL (apiUrl , "/api/navigation/component?component=" , authToken , project );
93+ data = fetchDataFromURL (apiUrl , "/api/navigation/component?component=" , authToken , project , branch );
9294 } catch (IOException | InterruptedException e ) {
9395 System .err .println ("Error at doing the HTTP petition: " + e .getMessage ());
9496 }
@@ -102,8 +104,8 @@ public static void main(String[] args) throws IOException {
102104 pdf .addLine ("• This document contains results of the code analysis of " + name + "." );
103105
104106 String date = "• Date: " + data .getString ("analysisDate" );
105- String branch = "• Branch: " + data .getString ("branch" );
106- pdf .addLine (branch );
107+ String outputBranch = "• Branch: " + data .getString ("branch" );
108+ pdf .addLine (outputBranch );
107109 pdf .addLine (date .replace ("T" , " " ));
108110
109111 // Configuration
@@ -132,29 +134,31 @@ public static void main(String[] args) throws IOException {
132134 qualityGate += qualityGateList .getString ("name" ) + "." ;
133135 pdf .addLine (qualityGate );
134136
135- // SYNTHESYS
136- pdf .tittle2Font ();
137- pdf .addLine ("SYNTHESIS" );
138-
139- // ANALYSIS STATUS
140- pdf .tittle3Font ();
141- pdf .addLine ("ANALYSIS STATUS" );
142-
143137
144138 String [] headers = { "Reliability" , "Security" , "Security Review" , "Maintainability" };
145139 List <String []> rows = new ArrayList <>();
146140
147141 data = null ;
148142 try {
149143
150- data = fetchDataFromURL (apiUrl , "/api/measures/component?metricKeys=reliability_rating,software_quality_maintainability_rating,security_rating,security_review_rating&component=" , authToken , project );
144+ data = fetchDataFromURL (apiUrl , "/api/measures/component?metricKeys=reliability_rating,software_quality_maintainability_rating,security_rating,security_review_rating&component=" , authToken , project , branch );
151145 data = data .getJSONObject ("component" );
152146
153147 } catch (IOException | InterruptedException e ) {
154148 System .err .println ("Error at doing the HTTP petition: " + e .getMessage ());
155149 }
150+
151+ // SYNTHESYS
152+ pdf .tittle2Font ();
153+ pdf .addLine ("SYNTHESIS" );
154+
155+ // ANALYSIS STATUS
156+ pdf .tittle3Font ();
157+ pdf .addLine ("ANALYSIS STATUS" );
156158
157159 JSONArray measuresList = data .getJSONArray ("measures" );
160+
161+
158162 String [] measures = new String [4 ];
159163 for (int i = 0 ; i < measuresList .length (); i ++) {
160164 JSONObject measure = measuresList .getJSONObject (i );
@@ -181,7 +185,7 @@ public static void main(String[] args) throws IOException {
181185
182186 try {
183187
184- data = fetchDataFromURL (apiUrl , "/api/qualitygates/project_status?projectKey=" , authToken , project );
188+ data = fetchDataFromURL (apiUrl , "/api/qualitygates/project_status?projectKey=" , authToken , project , branch );
185189 data = data .getJSONObject ("projectStatus" );
186190
187191 } catch (IOException | InterruptedException e ) {
@@ -198,7 +202,7 @@ public static void main(String[] args) throws IOException {
198202
199203 try {
200204
201- data = fetchDataFromURL (apiUrl , "/api/measures/component?metricKeys=duplicated_lines_density,comment_lines_density,ncloc,complexity,cognitive_complexity,coverage&component=" , authToken , project );
205+ data = fetchDataFromURL (apiUrl , "/api/measures/component?metricKeys=duplicated_lines_density,comment_lines_density,ncloc,complexity,cognitive_complexity,coverage&component=" , authToken , project , branch );
202206 data = data .getJSONObject ("component" );
203207
204208 } catch (IOException | InterruptedException e ) {
@@ -244,7 +248,7 @@ public static void main(String[] args) throws IOException {
244248
245249 try {
246250
247- data = fetchDataFromURL (apiUrl , "/api/measures/component?metricKeys=duplicated_lines_density,comment_lines_density,ncloc,complexity,cognitive_complexity,coverage&component=" , authToken , project );
251+ data = fetchDataFromURL (apiUrl , "/api/measures/component?metricKeys=duplicated_lines_density,comment_lines_density,ncloc,complexity,cognitive_complexity,coverage&component=" , authToken , project , branch );
248252 data = data .getJSONObject ("component" );
249253
250254 } catch (IOException | InterruptedException e ) {
@@ -289,7 +293,7 @@ public static void main(String[] args) throws IOException {
289293
290294 try {
291295
292- data = fetchDataFromURL (apiUrl , "/api/measures/component?metricKeys=reliability_remediation_effort,security_remediation_effort,sqale_index&component=" , authToken , project );
296+ data = fetchDataFromURL (apiUrl , "/api/measures/component?metricKeys=reliability_remediation_effort,security_remediation_effort,sqale_index&component=" , authToken , project , branch );
293297 data = data .getJSONObject ("component" );
294298
295299 } catch (IOException | InterruptedException e ) {
@@ -324,14 +328,12 @@ public static void main(String[] args) throws IOException {
324328
325329 rows .add (measures );
326330 pdf .drawTable (500 , headers , rows );
331+
327332
328333 // LINES PER LANGUAGE
329- pdf .tittle3Font ();
330- pdf .addLine ("LINES PER LANGUAGE" );
331-
332334 try {
333335
334- data = fetchDataFromURL (apiUrl , "/api/measures/component?metricKeys=ncloc_language_distribution&component=" , authToken , project );
336+ data = fetchDataFromURL (apiUrl , "/api/measures/component?metricKeys=ncloc_language_distribution&component=" , authToken , project , branch );
335337 data = data .getJSONObject ("component" );
336338
337339 } catch (IOException | InterruptedException e ) {
@@ -343,21 +345,26 @@ public static void main(String[] args) throws IOException {
343345
344346 measuresList = data .getJSONArray ("measures" );
345347
346- JSONObject measure = measuresList .getJSONObject (0 );
348+ if (!measuresList .isEmpty ()) {
349+
350+ pdf .tittle3Font ();
351+ pdf .addLine ("LINES PER LANGUAGE" );
352+
353+ JSONObject measure = measuresList .getJSONObject (0 );
347354
348- String rawLanguages = measure .getString ("value" );
355+ String rawLanguages = measure .getString ("value" );
349356
350- for (String pair : rawLanguages .split (";" )) {
351- String [] parts = pair .split ("=" );
352- if (parts .length == 2 ) {
353- int lines = Integer .parseInt (parts [1 ]);
354- String percent = String .format ("%.2f%%" , (lines * 100.0 ) / totalLinesOfCode );
355- rows .add (new String [] { parts [0 ], parts [1 ], percent });
357+ for (String pair : rawLanguages .split (";" )) {
358+ String [] parts = pair .split ("=" );
359+ if (parts .length == 2 ) {
360+ int lines = Integer .parseInt (parts [1 ]);
361+ String percent = String .format ("%.2f%%" , (lines * 100.0 ) / totalLinesOfCode );
362+ rows .add (new String [] { parts [0 ], parts [1 ], percent });
363+ }
356364 }
365+ pdf .drawTable (500 , headers , rows );
357366 }
358367
359- pdf .drawTable (500 , headers , rows );
360-
361368 // SECURITY HOTSPOTS
362369
363370 pdf .tittle2Font ();
@@ -367,7 +374,7 @@ public static void main(String[] args) throws IOException {
367374
368375 try {
369376
370- data = fetchDataFromURL (apiUrl , "/api/security_reports/show?standard=sonarsourceSecurity&project=" , authToken , project );
377+ data = fetchDataFromURL (apiUrl , "/api/security_reports/show?standard=sonarsourceSecurity&project=" , authToken , project , branch );
371378 dataArray = data .getJSONArray ("categories" );
372379
373380 } catch (IOException | InterruptedException e ) {
@@ -425,11 +432,8 @@ public static void main(String[] args) throws IOException {
425432
426433 pdf .drawTable (500 , headers , rows );
427434
428- // SECURITY HOTSPOTS LIST
429-
430- pdf .tittle3Font ();
431- pdf .addLine ("SECURITY HOTSPOT LIST" );
432435
436+ // SECURITY HOTSPOTS LIST
433437
434438 try {
435439 int pageIndex = 1 ;
@@ -440,7 +444,8 @@ public static void main(String[] args) throws IOException {
440444 apiUrl ,
441445 String .format ("/api/hotspots/search?status=TO_REVIEW&ps=500&pageIndex=%d&project=" , pageIndex ),
442446 authToken ,
443- project
447+ project ,
448+ branch
444449 );
445450
446451 JSONArray currentPageHotspots = data .getJSONArray ("hotspots" );
@@ -496,13 +501,19 @@ public static void main(String[] args) throws IOException {
496501 }
497502 // Convert map to JSONArray
498503 JSONArray hotspotArray = new JSONArray (hotspotMap .values ());
504+
505+ if (!hotspotArray .isEmpty ()) {
506+ pdf .tittle3Font ();
507+ pdf .addLine ("SECURITY HOTSPOT LIST" );
508+ }
509+
499510 for (int i = 0 ; i < hotspotArray .length (); i ++) {
500511 JSONObject hotspotObject = hotspotArray .getJSONObject (i );
501512 pdf .startBulletEntry (hotspotObject .getString ("message" ));
502513 pdf .addIndentedLine ("Vulnerability Probability" , hotspotObject .getString ("vulnerabilityProbability" ));
503514 pdf .addIndentedLine ("Count" , Integer .toString (hotspotObject .getInt ("count" )));
504515 pdf .addIndentedLine ("Locations" , hotspotObject .getString ("location" ));
505- pdf .addIndentedHyperlink ("Root Cause/How to fix" , apiUrl +"coding_rules?q=" +hotspotObject .getString ("ruleKey" )+"&open=" +hotspotObject .getString ("ruleKey" ),hotspotObject .getString ("ruleKey" ));
516+ pdf .addIndentedHyperlink ("Root Cause/How to fix" , apiUrl +"/ coding_rules?q=" +hotspotObject .getString ("ruleKey" )+"&open=" +hotspotObject .getString ("ruleKey" ),hotspotObject .getString ("ruleKey" ));
506517 }
507518
508519 // -----------------------------
@@ -518,7 +529,7 @@ public static void main(String[] args) throws IOException {
518529
519530 try {
520531
521- data = fetchDataFromURL (apiUrl , "/api/issues/search?types=BUG&facets=severities&componentKeys=" , authToken , project );
532+ data = fetchDataFromURL (apiUrl , "/api/issues/search?types=BUG&facets=severities&componentKeys=" , authToken , project , branch );
522533
523534 } catch (IOException | InterruptedException e ) {
524535 System .err .println ("Error at doing the HTTP petition: " + e .getMessage ());
@@ -530,7 +541,7 @@ public static void main(String[] args) throws IOException {
530541
531542 try {
532543
533- data = fetchDataFromURL (apiUrl , "/api/issues/search?types=VULNERABILITY&facets=severities&componentKeys=" , authToken , project );
544+ data = fetchDataFromURL (apiUrl , "/api/issues/search?types=VULNERABILITY&facets=severities&componentKeys=" , authToken , project , branch );
534545
535546 } catch (IOException | InterruptedException e ) {
536547 System .err .println ("Error at doing the HTTP petition: " + e .getMessage ());
@@ -542,7 +553,7 @@ public static void main(String[] args) throws IOException {
542553
543554 try {
544555
545- data = fetchDataFromURL (apiUrl , "/api/issues/search?types=CODE_SMELL&facets=severities&componentKeys=" , authToken , project );
556+ data = fetchDataFromURL (apiUrl , "/api/issues/search?types=CODE_SMELL&facets=severities&componentKeys=" , authToken , project , branch );
546557
547558 } catch (IOException | InterruptedException e ) {
548559 System .err .println ("Error at doing the HTTP petition: " + e .getMessage ());
@@ -555,8 +566,6 @@ public static void main(String[] args) throws IOException {
555566 pdf .drawTable (500 , headers , rows );
556567
557568 // ISSUES LIST
558- pdf .tittle3Font ();
559- pdf .addLine ("ISSUES LIST" );
560569
561570 try {
562571 int pageIndex = 1 ;
@@ -567,7 +576,8 @@ public static void main(String[] args) throws IOException {
567576 apiUrl ,
568577 String .format ("/api/issues/search?issueStatuses=OPEN&ps=500&pageIndex=%d&componentKeys=" , pageIndex ),
569578 authToken ,
570- project
579+ project ,
580+ branch
571581 );
572582
573583 JSONArray currentPageHotspots = data .getJSONArray ("issues" );
@@ -634,6 +644,11 @@ public static void main(String[] args) throws IOException {
634644 // Map to JsonArray
635645 JSONArray issuesArray = new JSONArray (issuesMap .values ());
636646
647+ if (!issuesArray .isEmpty ()) {
648+ pdf .tittle3Font ();
649+ pdf .addLine ("ISSUES LIST" );
650+ }
651+
637652 for (int i = 0 ; i < issuesArray .length (); i ++) {
638653 JSONObject issuesObject = issuesArray .getJSONObject (i );
639654 pdf .startBulletEntry (issuesObject .getString ("message" ));
0 commit comments