Search in sources :

Example 1 with TextRange

use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.

the class ModuleIssues method initAndAddIssue.

public boolean initAndAddIssue(Issue issue) {
    DefaultInputComponent inputComponent = (DefaultInputComponent) issue.primaryLocation().inputComponent();
    Rule rule = validateRule(issue);
    ActiveRule activeRule = activeRules.find(issue.ruleKey());
    if (activeRule == null) {
        // rule does not exist or is not enabled -> ignore the issue
        return false;
    }
    String primaryMessage = Strings.isNullOrEmpty(issue.primaryLocation().message()) ? rule.name() : issue.primaryLocation().message();
    org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity();
    Severity severity = overriddenSeverity != null ? Severity.valueOf(overriddenSeverity.name()) : Severity.valueOf(activeRule.severity());
    ScannerReport.Issue.Builder builder = ScannerReport.Issue.newBuilder();
    ScannerReport.IssueLocation.Builder locationBuilder = IssueLocation.newBuilder();
    ScannerReport.TextRange.Builder textRangeBuilder = ScannerReport.TextRange.newBuilder();
    // non-null fields
    builder.setSeverity(severity);
    builder.setRuleRepository(issue.ruleKey().repository());
    builder.setRuleKey(issue.ruleKey().rule());
    builder.setMsg(primaryMessage);
    locationBuilder.setMsg(primaryMessage);
    locationBuilder.setComponentRef(inputComponent.batchId());
    TextRange primaryTextRange = issue.primaryLocation().textRange();
    if (primaryTextRange != null) {
        builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange));
    }
    Double gap = issue.gap();
    if (gap != null) {
        builder.setGap(gap);
    }
    applyFlows(builder, locationBuilder, textRangeBuilder, issue);
    ScannerReport.Issue rawIssue = builder.build();
    if (filters.accept(inputComponent.key(), rawIssue)) {
        write(inputComponent.batchId(), rawIssue);
        return true;
    }
    return false;
}
Also used : Issue(org.sonar.api.batch.sensor.issue.Issue) ActiveRule(org.sonar.api.batch.rule.ActiveRule) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) Severity(org.sonar.scanner.protocol.Constants.Severity) TextRange(org.sonar.api.batch.fs.TextRange) IssueLocation(org.sonar.scanner.protocol.output.ScannerReport.IssueLocation) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) ActiveRule(org.sonar.api.batch.rule.ActiveRule) Rule(org.sonar.api.batch.rule.Rule)

Example 2 with TextRange

use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.

the class DefaultSensorStorage method store.

@Override
public void store(DefaultSymbolTable symbolTable) {
    ScannerReportWriter writer = reportPublisher.getWriter();
    DefaultInputFile inputFile = (DefaultInputFile) symbolTable.inputFile();
    inputFile.setPublish(true);
    int componentRef = inputFile.batchId();
    if (writer.hasComponentData(FileStructure.Domain.SYMBOLS, componentRef)) {
        throw new UnsupportedOperationException("Trying to save symbol table twice for the same file is not supported: " + symbolTable.inputFile().absolutePath());
    }
    final ScannerReport.Symbol.Builder builder = ScannerReport.Symbol.newBuilder();
    final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder();
    writer.writeComponentSymbols(componentRef, symbolTable.getReferencesBySymbol().entrySet().stream().map(input -> {
        builder.clear();
        rangeBuilder.clear();
        TextRange declaration = input.getKey();
        builder.setDeclaration(rangeBuilder.setStartLine(declaration.start().line()).setStartOffset(declaration.start().lineOffset()).setEndLine(declaration.end().line()).setEndOffset(declaration.end().lineOffset()).build());
        for (TextRange reference : input.getValue()) {
            builder.addReference(rangeBuilder.setStartLine(reference.start().line()).setStartOffset(reference.start().lineOffset()).setEndLine(reference.end().line()).setEndOffset(reference.end().lineOffset()).build());
        }
        return builder.build();
    }).collect(Collectors.toList()));
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TextRange(org.sonar.api.batch.fs.TextRange) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter)

Example 3 with TextRange

use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.

the class OneBugIssuePerTestLineSensor method createIssues.

private static void createIssues(InputFile file, SensorContext context, String repo) {
    RuleKey ruleKey = RuleKey.of(repo, RULE_KEY);
    for (int line = 1; line <= file.lines(); line++) {
        TextRange text = file.selectLine(line);
        // do not count empty lines, which can be a pain with end-of-file return
        if (text.end().lineOffset() == 0) {
            continue;
        }
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(ruleKey).at(newIssue.newLocation().on(file).at(text).message("This bug issue is generated on each line of a test file")).save();
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) TextRange(org.sonar.api.batch.fs.TextRange)

Example 4 with TextRange

use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.

the class OneBugIssuePerLineSensor method createIssues.

private static void createIssues(InputFile file, SensorContext context, String repo) {
    RuleKey ruleKey = RuleKey.of(repo, RULE_KEY);
    for (int line = 1; line <= file.lines(); line++) {
        TextRange text = file.selectLine(line);
        // do not count empty lines, which can be a pain with end-of-file return
        if (text.end().lineOffset() == 0) {
            continue;
        }
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(ruleKey).at(newIssue.newLocation().on(file).at(text).message("This bug issue is generated on each line")).save();
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) TextRange(org.sonar.api.batch.fs.TextRange)

Example 5 with TextRange

use of org.sonar.api.batch.fs.TextRange in project sonarqube by SonarSource.

the class ExternalIssueImporterTest method import_issue_with_complete_primary_location.

@Test
public void import_issue_with_complete_primary_location() {
    ReportParser.TextRange input = new ReportParser.TextRange();
    input.startLine = 1;
    input.startColumn = 4;
    input.endLine = 2;
    input.endColumn = 3;
    runOn(newIssue(input));
    assertThat(context.allExternalIssues()).hasSize(1);
    ExternalIssue output = context.allExternalIssues().iterator().next();
    assertSameRange(input, output.primaryLocation().textRange());
}
Also used : ExternalIssue(org.sonar.api.batch.sensor.issue.ExternalIssue) TextRange(org.sonar.api.batch.fs.TextRange) Test(org.junit.Test)

Aggregations

TextRange (org.sonar.api.batch.fs.TextRange)25 Test (org.junit.Test)6 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)4 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)4 RuleKey (org.sonar.api.rule.RuleKey)4 TextPointer (org.sonar.api.batch.fs.TextPointer)3 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)3 ExternalIssue (org.sonar.api.batch.sensor.issue.ExternalIssue)3 DefaultSymbolTable (org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable)3 Severity (org.sonar.scanner.protocol.Constants.Severity)3 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)3 IssueLocation (org.sonar.scanner.protocol.output.ScannerReport.IssueLocation)3 ArrayList (java.util.ArrayList)2 TypeOfText (org.sonar.api.batch.sensor.highlighting.TypeOfText)2 Issue (org.sonar.api.batch.sensor.issue.Issue)2 Flow (org.sonar.api.batch.sensor.issue.Issue.Flow)2 ScannerReportWriter (org.sonar.scanner.protocol.output.ScannerReportWriter)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 Map (java.util.Map)1 Set (java.util.Set)1