From 1c6c03d9ed72f5789d58d5fa93de757d8c0ed073 Mon Sep 17 00:00:00 2001 From: Jacek <52388493+jacek-poreda-sonarsource@users.noreply.github.com> Date: Fri, 20 Sep 2019 10:30:43 +0200 Subject: [PATCH] Fixing SonarQube analysis issues (#2084) --- .../period/NewCodePeriodResolver.java | 6 +- .../projectanalysis/step/LoadPeriodsStep.java | 9 ++- .../sonar/db/purge/period/KeepOneFilter.java | 1 - .../server/newcodeperiod/ws/SetAction.java | 14 +++-- .../server/newcodeperiod/ws/ShowAction.java | 4 +- .../ws/NewCodePeriodsWsTest.java | 58 +++++++++++++++++++ ...BuiltInQualityProfileAnnotationLoader.java | 1 - 7 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsTest.java 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 7fee57705c8..5e925e88401 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 @@ -62,8 +62,8 @@ public class NewCodePeriodResolver { 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); @@ -104,7 +104,6 @@ public class NewCodePeriodResolver { List 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); @@ -156,6 +155,9 @@ public class NewCodePeriodResolver { // 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(); 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 46b46ed41bc..13e7537e265 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,6 +19,8 @@ */ 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; @@ -77,10 +79,11 @@ public class LoadPeriodsStep implements ComputationStep { String projectVersion = treeRootHolder.getRoot().getProjectAttributes().getProjectVersion(); try (DbSession dbSession = dbClient.openSession(false)) { - Optional dto = firstPresent( + Optional 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)) @@ -89,7 +92,7 @@ public class LoadPeriodsStep implements ComputationStep { } } - private static Optional firstPresent(Supplier>... suppliers) { + private static Optional firstPresent(Collection>> suppliers) { for (Supplier> supplier : suppliers) { Optional result = supplier.get(); if (result.isPresent()) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java index f15bf1749dd..8b1d8821186 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java @@ -20,7 +20,6 @@ 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; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java index 0394cc27a70..96458fbb3e9 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java @@ -154,11 +154,7 @@ public class SetAction implements NewCodePeriodsWsAction { 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); @@ -171,6 +167,14 @@ public class SetAction implements NewCodePeriodsWsAction { } } + 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); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java index c46343aca4b..0460b4d7535 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java @@ -133,7 +133,7 @@ public class ShowAction implements NewCodePeriodsWsAction { .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); @@ -144,7 +144,7 @@ public class ShowAction implements NewCodePeriodsWsAction { 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); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsTest.java new file mode 100644 index 00000000000..54c45617fa0 --- /dev/null +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsTest.java @@ -0,0 +1,58 @@ +/* + * 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); + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/profile/BuiltInQualityProfileAnnotationLoader.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/profile/BuiltInQualityProfileAnnotationLoader.java index bb87c1fabcf..88bcb3f61ef 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/profile/BuiltInQualityProfileAnnotationLoader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/profile/BuiltInQualityProfileAnnotationLoader.java @@ -19,7 +19,6 @@ */ 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; -- 2.39.5