From a39b2c678f82200f1f9516f9a044a3a27a0074c0 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 9 Oct 2017 18:00:04 +0200 Subject: [PATCH] SONAR-9694 Ignore when secondary locations are out of current file for backdating --- .../issue/IssueCreationDateCalculator.java | 12 ++++--- .../scm/ScmInfoRepositoryImpl.java | 2 +- .../IssueCreationDateCalculatorTest.java | 36 +++++++++++++++++-- .../scm/ScmInfoRepositoryImplTest.java | 2 +- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueCreationDateCalculator.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueCreationDateCalculator.java index 1ed3b618c8a..b8075217f9e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueCreationDateCalculator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueCreationDateCalculator.java @@ -23,6 +23,7 @@ import java.time.format.DateTimeFormatter; import java.util.Comparator; import java.util.Date; import java.util.HashSet; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Supplier; @@ -94,7 +95,7 @@ public class IssueCreationDateCalculator extends IssueVisitor { if (pluginKey == null) { return false; } - + ScannerPlugin scannerPlugin = Optional.ofNullable(analysisMetadataHolder.getScannerPluginsByKey().get(pluginKey)) .orElseThrow(illegalStateException("The rule %s is declared to come from plugin %s, but this plugin was not used by scanner.", activeRule.getRuleKey(), pluginKey)); return pluginIsNew(scannerPlugin, lastAnalysisDate) @@ -121,7 +122,7 @@ public class IssueCreationDateCalculator extends IssueVisitor { private Optional getScmChangeDate(Component component, DefaultIssue issue) { return getScmInfo(component) - .flatMap(scmInfo -> getChangeset(scmInfo, issue)) + .flatMap(scmInfo -> getChangeset(component, scmInfo, issue)) .map(IssueCreationDateCalculator::getChangeDate); } @@ -133,7 +134,7 @@ public class IssueCreationDateCalculator extends IssueVisitor { return toJavaUtilOptional(scmInfoRepository.getScmInfo(component)); } - private static Optional getChangeset(ScmInfo scmInfo, DefaultIssue issue) { + private static Optional getChangeset(Component component, ScmInfo scmInfo, DefaultIssue issue) { Set involvedLines = new HashSet<>(); DbIssues.Locations locations = issue.getLocations(); if (locations != null) { @@ -142,7 +143,10 @@ public class IssueCreationDateCalculator extends IssueVisitor { } for (Flow f : locations.getFlowList()) { for (Location l : f.getLocationList()) { - addLines(involvedLines, l.getTextRange()); + if (Objects.equals(l.getComponentId(), component.getUuid())) { + // Ignore locations in other files, since it is currently not very common, and this is hard to load SCM by component UUID. + addLines(involvedLines, l.getTextRange()); + } } } if (!involvedLines.isEmpty()) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImpl.java index abab82d92eb..d6f1a394c09 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImpl.java @@ -45,7 +45,7 @@ public class ScmInfoRepositoryImpl implements ScmInfoRepository { @Override public Optional getScmInfo(Component component) { - requireNonNull(component, "Component cannot be bull"); + requireNonNull(component, "Component cannot be null"); return initializeScmInfoForComponent(component); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IssueCreationDateCalculatorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IssueCreationDateCalculatorTest.java index 8f3cf22439e..3555c069dcf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IssueCreationDateCalculatorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IssueCreationDateCalculatorTest.java @@ -54,6 +54,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class IssueCreationDateCalculatorTest { + private static final String COMPONENT_UUID = "ab12"; + @Rule public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); @@ -78,6 +80,7 @@ public class IssueCreationDateCalculatorTest { issueUpdater = mock(IssueFieldsSetter.class); activeRulesHolder = mock(ActiveRulesHolder.class); component = mock(Component.class); + when(component.getUuid()).thenReturn(COMPONENT_UUID); ruleKey = RuleKey.of("reop", "rule"); issue = mock(DefaultIssue.class); activeRule = mock(ActiveRule.class); @@ -253,8 +256,8 @@ public class IssueCreationDateCalculatorTest { Flow.Builder secondary = Flow.newBuilder().addLocation(Location.newBuilder().setTextRange(range(4, 5))); builder.addFlow(secondary).build(); Flow.Builder flow = Flow.newBuilder() - .addLocation(Location.newBuilder().setTextRange(range(6, 7))) - .addLocation(Location.newBuilder().setTextRange(range(8, 9))); + .addLocation(Location.newBuilder().setTextRange(range(6, 7)).setComponentId(COMPONENT_UUID)) + .addLocation(Location.newBuilder().setTextRange(range(8, 9)).setComponentId(COMPONENT_UUID)); builder.addFlow(flow).build(); when(issue.getLocations()).thenReturn(builder.build()); withScmAt(2, 1200L); @@ -271,6 +274,35 @@ public class IssueCreationDateCalculatorTest { assertChangeOfCreationDateTo(1900L); } + @Test + public void should_ignore_flows_location_outside_current_file_when_backdating() { + analysisMetadataHolder.setBaseAnalysis(null); + currentAnalysisIs(3000L); + + newIssue(); + Builder builder = DbIssues.Locations.newBuilder() + .setTextRange(range(2, 3)); + Flow.Builder secondary = Flow.newBuilder().addLocation(Location.newBuilder().setTextRange(range(4, 5))); + builder.addFlow(secondary).build(); + Flow.Builder flow = Flow.newBuilder() + .addLocation(Location.newBuilder().setTextRange(range(6, 7)).setComponentId(COMPONENT_UUID)) + .addLocation(Location.newBuilder().setTextRange(range(8, 9)).setComponentId("another")); + builder.addFlow(flow).build(); + when(issue.getLocations()).thenReturn(builder.build()); + withScmAt(2, 1200L); + withScmAt(3, 1300L); + withScmAt(4, 1400L); + withScmAt(5, 1500L); + withScmAt(6, 1600L); + withScmAt(7, 1700L); + withScmAt(8, 1800L); + withScmAt(9, 1900L); + + run(); + + assertChangeOfCreationDateTo(1700L); + } + private org.sonar.db.protobuf.DbCommons.TextRange.Builder range(int startLine, int endLine) { return TextRange.newBuilder().setStartLine(startLine).setEndLine(endLine); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImplTest.java index d11354b7a12..6005811b7be 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImplTest.java @@ -120,7 +120,7 @@ public class ScmInfoRepositoryImplTest { @Test public void fail_with_NPE_when_component_is_null() throws Exception { thrown.expect(NullPointerException.class); - thrown.expectMessage("Component cannot be bull"); + thrown.expectMessage("Component cannot be null"); underTest.getScmInfo(null); } -- 2.39.5