.orElseThrow(() -> new IllegalStateException("Analysis '" + value + "' of project '" + rootUuid
+ "' defined as the baseline does not exist"));
LOG.debug("Resolving new code period with a specific analysis");
- return newPeriod(NewCodePeriodType.SPECIFIC_ANALYSIS, value, Instant.ofEpochMilli(baseline.getCreatedAt()));
+ return newPeriod(NewCodePeriodType.SPECIFIC_ANALYSIS, value, baseline.getCreatedAt());
}
private Period resolveByPreviousVersion(DbSession dbSession, String projectUuid, String projectVersion) {
String mostRecentVersion = Optional.ofNullable(versions.iterator().next().getName())
.orElseThrow(() -> new IllegalStateException("selectVersionsByMostRecentFirst returned a DTO which didn't have a name"));
- if (versions.size() == 1) {
+ if (versions.size() == 1 && projectVersion.equals(mostRecentVersion)) {
return findOldestAnalysis(dbSession, projectUuid);
}
+
return resolvePreviousVersion(dbSession, projectVersion, versions, mostRecentVersion);
}
Instant targetDate = DateUtils.addDays(Instant.ofEpochMilli(referenceDate), -days);
LOG.debug("Resolving new code period by {} days: {}", days, supplierToString(() -> logDate(targetDate)));
SnapshotDto snapshot = findNearestSnapshotToTargetDate(snapshots, targetDate);
- return newPeriod(NewCodePeriodType.NUMBER_OF_DAYS, String.valueOf((int) days), Instant.ofEpochMilli(snapshot.getCreatedAt()));
+ return newPeriod(NewCodePeriodType.NUMBER_OF_DAYS, String.valueOf((int) days), snapshot.getCreatedAt());
}
private Period resolvePreviousVersion(DbSession dbSession, String currentVersion, List<EventDto> versions, String mostRecentVersion) {
private Period findOldestAnalysis(DbSession dbSession, String projectUuid) {
LOG.debug("Resolving first analysis as new code period as there is only one existing version");
Optional<Period> period = dbClient.snapshotDao().selectOldestSnapshot(dbSession, projectUuid)
- .map(dto -> newPeriod(NewCodePeriodType.PREVIOUS_VERSION, null, Instant.ofEpochMilli(dto.getCreatedAt())));
+ .map(dto -> newPeriod(NewCodePeriodType.PREVIOUS_VERSION, null, dto.getCreatedAt()));
ensureNotOnFirstAnalysis(period.isPresent());
return period.get();
}
private Period newPeriod(DbSession dbSession, EventDto previousVersion) {
Optional<Period> period = dbClient.snapshotDao().selectByUuid(dbSession, previousVersion.getAnalysisUuid())
- .map(dto -> newPeriod(NewCodePeriodType.PREVIOUS_VERSION, dto.getProjectVersion(), Instant.ofEpochMilli(dto.getCreatedAt())));
+ .map(dto -> newPeriod(NewCodePeriodType.PREVIOUS_VERSION, dto.getProjectVersion(), dto.getCreatedAt()));
if (!period.isPresent()) {
throw new IllegalStateException(format("Analysis '%s' for version event '%s' has been deleted",
previousVersion.getAnalysisUuid(), previousVersion.getName()));
return period.get();
}
- private static Period newPeriod(NewCodePeriodType type, @Nullable String value, Instant instant) {
- return new Period(type.name(), value, instant.toEpochMilli());
+ private static Period newPeriod(NewCodePeriodType type, @Nullable String value, long date) {
+ return new Period(type.name(), value, date);
}
private static Object supplierToString(Supplier<String> s) {
SnapshotDto analysis5 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setProjectVersion("1.1").setLast(true)); // 2008-11-29
dbTester.events().insertEvent(newEvent(analysis1).setName("0.9").setCategory(CATEGORY_VERSION).setDate(analysis1.getCreatedAt()));
dbTester.events().insertEvent(newEvent(analysis2).setName("1.0").setCategory(CATEGORY_VERSION).setDate(analysis2.getCreatedAt()));
- dbTester.events().insertEvent(newEvent(analysis5).setName("1.1").setCategory(CATEGORY_VERSION).setDate(analysis4.getCreatedAt()));
+ dbTester.events().insertEvent(newEvent(analysis5).setName("1.1").setCategory(CATEGORY_VERSION).setDate(analysis5.getCreatedAt()));
setupRoot(project, "1.1");
setProjectPeriod(project.uuid(), NewCodePeriodType.PREVIOUS_VERSION, null);
verifyDebugLogs("Resolving new code period by previous version: 1.0");
}
+ @Test
+ public void load_previous_version_when_version_is_changing() {
+ SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setProjectVersion("0.9").setLast(false)); // 2008-11-11
+ SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setProjectVersion("0.9").setLast(true)); // 2008-11-12
+
+ dbTester.events().insertEvent(newEvent(analysis2).setName("0.9").setCategory(CATEGORY_VERSION).setDate(analysis2.getCreatedAt()));
+ setupRoot(project, "1.0");
+ setProjectPeriod(project.uuid(), NewCodePeriodType.PREVIOUS_VERSION, null);
+
+ underTest.execute(new TestComputationStepContext());
+
+ assertPeriod(NewCodePeriodType.PREVIOUS_VERSION, "0.9", analysis2.getCreatedAt());
+
+ verifyDebugLogs("Resolving new code period by previous version: 0.9");
+ }
+
@Test
@UseDataProvider("zeroOrLess")
public void fail_with_MessageException_if_period_is_0_or_less(int zeroOrLess) {
public void load_previous_version_with_previous_version_deleted() {
SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setProjectVersion("0.9").setLast(false)); // 2008-11-11
SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setProjectVersion("1.0").setLast(false)); // 2008-11-12
- SnapshotDto analysis3 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setProjectVersion("1.1").setLast(false)); // 2008-11-20
+ SnapshotDto analysis3 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setProjectVersion("1.1").setLast(true)); // 2008-11-20
dbTester.events().insertEvent(newEvent(analysis1).setName("0.9").setCategory(CATEGORY_VERSION));
// The "1.0" version was deleted from the history
- dbTester.events().insertEvent(newEvent(analysis3).setName("1.1").setCategory(CATEGORY_VERSION));
setupRoot(project, "1.1");
underTest.execute(new TestComputationStepContext());
underTest.execute(new TestComputationStepContext());
- assertPeriod(NewCodePeriodType.PREVIOUS_VERSION, null, analysis.getCreatedAt());
- verifyDebugLogs("Resolving first analysis as new code period as there is only one existing version");
+ assertPeriod(NewCodePeriodType.PREVIOUS_VERSION, "0.9", analysis.getCreatedAt());
+ verifyDebugLogs("Resolving new code period by previous version: 0.9");
}
@Test
@DataProvider
public static Object[][] zeroOrLess() {
- return new Object[][]{
+ return new Object[][] {
{0},
{-1 - new Random().nextInt(30)}
};
@DataProvider
public static Object[][] stringConsideredAsVersions() {
- return new Object[][]{
+ return new Object[][] {
{randomAlphabetic(5)},
{"1,3"},
{"1.3"},
@DataProvider
public static Object[][] projectVersionNullOrNot() {
- return new Object[][]{
+ return new Object[][] {
{null},
{randomAlphabetic(15)},
};
@DataProvider
public static Object[][] anyValidLeakPeriodSettingValue() {
- return new Object[][]{
+ return new Object[][] {
// days
{NewCodePeriodType.NUMBER_OF_DAYS, "100"},
// previous_version