Tuesday, November 24, 2020

Create Sonar Plugin - Scanner Basic - Save issue 3

 saving your issues , when you have input file details and rule key information. If you have these you can create a new issue using the context ( org.sonar.api.batch.sensor.SensorContext)



-----------------------------------------------------------------------------------------------


private void saveIssue(final InputFile inputFile, int line, final String externalRuleKey, final String message) {
try {
RuleKey ruleKey = RuleKey.of(getRepositoryKeyForLanguage(Objects.requireNonNull(inputFile.language())), externalRuleKey);
NewIssue newIssue = context.newIssue()
.forRule(ruleKey);
LOG.info(ruleKey.toString() + " RULE KEY");
NewIssueLocation primaryLocation = newIssue.newLocation()
.on(inputFile)
.message(message);
if (line > 0) {
primaryLocation.at(inputFile.selectLine(line));
}
newIssue.at(primaryLocation);
newIssue.save();
LOG.info(newIssue.toString() + " saved");
} catch (Exception ex) {
LOG.error(ex.getLocalizedMessage(), ex);
}
}



No comments:

Post a Comment