]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11151 Only cache new lines from the report
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 15 Aug 2018 08:19:50 +0000 (10:19 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 19 Sep 2018 08:51:39 +0000 (10:51 +0200)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepository.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepositoryTest.java

index 38e14292a97861ae1028b7a965e59bf24ff505ae..fb3749491df31e9642d85b740f68dda521887f03 100644 (file)
@@ -38,7 +38,7 @@ public class NewLinesRepository {
   private final AnalysisMetadataHolder analysisMetadataHolder;
   private final ScmInfoRepository scmInfoRepository;
   private final PeriodHolder periodHolder;
-  private final Map<Component, Optional<Set<Integer>>> changedLinesCache = new HashMap<>();
+  private final Map<Component, Optional<Set<Integer>>> reportChangedLinesCache = new HashMap<>();
 
   public NewLinesRepository(BatchReportReader reportReader, AnalysisMetadataHolder analysisMetadataHolder, PeriodHolder periodHolder, ScmInfoRepository scmInfoRepository) {
     this.reportReader = reportReader;
@@ -48,14 +48,10 @@ public class NewLinesRepository {
   }
 
   public boolean newLinesAvailable() {
-    return periodHolder.hasPeriod();
+    return isPullRequestOrShortLivedBranch() || periodHolder.hasPeriod();
   }
 
   public Optional<Set<Integer>> getNewLines(Component component) {
-    return changedLinesCache.computeIfAbsent(component, this::computeNewLines);
-  }
-
-  private Optional<Set<Integer>> computeNewLines(Component component) {
     Optional<Set<Integer>> reportChangedLines = getChangedLinesFromReport(component);
     if (reportChangedLines.isPresent()) {
       return reportChangedLines;
@@ -98,11 +94,19 @@ public class NewLinesRepository {
   }
 
   private Optional<Set<Integer>> getChangedLinesFromReport(Component component) {
-    if (analysisMetadataHolder.isPullRequest() || analysisMetadataHolder.isShortLivingBranch()) {
-      return reportReader.readComponentChangedLines(component.getReportAttributes().getRef())
-        .map(c -> new HashSet<>(c.getLineList()));
+    if (isPullRequestOrShortLivedBranch()) {
+      return reportChangedLinesCache.computeIfAbsent(component, this::readFromReport);
     }
 
     return Optional.empty();
   }
+
+  private boolean isPullRequestOrShortLivedBranch() {
+    return analysisMetadataHolder.isPullRequest() || analysisMetadataHolder.isShortLivingBranch();
+  }
+
+  private Optional<Set<Integer>> readFromReport(Component component) {
+    return reportReader.readComponentChangedLines(component.getReportAttributes().getRef())
+      .map(c -> new HashSet<>(c.getLineList()));
+  }
 }
index 2a02b3473a0d6dc94cba63e3aea918dfacb9dc36..837b11a0592e6d0ba44fc9e83436cb5819762686 100644 (file)
@@ -56,7 +56,6 @@ public class NewLinesRepositoryTest {
 
   @Test
   public void load_new_lines_from_report_if_available_and_pullrequest() {
-    periodHolder.setPeriod(new Period("", null, Long.MAX_VALUE, ""));
     setPullRequest();
     createChangedLinesInReport(1, 2, 5);
 
@@ -80,7 +79,7 @@ public class NewLinesRepositoryTest {
   }
 
   @Test
-  public void return_empty_if_no_period() {
+  public void return_empty_if_no_period_and_not_pullrequest() {
     periodHolder.setPeriod(null);
 
     // even though we have lines in the report and scm data, nothing should be returned since we have no period