Search in sources :

Example 26 with NewIssue

use of org.sonar.api.batch.sensor.issue.NewIssue in project sonar-web by SonarSource.

the class HtmlSensor method saveMetrics.

private static void saveMetrics(SensorContext context, HtmlSourceCode sourceCode) {
    InputFile inputFile = sourceCode.inputFile();
    for (Map.Entry<Metric<Integer>, Integer> entry : sourceCode.getMeasures().entrySet()) {
        context.<Integer>newMeasure().on(inputFile).forMetric(entry.getKey()).withValue(entry.getValue()).save();
    }
    for (HtmlIssue issue : sourceCode.getIssues()) {
        NewIssue newIssue = context.newIssue().forRule(issue.ruleKey()).gap(issue.cost());
        NewIssueLocation location = locationForIssue(inputFile, issue, newIssue);
        newIssue.at(location);
        newIssue.save();
    }
}
Also used : NewIssueLocation(org.sonar.api.batch.sensor.issue.NewIssueLocation) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) Metric(org.sonar.api.measures.Metric) PreciseHtmlIssue(org.sonar.plugins.html.checks.PreciseHtmlIssue) HtmlIssue(org.sonar.plugins.html.checks.HtmlIssue) Map(java.util.Map) InputFile(org.sonar.api.batch.fs.InputFile)

Example 27 with NewIssue

use of org.sonar.api.batch.sensor.issue.NewIssue in project sonarlint-core by SonarSource.

the class GlobalSensor method execute.

@Override
public void execute(final SensorContext context) {
    long timeBefore = clock.millis();
    RuleKey globalRuleKey = RuleKey.of(GlobalRulesDefinition.KEY, GlobalRulesDefinition.RULE_KEY);
    ActiveRule activeGlobalRule = context.activeRules().find(globalRuleKey);
    if (activeGlobalRule != null) {
        Stream.of("stringParam", "textParam", "intParam", "boolParam", "floatParam", "enumParam", "enumListParam", "multipleIntegersParam").map(k -> Arrays.asList(k, activeGlobalRule.param(k))).forEach(kv -> LOGGER.info("Param {} has value {}", kv.get(0), kv.get(1)));
    } else {
        LOGGER.error("Rule is not active");
    }
    for (InputFile f : context.fileSystem().inputFiles(context.fileSystem().predicates().all())) {
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(globalRuleKey).at(newIssue.newLocation().on(f).message("Issue number " + GlobalExtension.getInstance().getAndInc())).save();
    }
    long timeAfter = clock.millis();
    LOGGER.info(String.format("Executed Global Sensor in %d ms", timeAfter - timeBefore));
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) ActiveRule(org.sonar.api.batch.rule.ActiveRule) Loggers(org.sonar.api.utils.log.Loggers) Arrays(java.util.Arrays) Stream(java.util.stream.Stream) RuleKey(org.sonar.api.rule.RuleKey) Clock(java.time.Clock) Sensor(org.sonar.api.batch.sensor.Sensor) SensorContext(org.sonar.api.batch.sensor.SensorContext) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) Logger(org.sonar.api.utils.log.Logger) SensorDescriptor(org.sonar.api.batch.sensor.SensorDescriptor) RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) ActiveRule(org.sonar.api.batch.rule.ActiveRule) InputFile(org.sonar.api.batch.fs.InputFile)

Example 28 with NewIssue

use of org.sonar.api.batch.sensor.issue.NewIssue in project sonarlint-core by SonarSource.

the class OneIssuePerLineSensor method execute.

@Override
public void execute(final SensorContext context) {
    for (InputFile f : context.fileSystem().inputFiles(context.fileSystem().predicates().all())) {
        for (int i = 1; i < f.lines(); i++) {
            NewIssue newIssue = context.newIssue();
            newIssue.forRule(RuleKey.of(FooLintRulesDefinition.KEY, "ExampleRule1")).at(newIssue.newLocation().on(f).at(f.selectLine(i)).message("Issue at line " + i)).save();
        }
    }
}
Also used : NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)28 RuleKey (org.sonar.api.rule.RuleKey)15 InputFile (org.sonar.api.batch.fs.InputFile)5 NewIssueLocation (org.sonar.api.batch.sensor.issue.NewIssueLocation)5 TextRange (org.sonar.api.batch.fs.TextRange)4 Map (java.util.Map)3 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 ActiveRule (org.sonar.api.batch.rule.ActiveRule)2 Sensor (org.sonar.api.batch.sensor.Sensor)2 SensorContext (org.sonar.api.batch.sensor.SensorContext)2 SensorDescriptor (org.sonar.api.batch.sensor.SensorDescriptor)2 Metric (org.sonar.api.measures.Metric)2 Clock (java.time.Clock)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Objects (java.util.Objects)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1