Search in sources :

Example 11 with InputComponent

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

the class JSONReport method writeJsonIssues.

private void writeJsonIssues(JsonWriter json, Set<RuleKey> ruleKeys, Set<String> logins) throws IOException {
    json.name("issues").beginArray();
    for (TrackedIssue issue : getIssues()) {
        if (issue.resolution() == null) {
            InputComponent component = componentStore.getByKey(issue.componentKey());
            String componentKey = getModule(component).definition().getKeyWithBranch();
            if (component instanceof InputPath) {
                componentKey = ComponentKeys.createEffectiveKey(componentKey, (InputPath) component);
            }
            json.beginObject().prop("key", issue.key()).prop("component", componentKey).prop("line", issue.startLine()).prop("startLine", issue.startLine()).prop("startOffset", issue.startLineOffset()).prop("endLine", issue.endLine()).prop("endOffset", issue.endLineOffset()).prop("message", issue.getMessage()).prop("severity", issue.severity()).prop("rule", issue.getRuleKey().toString()).prop("status", issue.status()).prop("resolution", issue.resolution()).prop("isNew", issue.isNew()).prop("assignee", issue.assignee()).prop("effortToFix", issue.gap()).propDateTime("creationDate", issue.creationDate());
            if (!StringUtils.isEmpty(issue.assignee())) {
                logins.add(issue.assignee());
            }
            json.endObject();
            ruleKeys.add(issue.getRuleKey());
        }
    }
    json.endArray();
}
Also used : TrackedIssue(org.sonar.scanner.issue.tracking.TrackedIssue) InputPath(org.sonar.api.batch.fs.InputPath) InputComponent(org.sonar.api.batch.fs.InputComponent)

Example 12 with InputComponent

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

the class DefaultIndexTest method shouldGetHierarchy.

@Test
public void shouldGetHierarchy() {
    InputComponent component = new DefaultInputModule("module1");
    InputFile file1 = new TestInputFileBuilder("module1", "src/org/foo/Bar.java").build();
    when(componentStore.getByKey("module1")).thenReturn(component);
    when(componentStore.getByKey("module1:src/org/foo/Bar.java")).thenReturn(file1);
    when(componentTree.getParent(file1)).thenReturn(component);
    when(componentTree.getChildren(component)).thenReturn(Collections.singleton(file1));
    assertThat(index.getParent("module1:src/org/foo/Bar.java").getKey()).isEqualTo("module1");
    assertThat(index.getParent("module1")).isNull();
    assertThat(index.getChildren("module1")).containsOnly(File.create("src/org/foo/Bar.java"));
    assertThat(index.getChildren("module1:src/org/foo/Bar.java")).isEmpty();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputComponent(org.sonar.api.batch.fs.InputComponent) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 13 with InputComponent

use of org.sonar.api.batch.fs.InputComponent in project sonarlint-core by SonarSource.

the class DefaultSensorStorage method store.

@Override
public void store(Issue issue) {
    InputComponent inputComponent = issue.primaryLocation().inputComponent();
    DefaultRule rule = validateRule(issue);
    ActiveRule activeRule = activeRules.find(issue.ruleKey());
    if (activeRule == null) {
        // rule does not exist or is not enabled -> ignore the issue
        return;
    }
    String primaryMessage = Strings.isNullOrEmpty(issue.primaryLocation().message()) ? rule.name() : issue.primaryLocation().message();
    org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity();
    String severity = overriddenSeverity != null ? overriddenSeverity.name() : activeRule.severity();
    String type = rule.type();
    DefaultClientIssue newIssue = new DefaultClientIssue(severity, type, activeRule, rules.find(activeRule.ruleKey()), primaryMessage, issue.primaryLocation().textRange(), inputComponent.isFile() ? ((SonarLintInputFile) inputComponent).getClientInputFile() : null, issue.flows());
    if (filters.accept(inputComponent, newIssue)) {
        issueListener.handle(newIssue);
    }
}
Also used : DefaultRule(org.sonar.api.batch.rule.internal.DefaultRule) SonarLintInputFile(org.sonarsource.sonarlint.core.container.analysis.filesystem.SonarLintInputFile) InputComponent(org.sonar.api.batch.fs.InputComponent) ActiveRule(org.sonar.api.batch.rule.ActiveRule) DefaultClientIssue(org.sonarsource.sonarlint.core.analyzer.issue.DefaultClientIssue)

Example 14 with InputComponent

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

the class EnforceIssuesFilter method accept.

@Override
public boolean accept(FilterableIssue issue, IssueFilterChain chain) {
    boolean atLeastOneRuleMatched = false;
    boolean atLeastOnePatternFullyMatched = false;
    IssuePattern matchingPattern = null;
    for (IssuePattern pattern : multicriteriaPatterns) {
        if (ruleMatches(pattern, issue.ruleKey())) {
            atLeastOneRuleMatched = true;
            InputComponent component = ((DefaultFilterableIssue) issue).getComponent();
            if (component.isFile()) {
                DefaultInputFile file = (DefaultInputFile) component;
                if (pattern.matchFile(file.getProjectRelativePath())) {
                    atLeastOnePatternFullyMatched = true;
                    matchingPattern = pattern;
                } else if (pattern.matchFile(file.getModuleRelativePath())) {
                    warnOnceDeprecatedIssuePattern("Specifying module-relative paths at project level in property '" + IssueInclusionPatternInitializer.CONFIG_KEY + "' is deprecated. " + "To continue matching files like '" + file.getProjectRelativePath() + "', update this property so that patterns refer to project-relative paths.");
                    atLeastOnePatternFullyMatched = true;
                    matchingPattern = pattern;
                }
            }
        }
    }
    if (atLeastOneRuleMatched) {
        if (atLeastOnePatternFullyMatched) {
            LOG.debug("Issue '{}' enforced by pattern '{}'", issue, matchingPattern);
        }
        return atLeastOnePatternFullyMatched;
    } else {
        return chain.accept(issue);
    }
}
Also used : InputComponent(org.sonar.api.batch.fs.InputComponent) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) IssuePattern(org.sonar.scanner.issue.ignore.pattern.IssuePattern) DefaultFilterableIssue(org.sonar.scanner.issue.DefaultFilterableIssue)

Example 15 with InputComponent

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

the class AbstractDefaultIssue method rewriteLocation.

private DefaultIssueLocation rewriteLocation(DefaultIssueLocation location) {
    InputComponent component = location.inputComponent();
    Optional<Path> dirOrModulePath = Optional.empty();
    if (component instanceof DefaultInputDir) {
        DefaultInputDir dirComponent = (DefaultInputDir) component;
        dirOrModulePath = Optional.of(project.getBaseDir().relativize(dirComponent.path()));
    } else if (component instanceof DefaultInputModule && !Objects.equals(project.key(), component.key())) {
        DefaultInputModule moduleComponent = (DefaultInputModule) component;
        dirOrModulePath = Optional.of(project.getBaseDir().relativize(moduleComponent.getBaseDir()));
    }
    if (dirOrModulePath.isPresent()) {
        String path = PathUtils.sanitize(dirOrModulePath.get().toString());
        DefaultIssueLocation fixedLocation = new DefaultIssueLocation();
        fixedLocation.on(project);
        StringBuilder fullMessage = new StringBuilder();
        if (path != null && !path.isEmpty()) {
            fullMessage.append("[").append(path).append("] ");
        }
        fullMessage.append(location.message());
        fixedLocation.message(fullMessage.toString());
        return fixedLocation;
    } else {
        return location;
    }
}
Also used : Path(java.nio.file.Path) InputComponent(org.sonar.api.batch.fs.InputComponent) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) DefaultInputDir(org.sonar.api.batch.fs.internal.DefaultInputDir)

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