diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-10-10 09:54:29 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-10-10 14:51:58 +0200 |
commit | 37915e1548b5a706b0aed606ee3250e3496850b0 (patch) | |
tree | a0e646c4f1fae129574d8fba3f515fe85cf2b879 /sonar-scanner-engine/src | |
parent | db2093a19f4cda810194cf9c0ab7a6db73b4a4a1 (diff) | |
download | sonarqube-37915e1548b5a706b0aed606ee3250e3496850b0.tar.gz sonarqube-37915e1548b5a706b0aed606ee3250e3496850b0.zip |
SONAR-9929 Filter secondary locations that are in a different file than primary location
Diffstat (limited to 'sonar-scanner-engine/src')
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ModuleIssues.java | 21 | ||||
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ModuleIssuesTest.java | 23 |
2 files changed, 36 insertions, 8 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ModuleIssues.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ModuleIssues.java index 279b003d933..6b386d9691f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ModuleIssues.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ModuleIssues.java @@ -19,8 +19,8 @@ */ package org.sonar.scanner.issue; +import com.google.common.base.Strings; import javax.annotation.concurrent.ThreadSafe; - import org.sonar.api.batch.fs.TextRange; import org.sonar.api.batch.fs.internal.DefaultInputComponent; import org.sonar.api.batch.rule.ActiveRule; @@ -36,8 +36,6 @@ import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReport.IssueLocation; import org.sonar.scanner.report.ReportPublisher; -import com.google.common.base.Strings; - /** * Initialize the issues raised during scan. */ @@ -75,7 +73,7 @@ public class ModuleIssues { return false; } - private static ScannerReport.Issue createReportIssue(Issue issue, int batchId, String ruleName, String activeRuleSeverity) { + private static ScannerReport.Issue createReportIssue(Issue issue, int componentRef, String ruleName, String activeRuleSeverity) { String primaryMessage = Strings.isNullOrEmpty(issue.primaryLocation().message()) ? ruleName : issue.primaryLocation().message(); org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity(); Severity severity = overriddenSeverity != null ? Severity.valueOf(overriddenSeverity.name()) : Severity.valueOf(activeRuleSeverity); @@ -90,7 +88,7 @@ public class ModuleIssues { builder.setMsg(primaryMessage); locationBuilder.setMsg(primaryMessage); - locationBuilder.setComponentRef(batchId); + locationBuilder.setComponentRef(componentRef); TextRange primaryTextRange = issue.primaryLocation().textRange(); if (primaryTextRange != null) { builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange)); @@ -99,11 +97,11 @@ public class ModuleIssues { if (gap != null) { builder.setGap(gap); } - applyFlows(builder, locationBuilder, textRangeBuilder, issue); + applyFlows(componentRef, builder, locationBuilder, textRangeBuilder, issue); return builder.build(); } - private static void applyFlows(ScannerReport.Issue.Builder builder, ScannerReport.IssueLocation.Builder locationBuilder, + private static void applyFlows(int componentRef, ScannerReport.Issue.Builder builder, ScannerReport.IssueLocation.Builder locationBuilder, ScannerReport.TextRange.Builder textRangeBuilder, Issue issue) { ScannerReport.Flow.Builder flowBuilder = ScannerReport.Flow.newBuilder(); for (Flow flow : issue.flows()) { @@ -112,8 +110,15 @@ public class ModuleIssues { } flowBuilder.clear(); for (org.sonar.api.batch.sensor.issue.IssueLocation location : flow.locations()) { + int locationComponentRef = ((DefaultInputComponent) location.inputComponent()).batchId(); + if (locationComponentRef != componentRef) { + // Some analyzers are trying to report cross file secondary locations. The API was designed to support it, but server side is not + // ready to handle it (especially the UI) + // So let's skip them for now (SONAR-9929) + continue; + } locationBuilder.clear(); - locationBuilder.setComponentRef(((DefaultInputComponent) location.inputComponent()).batchId()); + locationBuilder.setComponentRef(locationComponentRef); String message = location.message(); if (message != null) { locationBuilder.setMsg(message); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ModuleIssuesTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ModuleIssuesTest.java index 8bbdee15b59..4ae19f7ef7b 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ModuleIssuesTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ModuleIssuesTest.java @@ -19,6 +19,7 @@ */ package org.sonar.scanner.issue; +import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; @@ -182,6 +183,28 @@ public class ModuleIssuesTest { assertThat(argument.getValue().getMsg()).isEqualTo("Avoid Cycle"); } + // SONAR-9929 Filter secondary locations that are on different files + @Test + public void skip_cross_file_secondary_locations() { + ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); + activeRulesBuilder.create(SQUID_RULE_KEY).setSeverity(Severity.INFO).setName(SQUID_RULE_NAME).activate(); + initModuleIssues(); + + DefaultIssue issue = new DefaultIssue() + .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("Foo")) + .forRule(SQUID_RULE_KEY) + .addFlow(Arrays.asList(new DefaultIssueLocation().on(file).at(file.selectLine(4)).message("Location 1"), + new DefaultIssueLocation().on(new TestInputFileBuilder("foo", "src/Foo2.php").initMetadata("Foo\nBar\nBiz\n").build()).at(file.selectLine(3)).message("Location outside"))); + when(filters.accept(anyString(), any(ScannerReport.Issue.class))).thenReturn(true); + + boolean added = moduleIssues.initAndAddIssue(issue); + + assertThat(added).isTrue(); + ArgumentCaptor<ScannerReport.Issue> argument = ArgumentCaptor.forClass(ScannerReport.Issue.class); + verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.batchId()), argument.capture()); + assertThat(argument.getValue().getFlow(0).getLocationList()).hasSize(1); + } + @Test public void filter_issue() { ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); |