aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-ce-task-projectanalysis
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2019-08-21 06:08:31 -0500
committerSonarTech <sonartech@sonarsource.com>2019-09-24 20:21:14 +0200
commitd650e5eeb4098ff100fb9382ddb8ea51588b58f9 (patch)
tree8c0dc1b55dd81485c3baaf02da6f4a52a5ae2ebc /server/sonar-ce-task-projectanalysis
parentd170d4e4812844d37a7ec0355ba968bc9ef55545 (diff)
downloadsonarqube-d650e5eeb4098ff100fb9382ddb8ea51588b58f9.tar.gz
sonarqube-d650e5eeb4098ff100fb9382ddb8ea51588b58f9.zip
Feature/dm/migrate new code period (#2017)
* SONAR-12396 List new code periods for all branches with effective current values * SONAR-12347 Migrate old definitions of leak period
Diffstat (limited to 'server/sonar-ce-task-projectanalysis')
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java2
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/period/NewCodePeriodResolver.java190
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStep.java162
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStepTest.java12
4 files changed, 206 insertions, 160 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java
index f507563326b..92a6746f3f4 100644
--- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java
@@ -101,6 +101,7 @@ import org.sonar.ce.task.projectanalysis.measure.MeasureToMeasureDto;
import org.sonar.ce.task.projectanalysis.metric.MetricModule;
import org.sonar.ce.task.projectanalysis.notification.NotificationFactory;
import org.sonar.ce.task.projectanalysis.organization.DefaultOrganizationLoader;
+import org.sonar.ce.task.projectanalysis.period.NewCodePeriodResolver;
import org.sonar.ce.task.projectanalysis.period.PeriodHolderImpl;
import org.sonar.ce.task.projectanalysis.qualitygate.EvaluationResultTextConverterImpl;
import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateHolderImpl;
@@ -285,6 +286,7 @@ public final class ProjectAnalysisTaskContainerPopulator implements ContainerPop
BranchPersisterImpl.class,
SiblingsIssuesLoader.class,
SiblingsIssueMerger.class,
+ NewCodePeriodResolver.class,
// filemove
ScoreMatrixDumperImpl.class,
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
new file mode 100644
index 00000000000..7fee57705c8
--- /dev/null
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/period/NewCodePeriodResolver.java
@@ -0,0 +1,190 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.ce.task.projectanalysis.period;
+
+import java.time.Duration;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.List;
+import java.util.Optional;
+import java.util.function.Supplier;
+import javax.annotation.Nullable;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.api.utils.MessageException;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.component.SnapshotDto;
+import org.sonar.db.component.SnapshotQuery;
+import org.sonar.db.event.EventDto;
+import org.sonar.db.newcodeperiod.NewCodePeriodDto;
+import org.sonar.db.newcodeperiod.NewCodePeriodParser;
+import org.sonar.db.newcodeperiod.NewCodePeriodType;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+import static java.lang.String.format;
+import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED;
+import static org.sonar.db.component.SnapshotQuery.SORT_FIELD.BY_DATE;
+import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.ASC;
+
+public class NewCodePeriodResolver {
+ private static final Logger LOG = Loggers.get(NewCodePeriodResolver.class);
+
+ private final DbClient dbClient;
+
+ public NewCodePeriodResolver(DbClient dbClient) {
+ this.dbClient = dbClient;
+ }
+
+ public Period resolve(DbSession dbSession, String branchUuid, NewCodePeriodDto newCodePeriodDto, long referenceDate, String projectVersion) {
+ return toPeriod(newCodePeriodDto.getType(), newCodePeriodDto.getValue(), dbSession, projectVersion, branchUuid, referenceDate);
+ }
+
+ private Period toPeriod(NewCodePeriodType type, @Nullable String value, DbSession dbSession, String projectVersion, String rootUuid, long referenceDate) {
+ switch (type) {
+ case NUMBER_OF_DAYS:
+ Integer days = NewCodePeriodParser.parseDays(value);
+ checkNotNullValue(value, type);
+ return resolveByDays(dbSession, rootUuid, days, value, referenceDate);
+ case PREVIOUS_VERSION:
+ return resolveByPreviousVersion(dbSession, rootUuid, projectVersion);
+ case SPECIFIC_ANALYSIS:
+ checkNotNullValue(value, type);
+ return resolveBySpecificAnalysis(dbSession, rootUuid, value);
+ default:
+ throw new IllegalStateException("Unexpected type: " + type);
+ }
+ }
+
+ private Period resolveBySpecificAnalysis(DbSession dbSession, String rootUuid, String value) {
+ SnapshotDto baseline = dbClient.snapshotDao().selectByUuid(dbSession, value)
+ .filter(t -> t.getComponentUuid().equals(rootUuid))
+ .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()));
+ }
+
+ private Period resolveByPreviousVersion(DbSession dbSession, String projectUuid, String projectVersion) {
+ List<EventDto> versions = dbClient.eventDao().selectVersionsByMostRecentFirst(dbSession, projectUuid);
+ if (versions.isEmpty()) {
+ return findOldestAnalysis(dbSession, projectUuid);
+ }
+
+ 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) {
+ return findOldestAnalysis(dbSession, projectUuid);
+ }
+ return resolvePreviousVersion(dbSession, projectVersion, versions, mostRecentVersion);
+ }
+
+ private Period resolveByDays(DbSession dbSession, String rootUuid, Integer days, String value, long referenceDate) {
+ checkPeriodProperty(days > 0, value, "number of days is <= 0");
+ List<SnapshotDto> snapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbSession, createCommonQuery(rootUuid)
+ .setCreatedBefore(referenceDate).setSort(BY_DATE, ASC));
+
+ ensureNotOnFirstAnalysis(!snapshots.isEmpty());
+ 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()));
+ }
+
+ private Period resolvePreviousVersion(DbSession dbSession, String currentVersion, List<EventDto> versions, String mostRecentVersion) {
+ EventDto previousVersion = versions.get(currentVersion.equals(mostRecentVersion) ? 1 : 0);
+ LOG.debug("Resolving new code period by previous version: {}", previousVersion.getName());
+ return newPeriod(dbSession, previousVersion);
+ }
+
+ 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())));
+ 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())));
+ 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 Object supplierToString(Supplier<String> s) {
+ return new Object() {
+ @Override
+ public String toString() {
+ return s.get();
+ }
+ };
+ }
+
+ private static SnapshotQuery createCommonQuery(String projectUuid) {
+ return new SnapshotQuery().setComponentUuid(projectUuid).setStatus(STATUS_PROCESSED);
+ }
+
+ private static SnapshotDto findNearestSnapshotToTargetDate(List<SnapshotDto> snapshots, Instant targetDate) {
+ // FIXME shouldn't this be the first analysis after targetDate?
+ Duration bestDuration = null;
+ SnapshotDto nearest = null;
+ for (SnapshotDto snapshot : snapshots) {
+ Instant createdAt = Instant.ofEpochMilli(snapshot.getCreatedAt());
+ Duration duration = Duration.between(targetDate, createdAt).abs();
+ if (bestDuration == null || duration.compareTo(bestDuration) <= 0) {
+ bestDuration = duration;
+ nearest = snapshot;
+ }
+ }
+ return nearest;
+ }
+
+ private static void checkPeriodProperty(boolean test, String propertyValue, String testDescription, Object... args) {
+ if (!test) {
+ LOG.debug("Invalid code period '{}': {}", propertyValue, supplierToString(() -> format(testDescription, args)));
+ throw MessageException.of(format("Invalid new code period. '%s' is not one of: " +
+ "integer > 0, date before current analysis j, \"previous_version\", or version string that exists in the project' \n" +
+ "Please contact a project administrator to correct this setting", propertyValue));
+ }
+ }
+
+ private static void ensureNotOnFirstAnalysis(boolean expression) {
+ checkState(expression, "Attempting to resolve period while no analysis exist for project");
+ }
+
+ private static void checkNotNullValue(@Nullable String value, NewCodePeriodType type) {
+ checkNotNull(value, "Value can't be null with type %s", type);
+ }
+
+ private static String logDate(Instant instant) {
+ return DateUtils.formatDate(instant.truncatedTo(ChronoUnit.SECONDS));
+ }
+}
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStep.java
index d646d03a213..20cae44c073 100644
--- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStep.java
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStep.java
@@ -19,39 +19,19 @@
*/
package org.sonar.ce.task.projectanalysis.step;
-import java.time.Duration;
-import java.time.Instant;
-import java.time.temporal.ChronoUnit;
-import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
-import javax.annotation.Nullable;
-import org.sonar.api.utils.DateUtils;
-import org.sonar.api.utils.MessageException;
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
+import org.sonar.ce.task.projectanalysis.period.NewCodePeriodResolver;
import org.sonar.ce.task.projectanalysis.period.Period;
import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
import org.sonar.ce.task.projectanalysis.period.PeriodHolderImpl;
import org.sonar.ce.task.step.ComputationStep;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.component.SnapshotDto;
-import org.sonar.db.component.SnapshotQuery;
-import org.sonar.db.event.EventDto;
import org.sonar.db.newcodeperiod.NewCodePeriodDao;
import org.sonar.db.newcodeperiod.NewCodePeriodDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodParser;
-import org.sonar.db.newcodeperiod.NewCodePeriodType;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
-import static java.lang.String.format;
-import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED;
-import static org.sonar.db.component.SnapshotQuery.SORT_FIELD.BY_DATE;
-import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.ASC;
/**
* Populates the {@link PeriodHolder}
@@ -62,21 +42,22 @@ import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.ASC;
* - If a snapshot is found, a period is set to the repository, otherwise fail with MessageException
*/
public class LoadPeriodsStep implements ComputationStep {
- private static final Logger LOG = Loggers.get(LoadPeriodsStep.class);
private final AnalysisMetadataHolder analysisMetadataHolder;
private final NewCodePeriodDao newCodePeriodDao;
private final TreeRootHolder treeRootHolder;
private final PeriodHolderImpl periodsHolder;
private final DbClient dbClient;
+ private final NewCodePeriodResolver resolver;
public LoadPeriodsStep(AnalysisMetadataHolder analysisMetadataHolder, NewCodePeriodDao newCodePeriodDao, TreeRootHolder treeRootHolder,
- PeriodHolderImpl periodsHolder, DbClient dbClient) {
+ PeriodHolderImpl periodsHolder, DbClient dbClient, NewCodePeriodResolver resolver) {
this.analysisMetadataHolder = analysisMetadataHolder;
this.newCodePeriodDao = newCodePeriodDao;
this.treeRootHolder = treeRootHolder;
this.periodsHolder = periodsHolder;
this.dbClient = dbClient;
+ this.resolver = resolver;
}
@Override
@@ -101,8 +82,9 @@ public class LoadPeriodsStep implements ComputationStep {
() -> getProjectSetting(dbSession, projectUuid),
() -> getGlobalSetting(dbSession));
- Period period = dto.map(d -> toPeriod(d.getType(), d.getValue(), dbSession, projectVersion, branchUuid))
- .orElseGet(() -> toPeriod(NewCodePeriodType.PREVIOUS_VERSION, null, dbSession, projectVersion, branchUuid));
+ long analysisDate = analysisMetadataHolder.getAnalysisDate();
+ Period period = dto.map(d -> resolver.resolve(dbSession, branchUuid, d, analysisDate, projectVersion))
+ .orElseGet(() -> resolver.resolve(dbSession, branchUuid, NewCodePeriodDto.defaultInstance(), analysisDate, projectVersion));
periodsHolder.setPeriod(period);
}
}
@@ -129,137 +111,7 @@ public class LoadPeriodsStep implements ComputationStep {
return newCodePeriodDao.selectGlobal(dbSession);
}
- private Period toPeriod(NewCodePeriodType type, @Nullable String value, DbSession dbSession, String analysisProjectVersion, String rootUuid) {
- switch (type) {
- case NUMBER_OF_DAYS:
- Integer days = NewCodePeriodParser.parseDays(value);
- checkNotNullValue(value, type);
- return resolveByDays(dbSession, rootUuid, days, value);
- case PREVIOUS_VERSION:
- return resolveByPreviousVersion(dbSession, rootUuid, analysisProjectVersion);
- case SPECIFIC_ANALYSIS:
- checkNotNullValue(value, type);
- return resolveBySpecificAnalysis(dbSession, rootUuid, value);
- default:
- throw new IllegalStateException("Unexpected type: " + type);
- }
- }
-
private String getProjectBranchUuid() {
return analysisMetadataHolder.getProject().getUuid();
}
-
- private Period resolveBySpecificAnalysis(DbSession dbSession, String rootUuid, String value) {
- SnapshotDto baseline = dbClient.snapshotDao().selectByUuid(dbSession, value)
- .filter(t -> t.getComponentUuid().equals(rootUuid))
- .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()));
- }
-
- private Period resolveByPreviousVersion(DbSession dbSession, String projectUuid, String analysisProjectVersion) {
- List<EventDto> versions = dbClient.eventDao().selectVersionsByMostRecentFirst(dbSession, projectUuid);
- if (versions.isEmpty()) {
- return findOldestAnalysis(dbSession, projectUuid);
- }
-
- 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) {
- return findOldestAnalysis(dbSession, projectUuid);
- }
- return resolvePreviousVersion(dbSession, analysisProjectVersion, versions, mostRecentVersion);
- }
-
- private Period resolveByDays(DbSession dbSession, String rootUuid, Integer days, String propertyValue) {
- checkPeriodProperty(days > 0, propertyValue, "number of days is <= 0");
- long analysisDate = analysisMetadataHolder.getAnalysisDate();
- List<SnapshotDto> snapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbSession, createCommonQuery(rootUuid)
- .setCreatedBefore(analysisDate).setSort(BY_DATE, ASC));
-
- ensureNotOnFirstAnalysis(!snapshots.isEmpty());
- Instant targetDate = DateUtils.addDays(Instant.ofEpochMilli(analysisDate), -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()));
- }
-
- private Period resolvePreviousVersion(DbSession dbSession, String currentVersion, List<EventDto> versions, String mostRecentVersion) {
- EventDto previousVersion = versions.get(currentVersion.equals(mostRecentVersion) ? 1 : 0);
- LOG.debug("Resolving new code period by previous version: {}", previousVersion.getName());
- return newPeriod(dbSession, previousVersion);
- }
-
- 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())));
- 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())));
- 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 Object supplierToString(Supplier<String> s) {
- return new Object() {
- @Override
- public String toString() {
- return s.get();
- }
- };
- }
-
- private static SnapshotQuery createCommonQuery(String projectUuid) {
- return new SnapshotQuery().setComponentUuid(projectUuid).setStatus(STATUS_PROCESSED);
- }
-
- private static SnapshotDto findNearestSnapshotToTargetDate(List<SnapshotDto> snapshots, Instant targetDate) {
- // FIXME shouldn't this be the first analysis after targetDate?
- Duration bestDuration = null;
- SnapshotDto nearest = null;
- for (SnapshotDto snapshot : snapshots) {
- Instant createdAt = Instant.ofEpochMilli(snapshot.getCreatedAt());
- Duration duration = Duration.between(targetDate, createdAt).abs();
- if (bestDuration == null || duration.compareTo(bestDuration) <= 0) {
- bestDuration = duration;
- nearest = snapshot;
- }
- }
- return nearest;
- }
-
- private static void checkPeriodProperty(boolean test, String propertyValue, String testDescription, Object... args) {
- if (!test) {
- LOG.debug("Invalid code period '{}': {}", propertyValue, supplierToString(() -> format(testDescription, args)));
- throw MessageException.of(format("Invalid new code period. '%s' is not one of: " +
- "integer > 0, date before current analysis j, \"previous_version\", or version string that exists in the project' \n" +
- "Please contact a project administrator to correct this setting", propertyValue));
- }
- }
-
- private static void ensureNotOnFirstAnalysis(boolean expression) {
- checkState(expression, "Attempting to resolve period while no analysis exist for project");
- }
-
- private static void checkNotNullValue(@Nullable String value, NewCodePeriodType type) {
- checkNotNull(value, "Value can't be null with type %s", type);
- }
-
- private static String logDate(Instant instant) {
- return DateUtils.formatDate(instant.truncatedTo(ChronoUnit.SECONDS));
- }
}
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 1d253b9e06a..c440cfa96c9 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
@@ -44,6 +44,7 @@ import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.component.ReportComponent;
import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
+import org.sonar.ce.task.projectanalysis.period.NewCodePeriodResolver;
import org.sonar.ce.task.projectanalysis.period.Period;
import org.sonar.ce.task.projectanalysis.period.PeriodHolderImpl;
import org.sonar.ce.task.step.ComputationStep;
@@ -86,9 +87,10 @@ public class LoadPeriodsStepTest extends BaseStepTest {
private PeriodHolderImpl periodsHolder = new PeriodHolderImpl();
private System2 system2Mock = mock(System2.class);
private NewCodePeriodDao dao = new NewCodePeriodDao(system2Mock, new SequenceUuidFactory());
+ private NewCodePeriodResolver newCodePeriodResolver = new NewCodePeriodResolver(dbTester.getDbClient());
private ZonedDateTime analysisDate = ZonedDateTime.of(2019, 3, 20, 5, 30, 40, 0, ZoneId.systemDefault());
- private LoadPeriodsStep underTest = new LoadPeriodsStep(analysisMetadataHolder, dao, treeRootHolder, periodsHolder, dbTester.getDbClient());
+ private LoadPeriodsStep underTest = new LoadPeriodsStep(analysisMetadataHolder, dao, treeRootHolder, periodsHolder, dbTester.getDbClient(), newCodePeriodResolver);
private OrganizationDto organization;
private ComponentDto project;
@@ -373,7 +375,7 @@ public class LoadPeriodsStepTest extends BaseStepTest {
@DataProvider
public static Object[][] zeroOrLess() {
- return new Object[][] {
+ return new Object[][]{
{0},
{-1 - new Random().nextInt(30)}
};
@@ -381,7 +383,7 @@ public class LoadPeriodsStepTest extends BaseStepTest {
@DataProvider
public static Object[][] stringConsideredAsVersions() {
- return new Object[][] {
+ return new Object[][]{
{randomAlphabetic(5)},
{"1,3"},
{"1.3"},
@@ -393,7 +395,7 @@ public class LoadPeriodsStepTest extends BaseStepTest {
@DataProvider
public static Object[][] projectVersionNullOrNot() {
- return new Object[][] {
+ return new Object[][]{
{null},
{randomAlphabetic(15)},
};
@@ -401,7 +403,7 @@ public class LoadPeriodsStepTest extends BaseStepTest {
@DataProvider
public static Object[][] anyValidLeakPeriodSettingValue() {
- return new Object[][] {
+ return new Object[][]{
// days
{"100"},
// previous_version keyword