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);
+ Integer days = NewCodePeriodParser.parseDays(value);
return resolveByDays(dbSession, rootUuid, days, value, referenceDate);
case PREVIOUS_VERSION:
return resolveByPreviousVersion(dbSession, rootUuid, projectVersion);
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);
// FIXME shouldn't this be the first analysis after targetDate?
Duration bestDuration = null;
SnapshotDto nearest = null;
+
+ ensureNotOnFirstAnalysis(!snapshots.isEmpty());
+
for (SnapshotDto snapshot : snapshots) {
Instant createdAt = Instant.ofEpochMilli(snapshot.getCreatedAt());
Duration duration = Duration.between(targetDate, createdAt).abs();
*/
package org.sonar.ce.task.projectanalysis.step;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Optional;
import java.util.function.Supplier;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
String projectVersion = treeRootHolder.getRoot().getProjectAttributes().getProjectVersion();
try (DbSession dbSession = dbClient.openSession(false)) {
- Optional<NewCodePeriodDto> dto = firstPresent(
+ Optional<NewCodePeriodDto> dto = firstPresent(Arrays.asList(
() -> getBranchSetting(dbSession, projectUuid, branchUuid),
() -> getProjectSetting(dbSession, projectUuid),
- () -> getGlobalSetting(dbSession));
+ () -> getGlobalSetting(dbSession)
+ ));
long analysisDate = analysisMetadataHolder.getAnalysisDate();
Period period = dto.map(d -> resolver.resolve(dbSession, branchUuid, d, analysisDate, projectVersion))
}
}
- private static <T> Optional<T> firstPresent(Supplier<Optional<T>>... suppliers) {
+ private static <T> Optional<T> firstPresent(Collection<Supplier<Optional<T>>> suppliers) {
for (Supplier<Optional<T>> supplier : suppliers) {
Optional<T> result = supplier.get();
if (result.isPresent()) {
package org.sonar.db.purge.period;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
break;
case NUMBER_OF_DAYS:
requireValue(type, value);
- try {
- dto.setValue(Integer.toString(NewCodePeriodParser.parseDays(value)));
- } catch (Exception e) {
- throw new IllegalArgumentException("Failed to parse number of days: " + value);
- }
+ dto.setValue(parseDays(value));
break;
case SPECIFIC_ANALYSIS:
requireValue(type, value);
}
}
+ private static String parseDays(String value) {
+ try {
+ return Integer.toString(NewCodePeriodParser.parseDays(value));
+ } catch (Exception e) {
+ throw new IllegalArgumentException("Failed to parse number of days: " + value);
+ }
+ }
+
private static void requireValue(NewCodePeriodType type, @Nullable String value) {
Preconditions.checkArgument(value != null, "New Code Period type '%s' requires a value", type);
}
.orElseGet(() -> get(dbSession, projectUuid, null, true));
}
- private ShowWSResponse.Builder build(NewCodePeriodDto dto, boolean inherited) {
+ private static ShowWSResponse.Builder build(NewCodePeriodDto dto, boolean inherited) {
ShowWSResponse.Builder builder = ShowWSResponse.newBuilder()
.setType(convertType(dto.getType()))
.setInherited(inherited);
return builder;
}
- private ShowWSResponse.Builder buildDefault(boolean inherited) {
+ private static ShowWSResponse.Builder buildDefault(boolean inherited) {
return ShowWSResponse.newBuilder()
.setType(convertType(NewCodePeriodType.PREVIOUS_VERSION))
.setInherited(inherited);
--- /dev/null
+/*
+ * 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.server.newcodeperiod.ws;
+
+import org.junit.Test;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class NewCodePeriodsWsTest {
+ private String actionKey = randomAlphanumeric(10);
+ private NewCodePeriodsWs underTest = new NewCodePeriodsWs(
+ new NewCodePeriodsWsAction() {
+ @Override
+ public void define(WebService.NewController context) {
+ context.createAction(actionKey).setHandler(this);
+ }
+
+ @Override
+ public void handle(Request request, Response response) {
+
+ }
+ }
+ );
+
+ @Test
+ public void define_ws() {
+ WebService.Context context = new WebService.Context();
+
+ underTest.define(context);
+
+ WebService.Controller controller = context.controller("api/new_code_periods");
+ assertThat(controller).isNotNull();
+ assertThat(controller.description()).isNotEmpty();
+ assertThat(controller.actions()).hasSize(1);
+ }
+
+}
*/
package org.sonar.api.server.profile;
-import javax.annotation.CheckForNull;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.rules.RuleAnnotationUtils;
import org.sonar.api.server.ServerSide;