From: Duarte Meneses Date: Fri, 6 Dec 2019 18:46:58 +0000 (-0600) Subject: SONAR-12788 New code period is badly set when there's only one version X-Git-Tag: 8.1.0.31237~23 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4bba4b305c806a47c6fcc39d41f7e57a44c4072e;p=sonarqube.git SONAR-12788 New code period is badly set when there's only one version --- diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/period/NewCodePeriodResolver.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/period/NewCodePeriodResolver.java index 5e925e88401..487309769f0 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/period/NewCodePeriodResolver.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/period/NewCodePeriodResolver.java @@ -81,7 +81,7 @@ public class NewCodePeriodResolver { .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) { @@ -93,9 +93,10 @@ public class NewCodePeriodResolver { 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); } @@ -107,7 +108,7 @@ public class NewCodePeriodResolver { 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 versions, String mostRecentVersion) { @@ -119,14 +120,14 @@ public class NewCodePeriodResolver { 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 = 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 = 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())); @@ -134,8 +135,8 @@ public class NewCodePeriodResolver { 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 s) { diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStepTest.java index 47f9b6bf042..09666f0251f 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStepTest.java @@ -288,7 +288,7 @@ public class LoadPeriodsStepTest extends BaseStepTest { 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); @@ -299,6 +299,22 @@ public class LoadPeriodsStepTest extends BaseStepTest { 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) { @@ -313,10 +329,9 @@ public class LoadPeriodsStepTest extends BaseStepTest { 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()); @@ -349,8 +364,8 @@ public class LoadPeriodsStepTest extends BaseStepTest { 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 @@ -377,7 +392,7 @@ public class LoadPeriodsStepTest extends BaseStepTest { @DataProvider public static Object[][] zeroOrLess() { - return new Object[][]{ + return new Object[][] { {0}, {-1 - new Random().nextInt(30)} }; @@ -385,7 +400,7 @@ public class LoadPeriodsStepTest extends BaseStepTest { @DataProvider public static Object[][] stringConsideredAsVersions() { - return new Object[][]{ + return new Object[][] { {randomAlphabetic(5)}, {"1,3"}, {"1.3"}, @@ -397,7 +412,7 @@ public class LoadPeriodsStepTest extends BaseStepTest { @DataProvider public static Object[][] projectVersionNullOrNot() { - return new Object[][]{ + return new Object[][] { {null}, {randomAlphabetic(15)}, }; @@ -405,7 +420,7 @@ public class LoadPeriodsStepTest extends BaseStepTest { @DataProvider public static Object[][] anyValidLeakPeriodSettingValue() { - return new Object[][]{ + return new Object[][] { // days {NewCodePeriodType.NUMBER_OF_DAYS, "100"}, // previous_version