Search in sources :

Example 1 with NewIssue

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

the class WebSensor method saveMetrics.

private static void saveMetrics(SensorContext context, WebSourceCode sourceCode) {
    InputFile inputFile = sourceCode.inputFile();
    saveComplexityDistribution(context, sourceCode);
    for (Map.Entry<Metric<Integer>, Integer> entry : sourceCode.getMeasures().entrySet()) {
        context.<Integer>newMeasure().on(inputFile).forMetric(entry.getKey()).withValue(entry.getValue()).save();
    }
    for (WebIssue issue : sourceCode.getIssues()) {
        NewIssue newIssue = context.newIssue().forRule(issue.ruleKey()).gap(issue.cost());
        Integer line = issue.line();
        NewIssueLocation location = newIssue.newLocation().on(inputFile).message(issue.message());
        if (line != null) {
            location.at(inputFile.selectLine(line));
        }
        newIssue.at(location);
        newIssue.save();
    }
}
Also used : NewIssueLocation(org.sonar.api.batch.sensor.issue.NewIssueLocation) WebIssue(org.sonar.plugins.web.checks.WebIssue) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) Metric(org.sonar.api.measures.Metric) Map(java.util.Map) InputFile(org.sonar.api.batch.fs.InputFile)

Example 2 with NewIssue

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

the class GoSensor method reportIssue.

private void reportIssue(Issue issue, SensorContext context, InputFile inputFile) {
    // TODO improve common rule engine to handle this out of the box
    RuleKey ruleKey = checks.ruleKey(issue.getCheck());
    Objects.requireNonNull(ruleKey, "Rule key not found for " + issue.getCheck().getClass());
    NewIssue newIssue = context.newIssue();
    NewIssueLocation location = newIssue.newLocation().on(inputFile).message(issue.getMessage());
    if (issue.hasLocation()) {
        location.at(newRange(inputFile, issue.getPrimary().from, issue.getPrimary().to));
    }
    newIssue.forRule(ruleKey).at(location).gap(issue.getEffortToFix());
    Arrays.stream(issue.getSecondaries()).forEach(secondary -> newIssue.addLocation(newIssue.newLocation().on(inputFile).at(newRange(inputFile, secondary.from, secondary.to)).message(secondary.description == null ? "" : secondary.description)));
    newIssue.save();
}
Also used : NewIssueLocation(org.sonar.api.batch.sensor.issue.NewIssueLocation) RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue)

Example 3 with NewIssue

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

the class OneQuickFixPerLineSensor method createIssues.

private void createIssues(InputFile file, SensorContext context, String repo) {
    RuleKey ruleKey = RuleKey.of(repo, RULE_KEY);
    for (int line = 1; line <= file.lines(); line++) {
        NewIssue newIssue = context.newIssue();
        newIssue.setQuickFixAvailable(true).forRule(ruleKey).at(newIssue.newLocation().on(file).at(file.selectLine(line)).message("This quick fix issue is generated on each line")).save();
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue)

Example 4 with NewIssue

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

the class RandomAccessSensor method createIssues.

private static void createIssues(InputFile file, SensorContext context) {
    RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY);
    NewIssue newIssue = context.newIssue();
    newIssue.forRule(ruleKey).at(newIssue.newLocation().on(file).at(file.selectLine(1)).message("This issue is generated on each file")).save();
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue)

Example 5 with NewIssue

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

the class CreateIssueByInternalKeySensor method createIssues.

private static void createIssues(InputFile file, SensorContext context) {
    ActiveRule rule = context.activeRules().findByInternalKey(XooRulesDefinition.XOO_REPOSITORY, context.config().get(INTERNAL_KEY_PROPERTY).orElse(null));
    if (rule != null) {
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(rule.ruleKey()).at(newIssue.newLocation().on(file).message("This issue is generated on each file")).save();
    }
}
Also used : NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) ActiveRule(org.sonar.api.batch.rule.ActiveRule)

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