Skip to content

Commit 0e81174

Browse files
committed
#55 i-Code exceptions are converted to issues
1 parent 8d34fe7 commit 0e81174

File tree

6 files changed

+160
-30
lines changed

6 files changed

+160
-30
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ You can get i-Code CNES on GitHub: [lequal/i-CodeCNES](https://github.com/lequal
2121
- Run an analysis with *sonar-scanner*, *maven*, *gradle*, *msbuild*, etc.
2222

2323
#### Run i-Code automatically
24-
This SonarQube plugin is now able to run automaticcaly an embedded version of i-Code. If you do not specify properties to run i-Code [manually](#run-i-Code-manually) or [from a specific version](#Run-a-specific-i-Code-version-through-sonaricode-plugin), embedded version of i-Code will be executed.
24+
This SonarQube plugin is now able to run automatically an embedded version of i-Code. If you do not specify properties to run i-Code [manually](#run-i-Code-manually) or [from a specific version](#Run-a-specific-i-Code-version-through-sonaricode-plugin), embedded version of i-Code will be executed.
2525

2626
Here is the compatibility matrix of the plugin:
2727

2828
| sonaricode version | embedded i-Code version | supported SonarQube version |
2929
|:------------------:|:-----------------------:|:---------------------------:|
3030
| < 2.0.0 | none | 6.7.x |
31-
| 2.0.0 | 4.0.0 | 7.9 -> 8.1 |
32-
| 2.0.1 | 4.0.0 | 7.9 -> 8.1 |
31+
| 2.0.0 | 4.0.0 | 7.9 -> 8.2 |
32+
| 2.0.1 | 4.0.0 | 7.9 -> 8.2 |
33+
| 2.0.2 | 4.0.0 | 7.9 -> 8.2 |
3334

3435
#### Run i-Code manually
3536
If you need help to run i-Code please refer to the [official user manual](https://github.com/lequal/i-CodeCNES/blob/master/documentation/i-Code%20CNES%20-%20User%20Manual.pdf) or [i-Code issue tracker](https://github.com/lequal/i-CodeCNES/issues).

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>fr.cnes.sonar.plugins</groupId>
77
<artifactId>sonar-icode-cnes-plugin</artifactId>
88
<packaging>sonar-plugin</packaging>
9-
<version>2.0.1</version>
9+
<version>2.0.2</version>
1010

1111
<name>Sonar i-Code CNES plugin</name>
1212

@@ -67,6 +67,7 @@
6767
<sonar.pluginKey>icode</sonar.pluginKey>
6868
<sonar.pluginClass>fr.cnes.sonar.plugins.icode.ICodePlugin</sonar.pluginClass>
6969
<sonar.pluginUrl>https://github.com/lequal/sonar-icode-cnes-plugin</sonar.pluginUrl>
70+
<sonar.pluginSourcesUrl>https://github.com/lequal/sonar-icode-cnes-plugin</sonar.pluginSourcesUrl>
7071
<sonar.pluginOrganizationName>CNES</sonar.pluginOrganizationName>
7172
<sonar.sources>src/main/java</sonar.sources>
7273
<sonar.test>src/test/java</sonar.test>

src/main/java/fr/cnes/sonar/plugins/icode/check/ICodeSensor.java

Lines changed: 127 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@
1616
*/
1717
package fr.cnes.sonar.plugins.icode.check;
1818

19-
import fr.cnes.icode.Analyzer;
19+
import com.google.common.collect.Lists;
20+
import com.google.common.collect.Maps;
21+
import fr.cnes.icode.data.AbstractChecker;
2022
import fr.cnes.icode.data.CheckResult;
2123
import fr.cnes.icode.exception.JFlexException;
24+
import fr.cnes.icode.logger.ICodeLogger;
25+
import fr.cnes.icode.services.checkers.CheckerContainer;
26+
import fr.cnes.icode.services.checkers.CheckerService;
27+
import fr.cnes.icode.services.languages.ILanguage;
2228
import fr.cnes.icode.services.languages.LanguageService;
2329
import fr.cnes.sonar.plugins.icode.exceptions.ICodeException;
2430
import fr.cnes.sonar.plugins.icode.languages.Fortran77Language;
@@ -170,42 +176,140 @@ protected void executeExternalResultsImport(final SensorContext sensorContext) {
170176
*/
171177
private void executeEmbeddedICode(final SensorContext sensorContext) {
172178
// Initialisation of tools for analysis.
173-
final Analyzer analyzer = new Analyzer();
174179
final FileSystem fileSystem = sensorContext.fileSystem();
175180
final FilePredicates predicates = fileSystem.predicates();
176181
final ActiveRules activeRules = sensorContext.activeRules();
177182
final Iterable<InputFile> inputFiles = fileSystem.inputFiles(predicates.hasType(InputFile.Type.MAIN));
178183
final HashSet<File> files = new HashSet<>();
179184
final HashMap<String,InputFile> filesMap = new HashMap<>();
180185

181-
try {
182-
// Gather all files in a Set.
183-
for(final InputFile inputFile : inputFiles) {
184-
files.add(inputFile.file());
185-
filesMap.put(inputFile.file().getPath(), inputFile);
186+
// Gather all files in a Set.
187+
for(final InputFile inputFile : inputFiles) {
188+
files.add(inputFile.file());
189+
filesMap.put(inputFile.file().getPath(), inputFile);
190+
}
191+
192+
// Run all checkers on all files.
193+
final List<CheckResult> results = sonarCheck(files, LanguageService.getLanguagesIds(), null);
194+
195+
// Add each issue to SonarQube.
196+
for(final CheckResult result : results) {
197+
if(isRuleActive(activeRules, result.getName())) { // manage active rules
198+
saveIssue(sensorContext, result);
199+
} else if (ICodeMetricsProcessor.isMetric(result.getName())) { // manage trivial measures
200+
ICodeMetricsProcessor.saveMeasure(sensorContext, result);
201+
} else { // log ignored data
202+
LOGGER.info(String.format(
203+
"An issue for rule '%s' was detected by i-Code but this rule is deactivated in current analysis.",
204+
result.getName()));
186205
}
206+
}
207+
208+
ICodeMetricsProcessor.saveExtraMeasures(sensorContext, filesMap, results);
209+
210+
}
187211

188-
// Run all checkers on all files.
189-
final List<CheckResult> results = analyzer.stableCheck(files, LanguageService.getLanguagesIds(), null);
190-
191-
// Add each issue to SonarQube.
192-
for(final CheckResult result : results) {
193-
if(isRuleActive(activeRules, result.getName())) { // manage active rules
194-
saveIssue(sensorContext, result);
195-
} else if (ICodeMetricsProcessor.isMetric(result.getName())) { // manage trivial measures
196-
ICodeMetricsProcessor.saveMeasure(sensorContext, result);
197-
} else { // log ignored data
198-
LOGGER.info(String.format(
199-
"An issue for rule '%s' was detected by i-Code but this rule is deactivated in current analysis.",
200-
result.getName()));
212+
/**
213+
* <h1>{@link #sonarCheck(Set, List, List)}</h1>
214+
* <p>
215+
* This method apply all rules of the different contributions set in
216+
* parameter except the one excluded. File in parameters are being analyzed
217+
* by each contribution able to handle it or none if it isn't.
218+
* </p>
219+
* <p>
220+
* <strong>Important :</strong> Default configurations to run analysis are
221+
* available when setting parameters.
222+
*
223+
* @param pInputFiles
224+
* to analyze
225+
* @param pLanguageIds
226+
* to include in the analysis. <strong>Set null</strong> to run
227+
* an analysis including all contributions.
228+
* @param pExcludedCheckIds
229+
* rules identifier to exclude from the analysis. <strong>Set
230+
* null</strong> run analysis with every rules.
231+
* @return list of {@link CheckResult} found by the analysis.
232+
*/
233+
public List<CheckResult> sonarCheck(final Set<File> pInputFiles, final List<String> pLanguageIds,
234+
final List<String> pExcludedCheckIds) {
235+
final String methodName = "check";
236+
ICodeLogger.entering(this.getClass().getName(), methodName);
237+
238+
List<String> languageIds = pLanguageIds;
239+
if (languageIds == null) {
240+
languageIds = LanguageService.getLanguagesIds();
241+
}
242+
List<String> excludedCheckIds = pExcludedCheckIds;
243+
if (pExcludedCheckIds == null) {
244+
excludedCheckIds = new ArrayList<>();
245+
}
246+
final List<CheckResult> analysisResultCheckResult = new ArrayList<>();
247+
248+
// Contains checkers by language.
249+
final Map<String,List<CheckerContainer>> checkers = Maps.newHashMap();
250+
// Contains files by language.
251+
final Map<String, List<File>> inputs = Maps.newLinkedHashMap();
252+
// Get languages to check during the analysis.
253+
final List<ILanguage> languages = LanguageService.getLanguages(languageIds);
254+
// Get checkers to run during analysis.
255+
for(final ILanguage language : languages) {
256+
checkers.put(language.getId(), CheckerService.getCheckers(language.getId(), excludedCheckIds));
257+
}
258+
259+
// Sort files by language.
260+
for (final File file : pInputFiles) {
261+
final String languageId = LanguageService.getLanguageId(getFileExtension(file.getAbsolutePath()));
262+
final List<File> tempList = inputs.getOrDefault(languageId, Lists.newArrayList());
263+
tempList.add(file);
264+
inputs.put(languageId, tempList);
265+
}
266+
// For each selected language, run selected checkers on selected files.
267+
for (final ILanguage language : languages) {
268+
for (final File input : inputs.getOrDefault(language.getId(), Lists.newArrayList())) {
269+
for (final CheckerContainer checker : checkers.get(language.getId())) {
270+
try {
271+
AbstractChecker check = checker.getChecker();
272+
check.setInputFile(input);
273+
analysisResultCheckResult.addAll(check.run());
274+
} catch (final Exception e) {
275+
// Set the error message.
276+
final String errorMessage = String.format("Internal i-Code error: exception [%s] thrown while checking [%s] on file [%s] - %s",
277+
e.getClass().getSimpleName(), checker.getName(), input.getPath(), e.getMessage());
278+
// Log the error in i-Code and SonarQube logger.
279+
LOGGER.error(errorMessage, e);
280+
// Create an issue to be displayed in SonarQube.
281+
final CheckResult exception = new CheckResult("Parsing Error", "Parsing Error", input);
282+
exception.setLangageId(language.getId());
283+
exception.setLocation("unknown");
284+
exception.setLine(0);
285+
exception.setMessage(errorMessage);
286+
// Add the exception as a Parsing Error result.
287+
analysisResultCheckResult.add(exception);
288+
}
201289
}
202290
}
291+
}
292+
293+
return analysisResultCheckResult;
294+
}
295+
296+
/**
297+
* Return the file extension without the final point '.'.
298+
*
299+
* @param pFileName
300+
* to retrieve the extension
301+
* @return The extension name of the file
302+
*/
303+
private String getFileExtension(final String pFileName) {
304+
String extension = null;
203305

204-
ICodeMetricsProcessor.saveExtraMeasures(sensorContext, filesMap, results);
306+
final int i = pFileName.lastIndexOf('.');
307+
final int p = Math.max(pFileName.lastIndexOf('/'), pFileName.lastIndexOf('\\'));
205308

206-
} catch (final JFlexException e) {
207-
LOGGER.warn(e.getMessage(), e);
309+
if (i > p) {
310+
extension = pFileName.substring(i + 1);
208311
}
312+
return extension;
209313
}
210314

211315
/**

src/main/resources/rules/icode-f77-rules.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
<key>Parsing Error</key>
55
<name>Parsing Error</name>
66
<internalKey>*</internalKey>
7-
<description>An error occurs when parsing a file.</description>
7+
<description><![CDATA[
8+
<p>
9+
An error occurred when parsing a file. This is an internal error of i-Code CNES analysis.
10+
</p>
11+
<p>
12+
Please, report this error to i-Code CNES maintainers to improve the tool: <a href="https://github.com/lequal/i-CodeCNES/issues">https://github.com/lequal/i-CodeCNES/issues</a>
13+
</p>
14+
]]>
15+
</description>
816
<severity>BLOCKER</severity>
917
<cardinality>SINGLE</cardinality>
1018
<status>READY</status>

src/main/resources/rules/icode-f90-rules.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
<key>Parsing Error</key>
55
<name>Parsing Error</name>
66
<internalKey>*</internalKey>
7-
<description>An error occurs when parsing a file.</description>
7+
<description><![CDATA[
8+
<p>
9+
An error occurred when parsing a file. This is an internal error of i-Code CNES analysis.
10+
</p>
11+
<p>
12+
Please, report this error to i-Code CNES maintainers to improve the tool: <a href="https://github.com/lequal/i-CodeCNES/issues">https://github.com/lequal/i-CodeCNES/issues</a>
13+
</p>
14+
]]>
15+
</description>
816
<severity>BLOCKER</severity>
917
<cardinality>SINGLE</cardinality>
1018
<status>READY</status>

src/main/resources/rules/icode-shell-rules.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
<key>Parsing Error</key>
55
<name>Parsing Error</name>
66
<internalKey>*</internalKey>
7-
<description>An error occurs when parsing a file.</description>
7+
<description><![CDATA[
8+
<p>
9+
An error occurred when parsing a file. This is an internal error of i-Code CNES analysis.
10+
</p>
11+
<p>
12+
Please, report this error to i-Code CNES maintainers to improve the tool: <a href="https://github.com/lequal/i-CodeCNES/issues">https://github.com/lequal/i-CodeCNES/issues</a>
13+
</p>
14+
]]>
15+
</description>
816
<severity>BLOCKER</severity>
917
<cardinality>SINGLE</cardinality>
1018
<status>READY</status>

0 commit comments

Comments
 (0)