13
13
import org .sonar .api .batch .sensor .Sensor ;
14
14
import org .sonar .api .batch .sensor .SensorContext ;
15
15
import org .sonar .api .batch .sensor .SensorDescriptor ;
16
+ import org .sonar .api .batch .sensor .issue .NewIssue ;
17
+ import org .sonar .api .batch .sensor .issue .NewIssueLocation ;
18
+ import org .sonar .api .rule .RuleKey ;
16
19
import org .sonar .api .utils .log .Logger ;
17
20
import org .sonar .api .utils .log .Loggers ;
18
21
22
+ import fr .cnes .sonarqube .plugins .icode .rules .ICodeRulesDefinition ;
23
+ import fr .cnes .sonarqube .plugins .icode .settings .ICodeLanguageProperties ;
24
+ import fr .cnes .sonarqube .plugins .icode .report .ErrorInterface ;
25
+ import fr .cnes .sonarqube .plugins .icode .report .ReportFunctionRuleInterface ;
26
+ import fr .cnes .sonarqube .plugins .icode .report .ReportInterface ;
27
+ import fr .cnes .sonarqube .plugins .icode .report .ReportModuleRuleInterface ;
19
28
import fr .cnes .sonarqube .plugins .icode .report .XmlReportReader ;
20
29
21
30
/**
@@ -29,12 +38,18 @@ public class ICodeSensor implements Sensor {
29
38
30
39
private static final Logger LOGGER = Loggers .get (ICodeSensor .class );
31
40
32
- /** Report sub directory */
33
- public static final String REPORT_SUBDIR = "reports" ;
34
- /** Report extension */
35
- public static final String REPORT_EXT = ".res.xml" ;
36
- /** project code file patterns */
37
- public static final String EXPECTED_REPORT_INPUT_FILE_TYPES = "*.f,*.f77,*.f90" ;
41
+ private String expectedReportInputFileTypes = null ;
42
+
43
+ private String reportOutExt = null ;
44
+
45
+ private String reportSubdir = null ;
46
+
47
+ // /** Report sub directory */
48
+ // public static final String REPORT_SUBDIR = "reports";
49
+ // /** Report extension */
50
+ // public static final String REPORT_EXT = ".res.xml";
51
+ // /** project code file patterns */
52
+ // public static final String EXPECTED_REPORT_INPUT_FILE_TYPES = "*.f,*.f77,*.f90";
38
53
39
54
@ Override
40
55
public void describe (SensorDescriptor descriptor ) {
@@ -45,13 +60,21 @@ public void describe(SensorDescriptor descriptor) {
45
60
public void execute (SensorContext context ) {
46
61
LOGGER .info ("ICodeSensor is running..." );
47
62
FileSystem fs = context .fileSystem ();
63
+ LOGGER .info ("ICodeSensor : file system base dir = " + fs .baseDir ());
48
64
FilePredicates p = fs .predicates ();
49
- // // only "main" files, but not "tests"
50
- // Iterable<InputFile> files =
51
- // fs.inputFiles(fs.predicates().hasType(InputFile.Type.MAIN));
52
- String [] icodeMatchingPatterns = matchingPatterns ();
53
- Iterable <InputFile > filesC = fs .inputFiles (fs .predicates ().matchesPathPatterns (icodeMatchingPatterns ));
54
- for (InputFile file : filesC ) {
65
+ LOGGER .info ("ICodeSensor : file system base dir = " + fs .hasFiles (p .all ()));
66
+
67
+ // Read Plugin settings
68
+ expectedReportInputFileTypes = context .settings ().getString (ICodeLanguageProperties .EXPECTED_REPORT_INPUT_FILE_TYPES_KEY );
69
+ reportOutExt = context .settings ().getString (ICodeLanguageProperties .REPORT_OUT_EXT_KEY );
70
+ reportSubdir = context .settings ().getString (ICodeLanguageProperties .REPORT_SUBDIR_KEY );
71
+
72
+
73
+ // only "main" files, but not "tests"
74
+ String [] aMatchingPatterns = matchingPatterns ();
75
+ Iterable <InputFile > filesF = fs .inputFiles (fs .predicates ().matchesPathPatterns (aMatchingPatterns ));
76
+ for (InputFile file : filesF ) {
77
+ LOGGER .debug ("ICodeSensor : current input file = " + file .absolutePath ());
55
78
56
79
// Check for report out
57
80
String fileRelativePathNameReportOut = outReportFileName (file );
@@ -66,9 +89,8 @@ public void execute(SensorContext context) {
66
89
* @return all expected file code patterns
67
90
*/
68
91
private String [] matchingPatterns () {
69
- StringBuffer sb = new StringBuffer ();
70
- String patternSeparator = "," ;
71
- String [] res = EXPECTED_REPORT_INPUT_FILE_TYPES .trim ().split (patternSeparator );
92
+ String patternSeparator = ICodeLanguageProperties .FILE_SUFFIXES_SEPARATOR ;
93
+ String [] res = expectedReportInputFileTypes .trim ().split (patternSeparator );
72
94
return res ;
73
95
}
74
96
@@ -78,7 +100,6 @@ private String[] matchingPatterns() {
78
100
* @return relative report file for this input code file
79
101
*/
80
102
protected String outReportFileName (InputFile file ) {
81
- String reportOutExt = REPORT_EXT ;
82
103
return relativeReportFileName (file , reportOutExt );
83
104
}
84
105
@@ -92,7 +113,7 @@ protected String outReportFileName(InputFile file) {
92
113
private String relativeReportFileName (InputFile file , String reportOutExt ) {
93
114
String separator = file .file ().separator ;
94
115
String name = file .file ().getName ();
95
- return REPORT_SUBDIR + separator + name + reportOutExt ;
116
+ return reportSubdir + separator + name + reportOutExt ;
96
117
}
97
118
98
119
/**
@@ -106,7 +127,10 @@ private String relativeReportFileName(InputFile file, String reportOutExt) {
106
127
* @param fileRelativePathNameReportOut
107
128
* name of the expected report file for this input code file
108
129
*/
109
- private void analyseReportOut (SensorContext context , InputFile file , String fileRelativePathNameReportOut ) {
130
+ private void analyseReportOut (
131
+ SensorContext context ,
132
+ InputFile file ,
133
+ String fileRelativePathNameReportOut ) {
110
134
ReportInterface report = null ;
111
135
StringBuffer warningMsgs = new StringBuffer ();
112
136
int nbWarningMsgs = 0 ;
@@ -118,11 +142,10 @@ private void analyseReportOut(SensorContext context, InputFile file, String file
118
142
Path fileReportPath = Paths .get (file .absolutePath ()).getParent ().resolve (fileRelativePathNameReportOut );
119
143
if (existReportFile (fileReportPath )) {
120
144
121
- try {
122
- FileChannel reportFile = FileChannel .open (fileReportPath );
145
+ try (FileChannel reportFile = FileChannel .open (fileReportPath )){
123
146
report = XmlReportReader .parse (fileReportPath );
124
147
long reportFileSize = reportFile .size ();
125
- if (reportFileSize > 0 ) {
148
+ if (reportFileSize == 0 ) {
126
149
errorMsgs .append ("Empty report file : " + fileRelativePathNameReportOut );
127
150
nbErrorMsgs ++;
128
151
}
@@ -140,17 +163,29 @@ private void analyseReportOut(SensorContext context, InputFile file, String file
140
163
}
141
164
// Add a ICode report warning
142
165
if (nbWarningMsgs > 0 ) {
143
- context .<String >newMeasure ().forMetric (ICodeMetrics .REPORT_FILES_WARNING ).on (file )
144
- .withValue (warningMsgs .toString ()).save ();
145
- context .<Integer >newMeasure ().forMetric (ICodeMetrics .NUMBER_OF_WARNINGS ).on (file ).withValue (nbWarningMsgs )
146
- .save ();
166
+ context .<String >newMeasure ()
167
+ .forMetric (ICodeMetrics .REPORT_FILES_WARNING )
168
+ .on (file )
169
+ .withValue (warningMsgs .toString ())
170
+ .save ();
171
+ context .<Integer >newMeasure ()
172
+ .forMetric (ICodeMetrics .NUMBER_OF_WARNINGS )
173
+ .on (file )
174
+ .withValue (nbWarningMsgs )
175
+ .save ();
147
176
}
148
177
// Add a ICode report error
149
178
if (nbErrorMsgs > 0 ) {
150
- context .<String >newMeasure ().forMetric (ICodeMetrics .REPORT_FILES_ERROR ).on (file )
151
- .withValue (errorMsgs .toString ()).save ();
152
- context .<Integer >newMeasure ().forMetric (ICodeMetrics .NUMBER_OF_ERRORS ).on (file ).withValue (nbErrorMsgs )
153
- .save ();
179
+ context .<String >newMeasure ()
180
+ .forMetric (ICodeMetrics .REPORT_FILES_ERROR )
181
+ .on (file )
182
+ .withValue (errorMsgs .toString ())
183
+ .save ();
184
+ context .<Integer >newMeasure ()
185
+ .forMetric (ICodeMetrics .NUMBER_OF_ERRORS )
186
+ .on (file )
187
+ .withValue (nbErrorMsgs )
188
+ .save ();
154
189
}
155
190
if (report != null ) {
156
191
parseReportMeasures (context , file , report );
@@ -899,31 +934,64 @@ private void storeLOCMeasuresSHELL(SensorContext context, InputFile file, double
899
934
* @param report
900
935
*/
901
936
private void parseReportIssues (SensorContext context , InputFile file , ReportInterface report ) {
902
- // Read all report issues
903
- ReportModuleRuleInterface reportModuleRuleInterface = report .getModuleCyclomaticMeasure ();
904
- ReportFunctionRuleInterface [] reportModuleRuleInterfaces = report .getCyclomaticMeasureByFunction ();
937
+ LOGGER .info ("Parse and store report issues (doing...)" );
905
938
906
- // Create issues for this file
907
- if (reportModuleRuleInterface != null ) {
939
+ // Read all report issues
940
+ ErrorInterface [] errors = report .getErrors ();
941
+
942
+ /*
943
+ * // Read all report issues ReportModuleRuleInterface
944
+ * reportModuleRuleInterface = report.getModuleCyclomaticMeasure();
945
+ * ReportFunctionRuleInterface[] reportModuleRuleInterfaces =
946
+ * report.getCyclomaticMeasureByFunction();
947
+ *
948
+ * // Create issues for this file if (reportModuleRuleInterface != null)
949
+ * { InputFile inputFile = file; int lines = inputFile.lines();
950
+ *
951
+ * // Read measure value for each elements of this module for
952
+ * (ReportFunctionRuleInterface currentFunctionRuleInterface :
953
+ * reportModuleRuleInterfaces) { String line =
954
+ * currentFunctionRuleInterface.getLine(); int lineNr =
955
+ * getLineAsInt(line, lines); // RuleKey ruleKey =
956
+ * ICodeRulesDefinition.RULE_CYCLO;//TODO: TBD // NewIssue newIssue =
957
+ * context.newIssue().forRule(ruleKey); // NewIssueLocation location =
958
+ * newIssue.newLocation() // .on(inputFile) //
959
+ * .at(inputFile.selectLine(lineNr > 0 ? lineNr : 1)) //
960
+ * .message(currentFunctionRuleInterface.getValue()); // //
961
+ * newIssue.at(location); // newIssue.save(); //
962
+ * violationsCount++;//TODO: TBD count number of issues } }
963
+ */
964
+ if (errors != null ) {
908
965
InputFile inputFile = file ;
909
- int lines = inputFile .lines ();
910
-
911
- // Read measure value for each elements of this module
912
- for (ReportFunctionRuleInterface currentFunctionRuleInterface : reportModuleRuleInterfaces ) {
913
- String line = currentFunctionRuleInterface .getLine ();
914
- int lineNr = getLineAsInt (line , lines );
915
- // RuleKey ruleKey = ICodeRulesDefinition.RULE_CYCLO;//TODO: TBD
916
- // NewIssue newIssue = context.newIssue().forRule(ruleKey);
917
- // NewIssueLocation location = newIssue.newLocation()
918
- // .on(inputFile)
919
- // .at(inputFile.selectLine(lineNr > 0 ? lineNr : 1))
920
- // .message(currentFunctionRuleInterface.getValue());
921
- //
922
- // newIssue.at(location);
923
- // newIssue.save();
924
- // violationsCount++;//TODO: TBD count number of issues
966
+ for (ErrorInterface error : errors ) {
967
+ String lineString = error .getLineDescriptor ();
968
+ String message = error .getDescription ();
969
+ String externalRuleKey = error .getRuleKey ();
970
+ saveIssue (context , inputFile , lineString , externalRuleKey , message );
925
971
}
926
972
}
973
+ LOGGER .info ("Parse and store report issues (done)" );
974
+ }
975
+
976
+ private void saveIssue (SensorContext context , InputFile inputFile , String lineString , String externalRuleKey ,
977
+ String message ) {
978
+ RuleKey ruleKey = RuleKey .of (ICodeRulesDefinition .getRepositoryKeyForLanguage (), externalRuleKey );
979
+
980
+ LOGGER .info ("externalRuleKey: " + externalRuleKey );
981
+ LOGGER .info ("Repo: " + ICodeRulesDefinition .getRepositoryKeyForLanguage ());
982
+ LOGGER .info ("RuleKey: " + ruleKey );
983
+ NewIssue newIssue = context .newIssue ().forRule (ruleKey );
984
+
985
+ NewIssueLocation primaryLocation = newIssue .newLocation ().on (inputFile ).message (message );
986
+
987
+ int maxLine = inputFile .lines ();
988
+ int iLine = getLineAsInt (lineString , maxLine );
989
+ if (iLine > 0 ) {
990
+ primaryLocation .at (inputFile .selectLine (iLine ));
991
+ }
992
+ newIssue .at (primaryLocation );
993
+
994
+ newIssue .save ();
927
995
}
928
996
929
997
/**
0 commit comments