]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9694 Ignore when secondary locations are out of current file for backdating
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 9 Oct 2017 16:00:04 +0000 (18:00 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 10 Oct 2017 12:50:38 +0000 (14:50 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueCreationDateCalculator.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImpl.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IssueCreationDateCalculatorTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImplTest.java

index 1ed3b618c8a50f197d9f78408e5ea4d91ecef8e2..b8075217f9e6363cb4ad556f5ce6b2471d54a002 100644 (file)
@@ -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<Date> 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<Changeset> getChangeset(ScmInfo scmInfo, DefaultIssue issue) {
+  private static Optional<Changeset> getChangeset(Component component, ScmInfo scmInfo, DefaultIssue issue) {
     Set<Integer> 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()) {
index abab82d92eb36d2b13e713ce8d5a421297fce1dc..d6f1a394c097940a33f6bb20d32cbfe55334dd95 100644 (file)
@@ -45,7 +45,7 @@ public class ScmInfoRepositoryImpl implements ScmInfoRepository {
 
   @Override
   public Optional<ScmInfo> getScmInfo(Component component) {
-    requireNonNull(component, "Component cannot be bull");
+    requireNonNull(component, "Component cannot be null");
     return initializeScmInfoForComponent(component);
   }
 
index 8f3cf22439e82af33a12233010e468418c735402..3555c069dcff3a9a53675dd306f5e2a69e89905c 100644 (file)
@@ -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);
   }
index d11354b7a12f4e9f9fa8b5b0a6d570f575b9328c..6005811b7be6b52c659db3bff8e0f116a4046201 100644 (file)
@@ -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);
   }