2424import java .io .PrintStream ;
2525import java .util .Collection ;
2626import java .util .Date ;
27+ import java .util .List ;
28+ import java .util .Arrays ;
2729import java .util .function .Function ;
2830import org .sonarlint .cli .util .Logger ;
2931import org .sonarsource .sonarlint .core .client .api .common .RuleDetails ;
3335
3436public class JsonReport implements Reporter {
3537
36- private static final Logger LOGGER = Logger .get ();
38+ private static final List <String > VALID_RULE_DETAIL_TYPES = Arrays .asList (
39+ "BUG" ,
40+ "VULNERABILITY"
41+ );
3742
3843 JsonReport () {
3944
@@ -45,45 +50,57 @@ public void execute(String projectName, Date date, Collection<Trackable> trackab
4550 Issue issue = trackable .getIssue ();
4651 RuleDetails ruleDetails = ruleDescriptionProducer .apply (issue .getRuleKey ());
4752
48- JsonObject json = new JsonObject ();
49- json .addProperty ("type" , "issue" );
50- json .addProperty ("check_name" , issue .getRuleKey ());
51- json .addProperty ("severity" , ruleDetails .getSeverity ().toLowerCase ());
53+ if (VALID_RULE_DETAIL_TYPES .contains (ruleDetails .getType ())) {
54+ JsonObject json = new JsonObject ();
55+ json .addProperty ("type" , "issue" );
56+ json .addProperty ("check_name" , issue .getRuleKey ());
57+ json .addProperty ("severity" , ruleDetails .getSeverity ().toLowerCase ());
58+ json .addProperty ("description" , issue .getMessage ());
5259
53- JsonArray categories = new JsonArray ();
54- json .add ("categories" , categories );
55- categories .add ("Bug Risk" );
60+ JsonObject content = new JsonObject ();
61+ json .add ("content" , content );
62+ content .addProperty ("body" , ruleDetails .getHtmlDescription ());
63+ // // ruleDetails.getExtendedDescription();
5664
57- json .addProperty ("description" , issue .getMessage ());
65+ JsonObject location = new JsonObject ();
66+ json .add ("location" , location );
5867
59- JsonObject content = new JsonObject ();
60- json .add ("content" , content );
61- content .addProperty ("body" , ruleDetails .getHtmlDescription ());
62- // // ruleDetails.getExtendedDescription();
68+ // Code Climate CLI expects relative path to file
69+ location .addProperty ("path" , issue .getInputFile ().getPath ().replaceFirst ("^/code-read-write/" , "" ));
6370
64- JsonObject location = new JsonObject ();
65- json .add ("location " , location );
71+ JsonObject lines = new JsonObject ();
72+ location .add ("lines " , lines );
6673
67- // Code Climate CLI expects relative path to file
68- location .addProperty ("path " , issue .getInputFile (). getPath (). replaceFirst ( "^/code-read-write/" , "" ));
74+ if ( issue . getStartLine () != null ) {
75+ lines .addProperty ("begin " , issue .getStartLine ( ));
6976
70- JsonObject lines = new JsonObject ();
71- location .add ("lines" , lines );
72-
73- if (issue .getStartLine () != null ) {
74- lines .addProperty ("begin" , issue .getStartLine ());
75-
76- if (issue .getEndLine () != null ) {
77- lines .addProperty ("end" , issue .getEndLine ());
77+ if (issue .getEndLine () != null ) {
78+ lines .addProperty ("end" , issue .getEndLine ());
79+ } else {
80+ lines .addProperty ("end" , 1 );
81+ }
7882 } else {
83+ lines .addProperty ("begin" , 1 );
7984 lines .addProperty ("end" , 1 );
8085 }
81- } else {
82- lines .addProperty ("begin" , 1 );
83- lines .addProperty ("end" , 1 );
84- }
8586
86- System .out .println (json .toString () + "\0 " );
87+ String category ;
88+ switch (ruleDetails .getType ()) {
89+ case "VULNERABILITY" : {
90+ category = "Security" ;
91+ break ;
92+ }
93+ default : {
94+ category = "Bug Risk" ;
95+ break ;
96+ }
97+ }
98+ JsonArray categories = new JsonArray ();
99+ categories .add (category );
100+ json .add ("categories" , categories );
101+
102+ System .out .println (json .toString () + "\0 " );
103+ }
87104 }
88105 }
89106}
0 commit comments