From bc8db6a88713488ec46d0b45f24d5a0ef7b37103 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 24 Nov 2017 14:29:59 +0100 Subject: [PATCH] Merge QGateWsSupport to QualityGatesWsSupport --- .../server/qualitygate/QualityGateModule.java | 2 - .../server/qualitygate/ws/ListAction.java | 4 +- .../server/qualitygate/ws/QGateWsSupport.java | 95 ------------------- .../qualitygate/ws/QualityGatesWsSupport.java | 65 ++++++++++++- .../server/qualitygate/ws/RenameAction.java | 4 +- .../server/qualitygate/ws/ShowAction.java | 4 +- .../qualitygate/QualityGateModuleTest.java | 2 +- .../ws/DeleteConditionActionTest.java | 3 +- .../server/qualitygate/ws/ListActionTest.java | 2 +- .../qualitygate/ws/RenameActionTest.java | 2 +- .../server/qualitygate/ws/ShowActionTest.java | 2 +- 11 files changed, 73 insertions(+), 112 deletions(-) delete mode 100644 server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGateWsSupport.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java index ee9980dc112..6e97f2355f1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java @@ -30,7 +30,6 @@ import org.sonar.server.qualitygate.ws.DestroyAction; import org.sonar.server.qualitygate.ws.GetByProjectAction; import org.sonar.server.qualitygate.ws.ListAction; import org.sonar.server.qualitygate.ws.ProjectStatusAction; -import org.sonar.server.qualitygate.ws.QGateWsSupport; import org.sonar.server.qualitygate.ws.QualityGatesWs; import org.sonar.server.qualitygate.ws.QualityGatesWsSupport; import org.sonar.server.qualitygate.ws.RenameAction; @@ -51,7 +50,6 @@ public class QualityGateModule extends Module { QgateProjectFinder.class, QualityGateFinder.class, // WS - QGateWsSupport.class, QualityGatesWsSupport.class, QualityGatesWs.class, ListAction.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java index a3fc1cbc0d2..e2930804118 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java @@ -39,9 +39,9 @@ import static org.sonar.server.ws.WsUtils.writeProtobuf; public class ListAction implements QualityGatesWsAction { private final DbClient dbClient; - private final QGateWsSupport wsSupport; + private final QualityGatesWsSupport wsSupport; - public ListAction(DbClient dbClient, QGateWsSupport wsSupport) { + public ListAction(DbClient dbClient, QualityGatesWsSupport wsSupport) { this.dbClient = dbClient; this.wsSupport = wsSupport; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGateWsSupport.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGateWsSupport.java deleted file mode 100644 index f15b1fbc559..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGateWsSupport.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.qualitygate.ws; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.server.ServerSide; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.property.PropertyDto; -import org.sonar.db.qualitygate.QualityGateDto; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.user.UserSession; -import org.sonarqube.ws.Qualitygates; - -import static com.google.common.base.Preconditions.checkArgument; -import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; -import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY; - -@ServerSide -public class QGateWsSupport { - - private final DbClient dbClient; - private final UserSession userSession; - private final DefaultOrganizationProvider defaultOrganizationProvider; - - public QGateWsSupport(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider) { - this.dbClient = dbClient; - this.userSession = userSession; - this.defaultOrganizationProvider = defaultOrganizationProvider; - } - - boolean isQualityGateAdmin() { - return userSession.hasPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); - } - - Qualitygates.Actions getActions(QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate) { - Long defaultId = defaultQualityGate == null ? null : defaultQualityGate.getId(); - boolean isDefault = qualityGate.getId().equals(defaultId); - boolean isBuiltIn = qualityGate.isBuiltIn(); - boolean isQualityGateAdmin = isQualityGateAdmin(); - return Qualitygates.Actions.newBuilder() - .setCopy(isQualityGateAdmin) - .setEdit(!isBuiltIn && isQualityGateAdmin) - .setSetAsDefault(!isDefault && isQualityGateAdmin) - .setAssociateProjects(!isDefault && isQualityGateAdmin) - .build(); - } - - @CheckForNull - QualityGateDto getDefault(DbSession dbSession) { - Long defaultId = getDefaultId(dbSession); - if (defaultId == null) { - return null; - } - return dbClient.qualityGateDao().selectById(dbSession, defaultId); - } - - @CheckForNull - private Long getDefaultId(DbSession dbSession) { - PropertyDto defaultQgate = dbClient.propertiesDao().selectGlobalProperty(dbSession, SONAR_QUALITYGATE_PROPERTY); - if (defaultQgate == null || StringUtils.isBlank(defaultQgate.getValue())) { - return null; - } - return Long.valueOf(defaultQgate.getValue()); - } - - void checkCanEdit(QualityGateDto qualityGate) { - checkNotBuiltInt(qualityGate); - userSession.checkPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); - } - - private static void checkNotBuiltInt(QualityGateDto qualityGate) { - checkArgument(!qualityGate.isBuiltIn(), "Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName()); - } - -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java index f6e945b3783..6cbf5ab48a8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java @@ -20,23 +20,36 @@ package org.sonar.server.qualitygate.ws; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.apache.commons.lang.StringUtils; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.property.PropertyDto; import org.sonar.db.qualitygate.QualityGateConditionDto; +import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.organization.DefaultOrganizationProvider; +import org.sonar.server.user.UserSession; +import org.sonarqube.ws.Qualitygates; +import static com.google.common.base.Preconditions.checkArgument; import static java.lang.String.format; +import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; +import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY; import static org.sonar.server.ws.WsUtils.checkFound; public class QualityGatesWsSupport { + private final DbClient dbClient; - private final DefaultOrganizationProvider organizationProvider; + private final UserSession userSession; + private final DefaultOrganizationProvider defaultOrganizationProvider; - public QualityGatesWsSupport(DbClient dbClient, DefaultOrganizationProvider organizationProvider) { + public QualityGatesWsSupport(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider) { this.dbClient = dbClient; - this.organizationProvider = organizationProvider; + this.userSession = userSession; + this.defaultOrganizationProvider = defaultOrganizationProvider; } QualityGateConditionDto getCondition(DbSession dbSession, long id) { @@ -44,8 +57,52 @@ public class QualityGatesWsSupport { } OrganizationDto getOrganization(DbSession dbSession) { - String organizationKey = organizationProvider.get().getKey(); + String organizationKey = defaultOrganizationProvider.get().getKey(); return dbClient.organizationDao().selectByKey(dbSession, organizationKey) .orElseThrow(() -> new NotFoundException(format("No organization with key '%s'", organizationKey))); } + + boolean isQualityGateAdmin() { + return userSession.hasPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); + } + + Qualitygates.Actions getActions(QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate) { + Long defaultId = defaultQualityGate == null ? null : defaultQualityGate.getId(); + boolean isDefault = qualityGate.getId().equals(defaultId); + boolean isBuiltIn = qualityGate.isBuiltIn(); + boolean isQualityGateAdmin = isQualityGateAdmin(); + return Qualitygates.Actions.newBuilder() + .setCopy(isQualityGateAdmin) + .setEdit(!isBuiltIn && isQualityGateAdmin) + .setSetAsDefault(!isDefault && isQualityGateAdmin) + .setAssociateProjects(!isDefault && isQualityGateAdmin) + .build(); + } + + @CheckForNull + QualityGateDto getDefault(DbSession dbSession) { + Long defaultId = getDefaultId(dbSession); + if (defaultId == null) { + return null; + } + return dbClient.qualityGateDao().selectById(dbSession, defaultId); + } + + @CheckForNull + private Long getDefaultId(DbSession dbSession) { + PropertyDto defaultQgate = dbClient.propertiesDao().selectGlobalProperty(dbSession, SONAR_QUALITYGATE_PROPERTY); + if (defaultQgate == null || StringUtils.isBlank(defaultQgate.getValue())) { + return null; + } + return Long.valueOf(defaultQgate.getValue()); + } + + void checkCanEdit(QualityGateDto qualityGate) { + checkNotBuiltInt(qualityGate); + userSession.checkPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); + } + + private static void checkNotBuiltInt(QualityGateDto qualityGate) { + checkArgument(!qualityGate.isBuiltIn(), "Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName()); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java index e65a5ee037e..212330cc98f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java @@ -40,9 +40,9 @@ public class RenameAction implements QualityGatesWsAction { private final DbClient dbClient; private final QualityGateFinder qualityGateFinder; - private final QGateWsSupport wsSupport; + private final QualityGatesWsSupport wsSupport; - public RenameAction(DbClient dbClient, QualityGateFinder qualityGateFinder, QGateWsSupport wsSupport) { + public RenameAction(DbClient dbClient, QualityGateFinder qualityGateFinder, QualityGatesWsSupport wsSupport) { this.dbClient = dbClient; this.qualityGateFinder = qualityGateFinder; this.wsSupport = wsSupport; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java index 0f01909002b..6eee336e81a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java @@ -51,9 +51,9 @@ public class ShowAction implements QualityGatesWsAction { private final DbClient dbClient; private final QualityGateFinder qualityGateFinder; - private final QGateWsSupport wsSupport; + private final QualityGatesWsSupport wsSupport; - public ShowAction(DbClient dbClient, QualityGateFinder qualityGateFinder, QGateWsSupport wsSupport) { + public ShowAction(DbClient dbClient, QualityGateFinder qualityGateFinder, QualityGatesWsSupport wsSupport) { this.dbClient = dbClient; this.qualityGateFinder = qualityGateFinder; this.wsSupport = wsSupport; diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java index 36d9eba904b..622d9fc6236 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java @@ -29,6 +29,6 @@ public class QualityGateModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new QualityGateModule().configure(container); - assertThat(container.size()).isEqualTo(25 + 2); + assertThat(container.size()).isEqualTo(24 + 2); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeleteConditionActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeleteConditionActionTest.java index 1e242273a33..df5a1a52363 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeleteConditionActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeleteConditionActionTest.java @@ -54,7 +54,8 @@ public class DeleteConditionActionTest { private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.from(db); - private WsActionTester ws = new WsActionTester(new DeleteConditionAction(userSession, db.getDbClient(), new QualityGatesWsSupport(db.getDbClient(), organizationProvider))); + private WsActionTester ws = new WsActionTester( + new DeleteConditionAction(userSession, db.getDbClient(), new QualityGatesWsSupport(db.getDbClient(), userSession, organizationProvider))); @Test public void definition() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java index 108ef2da489..471da817602 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java @@ -50,7 +50,7 @@ public class ListActionTest { public DbTester db = DbTester.create(); private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(), new QGateWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider))); + private WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(), new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider))); @Test public void verify_definition() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java index 6f561a91c79..f1405906ccb 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java @@ -54,7 +54,7 @@ public class RenameActionTest { private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); private WsActionTester ws = new WsActionTester( - new RenameAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()), new QGateWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider))); + new RenameAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()), new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider))); @Test public void verify_definition() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java index 4036de490ae..34c0667ffe6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java @@ -58,7 +58,7 @@ public class ShowActionTest { private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); private WsActionTester ws = new WsActionTester( - new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()), new QGateWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider))); + new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()), new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider))); @Test public void verify_definition() { -- 2.39.5