]> source.dussan.org Git - sonarqube.git/commitdiff
'Days since last commit' metric without exceptions if not found in report and index
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 8 Apr 2015 16:22:36 +0000 (18:22 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 8 Apr 2015 16:22:36 +0000 (18:22 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistNumberOfDaysSinceLastCommitStep.java
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceLineIndex.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistNumberOfDaysSinceLastCommitStepTest.java
server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineIndexTest.java
server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistNumberOfDaysSinceLastCommitStepTest/empty.xml [new file with mode: 0644]

index 68cecbd88580f7697888ade37647c25e5edc5555..5929138da9dafd38c604eccbce6d4c2f6781d038 100644 (file)
@@ -33,8 +33,12 @@ import org.sonar.server.computation.measure.MetricCache;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.source.index.SourceLineIndex;
 
+import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
+import java.util.Date;
+
+import static com.google.common.base.Objects.firstNonNull;
 import static com.google.common.base.Preconditions.checkState;
 
 public class PersistNumberOfDaysSinceLastCommitStep implements ComputationStep {
@@ -70,11 +74,14 @@ public class PersistNumberOfDaysSinceLastCommitStep implements ComputationStep {
     int rootComponentRef = context.getReportMetadata().getRootComponentRef();
     recursivelyProcessComponent(context, rootComponentRef);
 
-    if (lastCommitTimestamp == 0L) {
-      lastCommitTimestamp = lastCommitFromIndex(context.getProject().uuid());
+    if (!commitFound()) {
+      Long lastCommitFromIndex = lastCommitFromIndex(context.getProject().uuid());
+      lastCommitTimestamp = firstNonNull(lastCommitFromIndex, lastCommitTimestamp);
     }
 
-    persistNumberOfDaysSinceLastCommit(context);
+    if (commitFound()) {
+      persistNumberOfDaysSinceLastCommit(context);
+    }
   }
 
   private void recursivelyProcessComponent(ComputationContext context, int componentRef) {
@@ -100,12 +107,14 @@ public class PersistNumberOfDaysSinceLastCommitStep implements ComputationStep {
     }
   }
 
-  private long lastCommitFromIndex(String projectUuid) {
-    return sourceLineIndex.lastCommitedDateOnProject(projectUuid).getTime();
+  @CheckForNull
+  private Long lastCommitFromIndex(String projectUuid) {
+    Date lastCommitDate = sourceLineIndex.lastCommitDateOnProject(projectUuid);
+    return lastCommitDate == null ? null : lastCommitDate.getTime();
   }
 
   private void persistNumberOfDaysSinceLastCommit(ComputationContext context) {
-    checkState(lastCommitTimestamp != 0, "The last commit time should exist");
+    checkState(commitFound(), "The last commit time should exist");
 
     long numberOfDaysSinceLastCommit = (system.now() - lastCommitTimestamp) / MILLISECONDS_PER_DAY;
     DbSession dbSession = dbClient.openSession(true);
@@ -119,4 +128,8 @@ public class PersistNumberOfDaysSinceLastCommitStep implements ComputationStep {
       MyBatis.closeQuietly(dbSession);
     }
   }
+
+  private boolean commitFound() {
+    return lastCommitTimestamp != 0L;
+  }
 }
index 78bba2af156b09e3cef810c38599aa6d4b376c3a..60b9efd6af9c40d2768f5ee1231da36737bda689 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.server.es.BaseIndex;
 import org.sonar.server.es.EsClient;
 import org.sonar.server.exceptions.NotFoundException;
 
+import javax.annotation.CheckForNull;
 import java.util.Date;
 import java.util.List;
 
@@ -114,7 +115,8 @@ public class SourceLineIndex extends BaseIndex {
     throw new NotFoundException(String.format("No source found on line %s for file '%s'", line, fileUuid));
   }
 
-  public Date lastCommitedDateOnProject(String projectUuid) {
+  @CheckForNull
+  public Date lastCommitDateOnProject(String projectUuid) {
     SearchRequestBuilder request = getClient().prepareSearch(SourceLineIndexDefinition.INDEX)
       .setTypes(SourceLineIndexDefinition.TYPE)
       .setSize(1)
@@ -128,6 +130,6 @@ public class SourceLineIndex extends BaseIndex {
       return new SourceLineDoc(result[0].sourceAsMap()).scmDate();
     }
 
-    throw new NotFoundException(String.format("No source found on project '%s'", projectUuid));
+    return null;
   }
 }
index 7bf6a9a8561a447764f5b58c686fedea8c166e19..846e9432a4da35d5c3ccc9230d5d8d0f1963cca8 100644 (file)
@@ -102,7 +102,7 @@ public class PersistNumberOfDaysSinceLastCommitStepTest extends BaseStepTest {
   @Test
   public void persist_number_of_days_since_last_commit_from_index() throws Exception {
     Date sixDaysAgo = DateUtils.addDays(new Date(), -6);
-    when(sourceLineIndex.lastCommitedDateOnProject("project-uuid")).thenReturn(sixDaysAgo);
+    when(sourceLineIndex.lastCommitDateOnProject("project-uuid")).thenReturn(sixDaysAgo);
     initReportWithProjectAndFile();
     ComputationContext context = new ComputationContext(new BatchReportReader(dir), ComponentTesting.newProjectDto("project-uuid"));
 
@@ -111,6 +111,16 @@ public class PersistNumberOfDaysSinceLastCommitStepTest extends BaseStepTest {
     db.assertDbUnit(getClass(), "insert-from-index-result.xml", new String[] {"id"}, "project_measures");
   }
 
+  @Test
+  public void no_scm_information_in_report_and_index() throws Exception {
+    initReportWithProjectAndFile();
+    ComputationContext context = new ComputationContext(new BatchReportReader(dir), ComponentTesting.newProjectDto("project-uuid"));
+
+    sut.execute(context);
+
+    db.assertDbUnit(getClass(), "empty.xml");
+  }
+
   private BatchReportWriter initReportWithProjectAndFile() throws IOException {
     BatchReportWriter writer = new BatchReportWriter(dir);
     writer.writeMetadata(BatchReport.Metadata.newBuilder()
index 5a27cd939df57d5833d202f11aab20cf801c1b9c..750b1d987a9ab4b13047a41b59795b1497449165 100644 (file)
@@ -128,8 +128,15 @@ public class SourceLineIndexTest {
         .setLine(24)
         .setFileUuid("file-uuid"));
 
-    Date returnedDate = index.lastCommitedDateOnProject("project-uuid");
+    Date returnedDate = index.lastCommitDateOnProject("project-uuid");
 
     assertThat(returnedDate).isEqualTo(now);
   }
+
+  @Test
+  public void last_commit_null_when_not_found() throws Exception {
+    Date date = index.lastCommitDateOnProject("fake-project-uuid");
+
+    assertThat(date).isNull();
+  }
 }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistNumberOfDaysSinceLastCommitStepTest/empty.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/PersistNumberOfDaysSinceLastCommitStepTest/empty.xml
new file mode 100644 (file)
index 0000000..a3306c7
--- /dev/null
@@ -0,0 +1,2 @@
+<dataset>
+</dataset>
\ No newline at end of file