import org.sonar.api.database.model.ResourceModel;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Project;
+import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.SonarException;
import java.util.Date;
public ProjectConfigurator configure(Project project) {
Date analysisDate = loadAnalysisDate();
- checkCurrentAnalyisIsTheLatestOne(project, analysisDate);
+ checkCurrentAnalyisIsTheLatestOne(project.getKey(), analysisDate);
project
.setConfiguration(new PropertiesConfiguration()) // will be populated by ProjectSettings
return this;
}
- private void checkCurrentAnalyisIsTheLatestOne(Project project, Date analysisDate) {
- if (!isLatestAnalysis(project.getKey(), analysisDate)) {
- throw new IllegalArgumentException(
- "The value '" + settings.getString(CoreProperties.PROJECT_DATE_PROPERTY) + "' of the sonar.projectDate property " +
- "can't be older than the date of last known quality snapshot " +
- "on this project. This property must only be used to rebuild the past in a chronological order.");
- }
- }
-
- boolean isLatestAnalysis(String projectKey, Date analysisDate) {
+ private void checkCurrentAnalyisIsTheLatestOne(String projectKey, Date analysisDate) {
ResourceModel persistedProject = databaseSession.getSingleResult(ResourceModel.class, "key", projectKey, "enabled", true);
if (persistedProject != null) {
Snapshot lastSnapshot = databaseSession.getSingleResult(Snapshot.class, "resourceId", persistedProject.getId(), "last", true);
- return lastSnapshot == null || lastSnapshot.getCreatedAt().before(analysisDate);
+ Date lastSnapshotCreationDate = lastSnapshot.getCreatedAt();
+ if (lastSnapshot != null && !lastSnapshotCreationDate.before(analysisDate)) {
+ throw new IllegalArgumentException(
+ "'sonar.projectDate' property cannot be older than the date of the last known quality snapshot on this project. Value: '"+ settings.getString(CoreProperties.PROJECT_DATE_PROPERTY) + "'. " +
+ "Latest quality snapshot: '"+ DateUtils.formatDate(lastSnapshotCreationDate) +"'. This property may only be used to rebuild the past in a chronological order."
+ );
+ }
}
- return true;
+
}
Date loadAnalysisDate() {
new ProjectConfigurator(getSession(), configuration).configure(project);
fail();
} catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The value '2005-12-25' of the sonar.projectDate property can't be older than the date " +
- "of last known quality snapshot on this project. This property must only be used to rebuild the past in a chronological order.");
+ assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("'sonar.projectDate' property cannot be older than the date of the last known quality snapshot on this project. " +
+ "Value: '2005-12-25'. Latest quality snapshot: '2010-12-02'. This property may only be used to rebuild the past in a chronological order.");
}
}