Search in sources :

Example 1 with InputComponent

use of org.sonar.api.batch.fs.InputComponent 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 2 with InputComponent

use of org.sonar.api.batch.fs.InputComponent 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 3 with InputComponent

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

the class TestExecutionPublisher method publish.

@Override
public void publish(ScannerReportWriter writer) {
    for (final InputComponent c : componentStore.all()) {
        DefaultInputComponent component = (DefaultInputComponent) c;
        if (component.isFile()) {
            DefaultInputFile file = (DefaultInputFile) component;
            // Recompute test execution measures from MutableTestPlan to take into account the possible merge of several reports
            updateTestExecutionFromTestPlan(file, writer);
        }
    }
}
Also used : DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputComponent(org.sonar.api.batch.fs.InputComponent) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent)

Example 4 with InputComponent

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

the class DefaultSensorStorage method saveMeasure.

private void saveMeasure(InputComponent component, DefaultMeasure<?> measure) {
    if (component.isFile()) {
        DefaultInputFile defaultInputFile = (DefaultInputFile) component;
        defaultInputFile.setPublished(true);
    }
    if (component instanceof InputDir || (component instanceof DefaultInputModule && ((DefaultInputModule) component).definition().getParent() != null)) {
        logOnce(measure.metric().key(), "Storing measures on folders or modules is deprecated. Provided value of metric '{}' is ignored.", measure.metric().key());
        return;
    }
    if (DEPRECATED_METRICS_KEYS.contains(measure.metric().key())) {
        logOnce(measure.metric().key(), "Metric '{}' is deprecated. Provided value is ignored.", measure.metric().key());
        return;
    }
    Metric metric = metricFinder.findByKey(measure.metric().key());
    if (metric == null) {
        throw new UnsupportedOperationException("Unknown metric: " + measure.metric().key());
    }
    if (!measure.isFromCore() && NEWLY_CORE_METRICS_KEYS.contains(measure.metric().key())) {
        logOnce(measure.metric().key(), "Metric '{}' is an internal metric computed by SonarQube/SonarCloud. Provided value is ignored.", measure.metric().key());
        return;
    }
    if (!scannerMetrics.getMetrics().contains(metric)) {
        throw new UnsupportedOperationException("Metric '" + metric.key() + "' should not be computed by a Sensor");
    }
    if (((DefaultInputComponent) component).hasMeasureFor(metric)) {
        throw new UnsupportedOperationException("Can not add the same measure twice on " + component + ": " + measure);
    }
    ((DefaultInputComponent) component).setHasMeasureFor(metric);
    if (metric.key().equals(CoreMetrics.EXECUTABLE_LINES_DATA_KEY)) {
        if (component.isFile()) {
            ((DefaultInputFile) component).setExecutableLines(KeyValueFormat.parseIntInt((String) measure.value()).entrySet().stream().filter(e -> e.getValue() > 0).map(Map.Entry::getKey).collect(Collectors.toSet()));
        } else {
            throw new IllegalArgumentException("Executable lines can only be saved on files");
        }
    }
    reportPublisher.getWriter().appendComponentMeasure(((DefaultInputComponent) component).scannerId(), toReportMeasure(measure));
}
Also used : Metric(org.sonar.api.batch.measure.Metric) ScannerMetrics(org.sonar.core.metric.ScannerMetrics) Block(org.sonar.duplications.block.Block) Measure(org.sonar.api.batch.sensor.measure.Measure) COMMENT_LINES_DATA_KEY(org.sonar.api.measures.CoreMetrics.COMMENT_LINES_DATA_KEY) PmdBlockChunker(org.sonar.duplications.internal.pmd.PmdBlockChunker) NewSignificantCode(org.sonar.api.batch.sensor.code.NewSignificantCode) Loggers(org.sonar.api.utils.log.Loggers) NewCpdTokens(org.sonar.api.batch.sensor.cpd.NewCpdTokens) Map(java.util.Map) Configuration(org.sonar.api.config.Configuration) MetricFinder(org.sonar.api.batch.measure.MetricFinder) InputComponent(org.sonar.api.batch.fs.InputComponent) CloseableIterator(org.sonar.core.util.CloseableIterator) Set(java.util.Set) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) Collectors(java.util.stream.Collectors) CoreMetrics(org.sonar.api.measures.CoreMetrics) Serializable(java.io.Serializable) List(java.util.List) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) ReportPublisher(org.sonar.scanner.report.ReportPublisher) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable) PUBLIC_DOCUMENTED_API_DENSITY_KEY(org.sonar.api.measures.CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY_KEY) Math.max(java.lang.Math.max) DefaultHighlighting(org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) ContextPropertiesCache(org.sonar.scanner.repository.ContextPropertiesCache) SortedMap(java.util.SortedMap) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) TEST_SUCCESS_DENSITY_KEY(org.sonar.api.measures.CoreMetrics.TEST_SUCCESS_DENSITY_KEY) InputFile(org.sonar.api.batch.fs.InputFile) IssuePublisher(org.sonar.scanner.issue.IssuePublisher) BranchConfiguration(org.sonar.scanner.scan.branch.BranchConfiguration) SonarCpdBlockIndex(org.sonar.scanner.cpd.index.SonarCpdBlockIndex) Constants(org.sonar.scanner.protocol.Constants) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) ExternalIssue(org.sonar.api.batch.sensor.issue.ExternalIssue) HashSet(java.util.HashSet) ScannerReportUtils(org.sonar.scanner.report.ScannerReportUtils) AdHocRule(org.sonar.api.batch.sensor.rule.AdHocRule) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) TextRange(org.sonar.api.batch.fs.TextRange) NewSymbolTable(org.sonar.api.batch.sensor.symbol.NewSymbolTable) LINES_KEY(org.sonar.api.measures.CoreMetrics.LINES_KEY) DefaultSignificantCode(org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode) Issue(org.sonar.api.batch.sensor.issue.Issue) FileStructure(org.sonar.scanner.protocol.output.FileStructure) AnalysisError(org.sonar.api.batch.sensor.error.AnalysisError) DefaultCoverage(org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) Collectors.toList(java.util.stream.Collectors.toList) TreeMap(java.util.TreeMap) InputDir(org.sonar.api.batch.fs.InputDir) NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) KeyValueFormat(org.sonar.api.utils.KeyValueFormat) DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) Metric(org.sonar.api.batch.measure.Metric) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputDir(org.sonar.api.batch.fs.InputDir)

Example 5 with InputComponent

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

the class DefaultIndex method addMeasure.

public Measure addMeasure(String key, Measure measure) {
    InputComponent component = componentStore.getByKey(key);
    if (component == null) {
        throw new IllegalStateException("Invalid component key: " + key);
    }
    if (sensorStorage.isDeprecatedMetric(measure.getMetricKey())) {
        // Ignore deprecated metrics
        return measure;
    }
    org.sonar.api.batch.measure.Metric<?> metric = metricFinder.findByKey(measure.getMetricKey());
    if (metric == null) {
        throw new UnsupportedOperationException("Unknown metric: " + measure.getMetricKey());
    }
    DefaultMeasure<?> newMeasure;
    if (Boolean.class.equals(metric.valueType())) {
        newMeasure = new DefaultMeasure<Boolean>().forMetric((Metric<Boolean>) metric).withValue(measure.getValue() != 0.0);
    } else if (Integer.class.equals(metric.valueType())) {
        newMeasure = new DefaultMeasure<Integer>().forMetric((Metric<Integer>) metric).withValue(measure.getValue().intValue());
    } else if (Double.class.equals(metric.valueType())) {
        newMeasure = new DefaultMeasure<Double>().forMetric((Metric<Double>) metric).withValue(measure.getValue());
    } else if (String.class.equals(metric.valueType())) {
        newMeasure = new DefaultMeasure<String>().forMetric((Metric<String>) metric).withValue(measure.getData());
    } else if (Long.class.equals(metric.valueType())) {
        newMeasure = new DefaultMeasure<Long>().forMetric((Metric<Long>) metric).withValue(measure.getValue().longValue());
    } else {
        throw new UnsupportedOperationException("Unsupported type :" + metric.valueType());
    }
    sensorStorage.saveMeasure(component, newMeasure);
    return measure;
}
Also used : InputComponent(org.sonar.api.batch.fs.InputComponent) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) Metric(org.sonar.api.measures.Metric)

Aggregations

InputComponent (org.sonar.api.batch.fs.InputComponent)15 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)6 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)5 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)4 InputFile (org.sonar.api.batch.fs.InputFile)3 DefaultMeasure (org.sonar.api.batch.sensor.measure.internal.DefaultMeasure)3 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)3 HashSet (java.util.HashSet)2 Collectors (java.util.stream.Collectors)2 CheckForNull (javax.annotation.CheckForNull)2 InputDir (org.sonar.api.batch.fs.InputDir)2 InputPath (org.sonar.api.batch.fs.InputPath)2 TrackedIssue (org.sonar.scanner.issue.tracking.TrackedIssue)2 Serializable (java.io.Serializable)1 Math.max (java.lang.Math.max)1 Path (java.nio.file.Path)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1