Skip to content

Commit e5cff25

Browse files
committed
ICode Sensor setting by ICode properties
1 parent 11d732f commit e5cff25

File tree

1 file changed

+118
-50
lines changed

1 file changed

+118
-50
lines changed

src/main/java/fr/cnes/sonarqube/plugins/icode/measures/ICodeSensor.java

Lines changed: 118 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@
1313
import org.sonar.api.batch.sensor.Sensor;
1414
import org.sonar.api.batch.sensor.SensorContext;
1515
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;
1619
import org.sonar.api.utils.log.Logger;
1720
import org.sonar.api.utils.log.Loggers;
1821

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;
1928
import fr.cnes.sonarqube.plugins.icode.report.XmlReportReader;
2029

2130
/**
@@ -29,12 +38,18 @@ public class ICodeSensor implements Sensor {
2938

3039
private static final Logger LOGGER = Loggers.get(ICodeSensor.class);
3140

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";
3853

3954
@Override
4055
public void describe(SensorDescriptor descriptor) {
@@ -45,13 +60,21 @@ public void describe(SensorDescriptor descriptor) {
4560
public void execute(SensorContext context) {
4661
LOGGER.info("ICodeSensor is running...");
4762
FileSystem fs = context.fileSystem();
63+
LOGGER.info("ICodeSensor : file system base dir = " + fs.baseDir());
4864
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());
5578

5679
// Check for report out
5780
String fileRelativePathNameReportOut = outReportFileName(file);
@@ -66,9 +89,8 @@ public void execute(SensorContext context) {
6689
* @return all expected file code patterns
6790
*/
6891
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);
7294
return res;
7395
}
7496

@@ -78,7 +100,6 @@ private String[] matchingPatterns() {
78100
* @return relative report file for this input code file
79101
*/
80102
protected String outReportFileName(InputFile file) {
81-
String reportOutExt = REPORT_EXT;
82103
return relativeReportFileName(file, reportOutExt);
83104
}
84105

@@ -92,7 +113,7 @@ protected String outReportFileName(InputFile file) {
92113
private String relativeReportFileName(InputFile file, String reportOutExt) {
93114
String separator = file.file().separator;
94115
String name = file.file().getName();
95-
return REPORT_SUBDIR + separator + name + reportOutExt;
116+
return reportSubdir + separator + name + reportOutExt;
96117
}
97118

98119
/**
@@ -106,7 +127,10 @@ private String relativeReportFileName(InputFile file, String reportOutExt) {
106127
* @param fileRelativePathNameReportOut
107128
* name of the expected report file for this input code file
108129
*/
109-
private void analyseReportOut(SensorContext context, InputFile file, String fileRelativePathNameReportOut) {
130+
private void analyseReportOut(
131+
SensorContext context,
132+
InputFile file,
133+
String fileRelativePathNameReportOut) {
110134
ReportInterface report = null;
111135
StringBuffer warningMsgs = new StringBuffer();
112136
int nbWarningMsgs = 0;
@@ -118,11 +142,10 @@ private void analyseReportOut(SensorContext context, InputFile file, String file
118142
Path fileReportPath = Paths.get(file.absolutePath()).getParent().resolve(fileRelativePathNameReportOut);
119143
if (existReportFile(fileReportPath)) {
120144

121-
try {
122-
FileChannel reportFile = FileChannel.open(fileReportPath);
145+
try (FileChannel reportFile = FileChannel.open(fileReportPath)){
123146
report = XmlReportReader.parse(fileReportPath);
124147
long reportFileSize = reportFile.size();
125-
if (reportFileSize > 0) {
148+
if (reportFileSize == 0) {
126149
errorMsgs.append("Empty report file : " + fileRelativePathNameReportOut);
127150
nbErrorMsgs++;
128151
}
@@ -140,17 +163,29 @@ private void analyseReportOut(SensorContext context, InputFile file, String file
140163
}
141164
// Add a ICode report warning
142165
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();
147176
}
148177
// Add a ICode report error
149178
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();
154189
}
155190
if (report != null) {
156191
parseReportMeasures(context, file, report);
@@ -899,31 +934,64 @@ private void storeLOCMeasuresSHELL(SensorContext context, InputFile file, double
899934
* @param report
900935
*/
901936
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...)");
905938

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) {
908965
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);
925971
}
926972
}
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();
927995
}
928996

929997
/**

0 commit comments

Comments
 (0)