Search in sources :

Example 11 with DefaultInputComponent

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

the class MeasuresPublisher method publish.

@Override
public void publish(ScannerReportWriter writer) {
    final ScannerReport.Measure.Builder builder = ScannerReport.Measure.newBuilder();
    for (final InputComponent c : componentStore.all()) {
        DefaultInputComponent component = (DefaultInputComponent) c;
        if (component.isFile()) {
            DefaultInputFile file = (DefaultInputFile) component;
            // Recompute all coverage measures from line data to take into account the possible merge of several reports
            updateCoverageFromLineData(file);
            // Recompute test execution measures from MutableTestPlan to take into account the possible merge of several reports
            updateTestExecutionFromTestPlan(file);
        }
        Iterable<DefaultMeasure<?>> scannerMeasures = measureCache.byComponentKey(component.key());
        if (scannerMeasures.iterator().hasNext()) {
            writer.writeComponentMeasures(component.batchId(), StreamSupport.stream(scannerMeasures.spliterator(), false).map(input -> {
                if (input.value() == null) {
                    throw new IllegalArgumentException(String.format("Measure on metric '%s' and component '%s' has no value, but it's not allowed", input.metric().key(), component.key()));
                }
                builder.clear();
                builder.setMetricKey(input.metric().key());
                setValueAccordingToType(builder, input);
                return builder.build();
            }).collect(Collectors.toList()));
        }
    }
}
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) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent)

Example 12 with DefaultInputComponent

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

the class ComponentsPublisher method recursiveWriteComponent.

/**
   * Writes the tree of components recursively, deep-first. 
   * @return true if component was written (not skipped)
   */
private boolean recursiveWriteComponent(DefaultInputComponent component) {
    Collection<InputComponent> children = componentTree.getChildren(component).stream().filter(c -> recursiveWriteComponent((DefaultInputComponent) c)).collect(Collectors.toList());
    if (shouldSkipComponent(component, children)) {
        return false;
    }
    ScannerReport.Component.Builder builder = ScannerReport.Component.newBuilder();
    // non-null fields
    builder.setRef(component.batchId());
    builder.setType(getType(component));
    // Don't set key on directories and files to save space since it can be deduced from path
    if (component instanceof InputModule) {
        DefaultInputModule inputModule = (DefaultInputModule) component;
        // Here we want key without branch
        builder.setKey(inputModule.key());
        // protocol buffers does not accept null values
        String name = getName(inputModule);
        if (name != null) {
            builder.setName(name);
        }
        String description = getDescription(inputModule);
        if (description != null) {
            builder.setDescription(description);
        }
        writeVersion(inputModule, builder);
    }
    if (component.isFile()) {
        DefaultInputFile file = (DefaultInputFile) component;
        builder.setIsTest(file.type() == InputFile.Type.TEST);
        builder.setLines(file.lines());
        String lang = getLanguageKey(file);
        if (lang != null) {
            builder.setLanguage(lang);
        }
    }
    String path = getPath(component);
    if (path != null) {
        builder.setPath(path);
    }
    for (InputComponent child : children) {
        builder.addChildRef(((DefaultInputComponent) child).batchId());
    }
    writeLinks(component, builder);
    writer.writeComponent(builder.build());
    return true;
}
Also used : DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputFile(org.sonar.api.batch.fs.InputFile) StringUtils(org.apache.commons.lang.StringUtils) InputComponent(org.sonar.api.batch.fs.InputComponent) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) CloseableIterator(org.sonar.core.util.CloseableIterator) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) Collection(java.util.Collection) CoreProperties(org.sonar.api.CoreProperties) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) InputPath(org.sonar.api.batch.fs.InputPath) Collectors(java.util.stream.Collectors) ComponentLinkType(org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType) InputDir(org.sonar.api.batch.fs.InputDir) ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) ComponentType(org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType) ComponentLink(org.sonar.scanner.protocol.output.ScannerReport.ComponentLink) Issue(org.sonar.scanner.protocol.output.ScannerReport.Issue) InputModule(org.sonar.api.batch.fs.InputModule) InputModuleHierarchy(org.sonar.api.batch.fs.internal.InputModuleHierarchy) CheckForNull(javax.annotation.CheckForNull) ProjectDefinition(org.sonar.api.batch.bootstrap.ProjectDefinition) InputComponentTree(org.sonar.api.batch.fs.internal.InputComponentTree) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputComponent(org.sonar.api.batch.fs.InputComponent) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) InputModule(org.sonar.api.batch.fs.InputModule) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputComponent(org.sonar.api.batch.fs.InputComponent)

Example 13 with DefaultInputComponent

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

the class IssuePublisher method initAndAddIssue.

public boolean initAndAddIssue(Issue issue) {
    DefaultInputComponent inputComponent = (DefaultInputComponent) issue.primaryLocation().inputComponent();
    if (noSonar(inputComponent, issue)) {
        return false;
    }
    ActiveRule activeRule = activeRules.find(issue.ruleKey());
    if (activeRule == null) {
        // rule does not exist or is not enabled -> ignore the issue
        return false;
    }
    ScannerReport.Issue rawIssue = createReportIssue(issue, inputComponent.scannerId(), activeRule.severity());
    if (filters.accept(inputComponent, rawIssue)) {
        write(inputComponent.scannerId(), rawIssue);
        return true;
    }
    return false;
}
Also used : ActiveRule(org.sonar.api.batch.rule.ActiveRule) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent)

Example 14 with DefaultInputComponent

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

the class IssuePublisher method applyFlows.

private static void applyFlows(Consumer<ScannerReport.Flow> consumer, ScannerReport.IssueLocation.Builder locationBuilder, ScannerReport.TextRange.Builder textRangeBuilder, Collection<Flow> flows) {
    ScannerReport.Flow.Builder flowBuilder = ScannerReport.Flow.newBuilder();
    for (Flow flow : flows) {
        if (flow.locations().isEmpty()) {
            return;
        }
        flowBuilder.clear();
        for (org.sonar.api.batch.sensor.issue.IssueLocation location : flow.locations()) {
            int locationComponentRef = ((DefaultInputComponent) location.inputComponent()).scannerId();
            locationBuilder.clear();
            locationBuilder.setComponentRef(locationComponentRef);
            String message = location.message();
            if (message != null) {
                locationBuilder.setMsg(message);
            }
            TextRange textRange = location.textRange();
            if (textRange != null) {
                locationBuilder.setTextRange(toProtobufTextRange(textRangeBuilder, textRange));
            }
            flowBuilder.addLocation(locationBuilder.build());
        }
        consumer.accept(flowBuilder.build());
    }
}
Also used : DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) TextRange(org.sonar.api.batch.fs.TextRange) Flow(org.sonar.api.batch.sensor.issue.Issue.Flow)

Example 15 with DefaultInputComponent

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

the class AnalysisResult method highlightingTypeFor.

/**
 * Get highlighting types at a given position in an inputfile
 *
 * @param lineOffset 0-based offset in file
 */
public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) {
    int ref = ((DefaultInputComponent) file).scannerId();
    if (!reader.hasSyntaxHighlighting(ref)) {
        return Collections.emptyList();
    }
    TextPointer pointer = file.newPointer(line, lineOffset);
    List<TypeOfText> result = new ArrayList<>();
    try (CloseableIterator<ScannerReport.SyntaxHighlightingRule> it = reader.readComponentSyntaxHighlighting(ref)) {
        while (it.hasNext()) {
            ScannerReport.SyntaxHighlightingRule rule = it.next();
            TextRange ruleRange = toRange(file, rule.getRange());
            if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) {
                result.add(ScannerReportUtils.toBatchType(rule.getType()));
            }
        }
    } catch (Exception e) {
        throw new IllegalStateException("Can't read syntax highlighting for " + file, e);
    }
    return result;
}
Also used : TypeOfText(org.sonar.api.batch.sensor.highlighting.TypeOfText) TextPointer(org.sonar.api.batch.fs.TextPointer) ArrayList(java.util.ArrayList) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) TextRange(org.sonar.api.batch.fs.TextRange) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent)

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