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 {
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) {
}
}
- 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);
MyBatis.closeQuietly(dbSession);
}
}
+
+ private boolean commitFound() {
+ return lastCommitTimestamp != 0L;
+ }
}
import org.sonar.server.es.EsClient;
import org.sonar.server.exceptions.NotFoundException;
+import javax.annotation.CheckForNull;
import java.util.Date;
import java.util.List;
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)
return new SourceLineDoc(result[0].sourceAsMap()).scmDate();
}
- throw new NotFoundException(String.format("No source found on project '%s'", projectUuid));
+ return null;
}
}
@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"));
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()
.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();
+ }
}