Search in sources :

Example 1 with DefaultInputComponent

use of org.sonar.api.batch.fs.internal.DefaultInputComponent 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 DefaultInputComponent

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

the class IssueTransition method execute.

public void execute() {
    if (localIssueTracking != null) {
        localIssueTracking.init();
    }
    ScannerReportReader reader = new ScannerReportReader(reportPublisher.getReportDir());
    int nbComponents = inputComponentStore.all().size();
    if (nbComponents == 0) {
        return;
    }
    ProgressReport progressReport = new ProgressReport("issue-tracking-report", TimeUnit.SECONDS.toMillis(10));
    progressReport.start("Performing issue tracking");
    int count = 0;
    try {
        for (InputComponent component : inputComponentStore.all()) {
            trackIssues(reader, (DefaultInputComponent) component);
            count++;
            progressReport.message(count + "/" + nbComponents + " components tracked");
        }
    } finally {
        progressReport.stop(count + "/" + nbComponents + " components tracked");
    }
}
Also used : ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputComponent(org.sonar.api.batch.fs.InputComponent) ProgressReport(org.sonar.scanner.util.ProgressReport)

Example 3 with DefaultInputComponent

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

the class TestExecutionAndCoveragePublisher method publish.

@Override
public void publish(ScannerReportWriter writer) {
    final ScannerReport.Test.Builder testBuilder = ScannerReport.Test.newBuilder();
    final ScannerReport.CoverageDetail.Builder builder = ScannerReport.CoverageDetail.newBuilder();
    final ScannerReport.CoverageDetail.CoveredFile.Builder coveredBuilder = ScannerReport.CoverageDetail.CoveredFile.newBuilder();
    for (final InputComponent c : componentStore.all()) {
        DefaultInputComponent component = (DefaultInputComponent) c;
        final MutableTestPlan testPlan = testPlanBuilder.loadPerspective(MutableTestPlan.class, component);
        if (testPlan == null || Iterables.isEmpty(testPlan.testCases())) {
            continue;
        }
        final Set<String> testNamesWithCoverage = new HashSet<>();
        writer.writeTests(component.batchId(), StreamSupport.stream(testPlan.testCases().spliterator(), false).map(testCase -> toProtobufTest(testBuilder, testNamesWithCoverage, testCase)).collect(toList()));
        writer.writeCoverageDetails(component.batchId(), testNamesWithCoverage.stream().map(testName -> toProtobufCoverageDetails(builder, coveredBuilder, testPlan, testName)).collect(toList()));
    }
}
Also used : CoverageDetail(org.sonar.scanner.protocol.output.ScannerReport.CoverageDetail) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputComponent(org.sonar.api.batch.fs.InputComponent) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) MutableTestPlan(org.sonar.api.test.MutableTestPlan) Test(org.sonar.scanner.protocol.output.ScannerReport.Test) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) HashSet(java.util.HashSet)

Example 4 with DefaultInputComponent

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

the class TestExecutionAndCoveragePublisher method toProtobufCoverageDetails.

private CoverageDetail toProtobufCoverageDetails(final ScannerReport.CoverageDetail.Builder builder, final ScannerReport.CoverageDetail.CoveredFile.Builder coveredBuilder, final MutableTestPlan testPlan, String testName) {
    // Take first test with provided name
    MutableTestCase testCase = testPlan.testCasesByName(testName).iterator().next();
    builder.clear();
    builder.setTestName(testName);
    for (CoverageBlock block : testCase.coverageBlocks()) {
        coveredBuilder.clear();
        DefaultInputComponent c = (DefaultInputComponent) componentStore.getByKey(((DefaultTestable) block.testable()).inputFile().key());
        coveredBuilder.setFileRef(c.batchId());
        for (int line : block.lines()) {
            coveredBuilder.addCoveredLine(line);
        }
        builder.addCoveredFile(coveredBuilder.build());
    }
    return builder.build();
}
Also used : MutableTestCase(org.sonar.api.test.MutableTestCase) CoverageBlock(org.sonar.api.test.CoverageBlock) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent)

Example 5 with DefaultInputComponent

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

the class DefaultComponentTreeTest method test.

@Test
public void test() {
    DefaultInputComponent root = new DefaultInputModule("root");
    DefaultInputComponent mod1 = new DefaultInputModule("mod1");
    DefaultInputComponent mod2 = new DefaultInputModule("mod2");
    DefaultInputComponent mod3 = new DefaultInputModule("mod3");
    DefaultInputComponent mod4 = new DefaultInputModule("mod4");
    tree.index(mod1, root);
    tree.index(mod2, mod1);
    tree.index(mod3, root);
    tree.index(mod4, root);
    assertThat(tree.getChildren(root)).containsOnly(mod1, mod3, mod4);
    assertThat(tree.getChildren(mod4)).isEmpty();
    assertThat(tree.getChildren(mod1)).containsOnly(mod2);
    assertThat(tree.getParent(mod4)).isEqualTo(root);
    assertThat(tree.getParent(mod2)).isEqualTo(mod1);
    assertThat(tree.getParent(mod1)).isEqualTo(root);
    assertThat(tree.getParent(root)).isNull();
}
Also used : DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) Test(org.junit.Test)

Aggregations

DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)15 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)7 InputComponent (org.sonar.api.batch.fs.InputComponent)6 TextRange (org.sonar.api.batch.fs.TextRange)4 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)4 InputFile (org.sonar.api.batch.fs.InputFile)3 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 InputDir (org.sonar.api.batch.fs.InputDir)2 ActiveRule (org.sonar.api.batch.rule.ActiveRule)2 Issue (org.sonar.api.batch.sensor.issue.Issue)2 DefaultMeasure (org.sonar.api.batch.sensor.measure.internal.DefaultMeasure)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Serializable (java.io.Serializable)1 Math.max (java.lang.Math.max)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Map (java.util.Map)1