}
private ProjectAnalysis createProjectAnalysis(CeTask.Status status) {
+ Long analysisDate = getAnalysisDate();
return new ProjectAnalysis(
new CeTaskImpl(this.ceTask.getUuid(), status),
createProject(this.ceTask),
- getAnalysisDate(),
+ analysisDate,
+ analysisDate == null ? system2.now() : analysisDate,
ScannerContextImpl.from(reportReader.readContextProperties()),
status == SUCCESS ? createQualityGate(this.qualityGateHolder) : null);
}
ceTask.getComponentName());
}
- private long getAnalysisDate() {
+ @CheckForNull
+ private Long getAnalysisDate() {
if (this.analysisMetadataHolder.hasAnalysisDateBeenSet()) {
return this.analysisMetadataHolder.getAnalysisDate();
}
- return system2.now();
+ return null;
}
@CheckForNull
private static class ProjectAnalysis implements PostProjectAnalysisTask.ProjectAnalysis {
private final CeTask ceTask;
private final Project project;
+ @CheckForNull
+ private final Long analysisDate;
private final long date;
private final ScannerContext scannerContext;
@CheckForNull
private final QualityGate qualityGate;
- private ProjectAnalysis(CeTask ceTask, Project project, long date,
+ private ProjectAnalysis(CeTask ceTask, Project project,
+ @Nullable Long analysisDate, long date,
ScannerContext scannerContext, @Nullable QualityGate qualityGate) {
this.ceTask = requireNonNull(ceTask, "ceTask can not be null");
this.project = requireNonNull(project, "project can not be null");
+ this.analysisDate = analysisDate;
this.date = date;
this.scannerContext = requireNonNull(scannerContext, "scannerContext can not be null");
this.qualityGate = qualityGate;
return new Date(date);
}
+ @Override
+ public java.util.Optional<Date> getAnalysisDate() {
+ if (analysisDate == null) {
+ return java.util.Optional.empty();
+ }
+ return java.util.Optional.of(new Date(analysisDate));
+ }
+
@Override
public ScannerContext getScannerContext() {
return scannerContext;
return "ProjectAnalysis{" +
"ceTask=" + ceTask +
", project=" + project +
+ ", analysisDate=" + analysisDate +
", date=" + date +
+ ", scannerContext=" + scannerContext +
", qualityGate=" + qualityGate +
'}';
}
}
@Test
- public void analysisDate_comes_from_AnalysisMetadataHolder() {
+ public void date_comes_from_AnalysisMetadataHolder() {
analysisMetadataHolder.setAnalysisDate(8465132498L);
underTest.finished(true);
}
@Test
- public void analysisDate_comes_from_system2_if_not_set_in_AnalysisMetadataHolder() {
+ public void date_comes_from_system2_if_not_set_in_AnalysisMetadataHolder() {
long now = 1_999_663L;
when(system2.now()).thenReturn(now);
assertThat(projectAnalysisArgumentCaptor.getValue().getDate()).isEqualTo(new Date(now));
}
+ @Test
+ public void analysisDate_comes_from_AnalysisMetadataHolder_when_set() {
+ analysisMetadataHolder.setAnalysisDate(8465132498L);
+
+ underTest.finished(true);
+
+ verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
+
+ assertThat(projectAnalysisArgumentCaptor.getValue().getAnalysisDate())
+ .contains(new Date(analysisMetadataHolder.getAnalysisDate()));
+ }
+
+ @Test
+ public void analysisDate_is_empty_when_not_set_in_AnalysisMetadataHolder() {
+ underTest.finished(false);
+
+ verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
+
+ assertThat(projectAnalysisArgumentCaptor.getValue().getAnalysisDate()).isEmpty();
+ }
+
@Test
public void qualityGate_is_null_when_finished_method_argument_is_false() {
underTest.finished(false);
package org.sonar.api.ce.posttask;
import java.util.Date;
+import java.util.Optional;
import javax.annotation.CheckForNull;
import org.sonar.api.ExtensionPoint;
import org.sonar.api.ce.ComputeEngineSide;
* Date of the analysis.
* <p>
* This date is the same as the date of the project analysis report and the snapshot.
- *
+ *
+ * @deprecated use {@link #getAnalysisDate()} instead. When {@link #getAnalysisDate()} returns
+ * {@link Optional#empty() empty}, the current date will be returned.
*/
+ @Deprecated
Date getDate();
+ /**
+ * Date of the analysis.
+ * <p>
+ * This date is the same as the date of the project analysis report and therefore as the analysis in DB. It can be
+ * missing when the status of the task is {@link org.sonar.api.ce.posttask.CeTask.Status#FAILED FAILED}.
+ * </p>
+ */
+ Optional<Date> getAnalysisDate();
+
/**
* Context as defined by scanner through {@link org.sonar.api.batch.sensor.SensorContext#addContextProperty(String, String)}.
* It does not contain the settings used by scanner.
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
return date;
}
+ @Override
+ public Optional<Date> getAnalysisDate() {
+ return Optional.of(date);
+ }
+
@Override
public String toString() {
return "ProjectAnalysis{" +
"ceTask=" + ceTask +
", project=" + project +
", date=" + date.getTime() +
+ ", analysisDate=" + date.getTime() +
", qualityGate=" + qualityGate +
'}';
}
underTest.execute();
assertThat(captorPostProjectAnalysisTask.projectAnalysis.toString())
- .isEqualTo("ProjectAnalysis{ceTask=CeTask, project=Project, date=846351351684351, qualityGate=QualityGate}");
+ .isEqualTo("ProjectAnalysis{ceTask=CeTask, project=Project, date=846351351684351, analysisDate=846351351684351, qualityGate=QualityGate}");
}