diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2019-10-22 17:37:03 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-11-06 10:04:27 +0100 |
commit | cfaff21330220890db6712830c50650f08f370db (patch) | |
tree | 107a9ed7b53f7e46a22bf4c53906a85b738b910f /server/sonar-webserver-webapi | |
parent | b1d5e0b3bb757d215b2b12feea0c06b97db1d665 (diff) | |
download | sonarqube-cfaff21330220890db6712830c50650f08f370db.tar.gz sonarqube-cfaff21330220890db6712830c50650f08f370db.zip |
SONAR-12515 Add AlmSettingsSupport to centralize common code
Diffstat (limited to 'server/sonar-webserver-webapi')
21 files changed, 203 insertions, 105 deletions
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/AlmSettingsSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/AlmSettingsSupport.java index 0a940e25c88..bbead0d9abf 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/AlmSettingsSupport.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/AlmSettingsSupport.java @@ -20,15 +20,47 @@ package org.sonar.server.almsettings; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; import org.sonar.db.alm.setting.ALM; +import org.sonar.db.alm.setting.AlmSettingDto; +import org.sonar.db.component.ComponentDto; +import org.sonar.server.component.ComponentFinder; +import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.user.UserSession; import org.sonarqube.ws.AlmSettings; import static java.lang.String.format; +import static org.sonar.api.web.UserRole.ADMIN; class AlmSettingsSupport { - private AlmSettingsSupport() { - // Only static methods here for the moment + private final DbClient dbClient; + private final UserSession userSession; + private final ComponentFinder componentFinder; + + public AlmSettingsSupport(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder) { + this.dbClient = dbClient; + this.userSession = userSession; + this.componentFinder = componentFinder; + } + + void checkAlmSettingDoesNotAlreadyExist(DbSession dbSession, String almSetting) { + dbClient.almSettingDao().selectByKey(dbSession, almSetting) + .ifPresent(a -> { + throw new IllegalArgumentException(format("An ALM setting with key '%s' already exists", a.getKey())); + }); + } + + ComponentDto getProject(DbSession dbSession, String projectKey) { + ComponentDto project = componentFinder.getByKey(dbSession, projectKey); + userSession.checkComponentPermission(ADMIN, project); + return project; + } + + AlmSettingDto getAlmSetting(DbSession dbSession, String almSetting) { + return dbClient.almSettingDao().selectByKey(dbSession, almSetting) + .orElseThrow(() -> new NotFoundException(format("ALM setting with key '%s' cannot be found", almSetting))); } static AlmSettings.Alm toAlmWs(ALM alm) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/AlmSettingsWsModule.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/AlmSettingsWsModule.java index 7c970d2da77..d3eeffcb354 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/AlmSettingsWsModule.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/AlmSettingsWsModule.java @@ -26,19 +26,25 @@ public class AlmSettingsWsModule extends Module { protected void configureModule() { add( AlmSettingsWs.class, + // Common actions + AlmSettingsSupport.class, + CountBindingAction.class, + DeleteAction.class, + DeleteBindingAction.class, + ListAction.class, ListDefinitionsAction.class, + // GitHub specific actions CreateGithubAction.class, + GetBindingAction.class, + SetGithubBindingAction.class, UpdateGitHubAction.class, - DeleteAction.class, + // Azure DevOps specific actions CreateAzureAction.class, + SetAzureBindingAction.class, UpdateAzureAction.class, + // Bitbucket DevOps specific actions CreateBitBucketAction.class, - UpdateBitbucketAction.class, - SetGithubBindingAction.class, - DeleteBindingAction.class, - GetBindingAction.class, - ListAction.class, - CountBindingAction.class + UpdateBitbucketAction.class ); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CountBindingAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CountBindingAction.java index a874360385d..578f3666072 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CountBindingAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CountBindingAction.java @@ -26,11 +26,9 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.alm.setting.AlmSettingDto; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; import org.sonarqube.ws.AlmSettings.CountBindingWsResponse; -import static java.lang.String.format; import static org.sonar.server.ws.WsUtils.writeProtobuf; public class CountBindingAction implements AlmSettingsWsAction { @@ -39,10 +37,12 @@ public class CountBindingAction implements AlmSettingsWsAction { private final DbClient dbClient; private final UserSession userSession; + private final AlmSettingsSupport almSettingsSupport; - public CountBindingAction(DbClient dbClient, UserSession userSession) { + public CountBindingAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) { this.dbClient = dbClient; this.userSession = userSession; + this.almSettingsSupport = almSettingsSupport; } @Override @@ -70,8 +70,7 @@ public class CountBindingAction implements AlmSettingsWsAction { private CountBindingWsResponse doHandle(Request request) { String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING); try (DbSession dbSession = dbClient.openSession(false)) { - AlmSettingDto almSetting = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey) - .orElseThrow(() -> new NotFoundException(format("ALM setting with key '%s' cannot be found", almSettingKey))); + AlmSettingDto almSetting = almSettingsSupport.getAlmSetting(dbSession, almSettingKey); int projectsBound = dbClient.projectAlmSettingDao().countByAlmSetting(dbSession, almSetting); return CountBindingWsResponse.newBuilder() .setKey(almSetting.getKey()) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateAzureAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateAzureAction.java index 5aa43a40b4e..f3c8c37b4d8 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateAzureAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateAzureAction.java @@ -27,9 +27,7 @@ import org.sonar.db.DbSession; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.server.user.UserSession; -import static java.lang.String.format; import static org.sonar.db.alm.setting.ALM.AZURE_DEVOPS; -import static org.sonar.db.alm.setting.ALM.GITHUB; public class CreateAzureAction implements AlmSettingsWsAction { @@ -38,10 +36,12 @@ public class CreateAzureAction implements AlmSettingsWsAction { private final DbClient dbClient; private UserSession userSession; + private final AlmSettingsSupport almSettingsSupport; - public CreateAzureAction(DbClient dbClient, UserSession userSession) { + public CreateAzureAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) { this.dbClient = dbClient; this.userSession = userSession; + this.almSettingsSupport = almSettingsSupport; } @Override @@ -74,10 +74,7 @@ public class CreateAzureAction implements AlmSettingsWsAction { String key = request.mandatoryParam(PARAM_KEY); String pat = request.mandatoryParam(PARAM_PERSONAL_ACCESS_TOKEN); try (DbSession dbSession = dbClient.openSession(false)) { - dbClient.almSettingDao().selectByKey(dbSession, key) - .ifPresent(almSetting -> { - throw new IllegalArgumentException(format("An ALM setting with key '%s' already exist", almSetting.getKey())); - }); + almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, key); dbClient.almSettingDao().insert(dbSession, new AlmSettingDto() .setAlm(AZURE_DEVOPS) .setKey(key) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateBitBucketAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateBitBucketAction.java index 7271920449e..8dabed7ae48 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateBitBucketAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateBitBucketAction.java @@ -27,8 +27,6 @@ import org.sonar.db.DbSession; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.server.user.UserSession; -import static java.lang.String.format; -import static org.sonar.db.alm.setting.ALM.AZURE_DEVOPS; import static org.sonar.db.alm.setting.ALM.BITBUCKET; public class CreateBitBucketAction implements AlmSettingsWsAction { @@ -39,10 +37,12 @@ public class CreateBitBucketAction implements AlmSettingsWsAction { private final DbClient dbClient; private UserSession userSession; + private final AlmSettingsSupport almSettingsSupport; - public CreateBitBucketAction(DbClient dbClient, UserSession userSession) { + public CreateBitBucketAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) { this.dbClient = dbClient; this.userSession = userSession; + this.almSettingsSupport = almSettingsSupport; } @Override @@ -80,10 +80,7 @@ public class CreateBitBucketAction implements AlmSettingsWsAction { String url = request.mandatoryParam(PARAM_URL); String pat = request.mandatoryParam(PARAM_PERSONAL_ACCESS_TOKEN); try (DbSession dbSession = dbClient.openSession(false)) { - dbClient.almSettingDao().selectByKey(dbSession, key) - .ifPresent(almSetting -> { - throw new IllegalArgumentException(format("An ALM setting with key '%s' already exist", almSetting.getKey())); - }); + almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, key); dbClient.almSettingDao().insert(dbSession, new AlmSettingDto() .setAlm(BITBUCKET) .setKey(key) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateGithubAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateGithubAction.java index 8a98643bef5..605498ec45e 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateGithubAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/CreateGithubAction.java @@ -27,7 +27,6 @@ import org.sonar.db.DbSession; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.server.user.UserSession; -import static java.lang.String.format; import static org.sonar.db.alm.setting.ALM.GITHUB; public class CreateGithubAction implements AlmSettingsWsAction { @@ -39,10 +38,12 @@ public class CreateGithubAction implements AlmSettingsWsAction { private final DbClient dbClient; private final UserSession userSession; + private final AlmSettingsSupport almSettingsSupport; - public CreateGithubAction(DbClient dbClient, UserSession userSession) { + public CreateGithubAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) { this.dbClient = dbClient; this.userSession = userSession; + this.almSettingsSupport = almSettingsSupport; } @Override @@ -85,10 +86,7 @@ public class CreateGithubAction implements AlmSettingsWsAction { String appId = request.mandatoryParam(PARAM_APP_ID); String privateKey = request.mandatoryParam(PARAM_PRIVATE_KEY); try (DbSession dbSession = dbClient.openSession(false)) { - dbClient.almSettingDao().selectByKey(dbSession, key) - .ifPresent(almSetting -> { - throw new IllegalArgumentException(format("An ALM setting with key '%s' already exist", almSetting.getKey())); - }); + almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, key); dbClient.almSettingDao().insert(dbSession, new AlmSettingDto() .setAlm(GITHUB) .setKey(key) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/DeleteAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/DeleteAction.java index 62c3ef47df7..232e85425d7 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/DeleteAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/DeleteAction.java @@ -25,22 +25,20 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.alm.setting.AlmSettingDto; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; -import static java.lang.String.format; - public class DeleteAction implements AlmSettingsWsAction { - private static final String PARAM_KEY = "key"; private final DbClient dbClient; private final UserSession userSession; + private final AlmSettingsSupport almSettingsSupport; - public DeleteAction(DbClient dbClient, UserSession userSession) { + public DeleteAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) { this.dbClient = dbClient; this.userSession = userSession; + this.almSettingsSupport = almSettingsSupport; } @Override @@ -69,8 +67,7 @@ public class DeleteAction implements AlmSettingsWsAction { private void doHandle(Request request) { String key = request.mandatoryParam(PARAM_KEY); try (DbSession dbSession = dbClient.openSession(false)) { - AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, key) - .orElseThrow(() -> new NotFoundException(format("No ALM setting with key '%s' has been found", key))); + AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key); dbClient.projectAlmSettingDao().deleteByAlmSetting(dbSession, almSettingDto); dbClient.almSettingDao().delete(dbSession, almSettingDto); dbSession.commit(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/SetAzureBindingAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/SetAzureBindingAction.java new file mode 100644 index 00000000000..31b7e0e1db2 --- /dev/null +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/SetAzureBindingAction.java @@ -0,0 +1,81 @@ +/* + * 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.almsettings; + +import org.sonar.api.server.ws.Request; +import org.sonar.api.server.ws.Response; +import org.sonar.api.server.ws.WebService; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.alm.setting.AlmSettingDto; +import org.sonar.db.alm.setting.ProjectAlmSettingDto; +import org.sonar.db.component.ComponentDto; + +public class SetAzureBindingAction implements AlmSettingsWsAction { + + private static final String PARAM_ALM_SETTING = "almSetting"; + private static final String PARAM_PROJECT = "project"; + + private final DbClient dbClient; + private final AlmSettingsSupport almSettingsSupport; + + public SetAzureBindingAction(DbClient dbClient, AlmSettingsSupport almSettingsSupport) { + this.dbClient = dbClient; + this.almSettingsSupport = almSettingsSupport; + } + + @Override + public void define(WebService.NewController context) { + WebService.NewAction action = context.createAction("set_azure_binding") + .setDescription("Bind a Azure DevOps ALM instance to a project.<br/>" + + "If the project was already bound to a previous Azure DevOps ALM instance, the binding will be updated to the new one." + + "Requires the 'Administer' permission on the project") + .setPost(true) + .setSince("8.1") + .setHandler(this); + + action.createParam(PARAM_ALM_SETTING) + .setRequired(true) + .setDescription("GitHub ALM setting key"); + action.createParam(PARAM_PROJECT) + .setRequired(true) + .setDescription("Project key"); + } + + @Override + public void handle(Request request, Response response) throws Exception { + doHandle(request); + response.noContent(); + } + + private void doHandle(Request request) { + String almSetting = request.mandatoryParam(PARAM_ALM_SETTING); + String projectKey = request.mandatoryParam(PARAM_PROJECT); + try (DbSession dbSession = dbClient.openSession(false)) { + ComponentDto project = almSettingsSupport.getProject(dbSession, projectKey); + AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, almSetting); + dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, new ProjectAlmSettingDto() + .setProjectUuid(project.uuid()) + .setAlmSettingUuid(almSettingDto.getUuid())); + dbSession.commit(); + } + } + +} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/SetGithubBindingAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/SetGithubBindingAction.java index 6740ee25cca..bc704c27f15 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/SetGithubBindingAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/SetGithubBindingAction.java @@ -27,12 +27,6 @@ import org.sonar.db.DbSession; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.db.alm.setting.ProjectAlmSettingDto; import org.sonar.db.component.ComponentDto; -import org.sonar.server.component.ComponentFinder; -import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.user.UserSession; - -import static java.lang.String.format; -import static org.sonar.api.web.UserRole.ADMIN; public class SetGithubBindingAction implements AlmSettingsWsAction { @@ -41,13 +35,11 @@ public class SetGithubBindingAction implements AlmSettingsWsAction { private static final String PARAM_REPOSITORY = "repository"; private final DbClient dbClient; - private final UserSession userSession; - private final ComponentFinder componentFinder; + private final AlmSettingsSupport almSettingsSupport; - public SetGithubBindingAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder) { + public SetGithubBindingAction(DbClient dbClient, AlmSettingsSupport almSettingsSupport) { this.dbClient = dbClient; - this.userSession = userSession; - this.componentFinder = componentFinder; + this.almSettingsSupport = almSettingsSupport; } @Override @@ -83,10 +75,8 @@ public class SetGithubBindingAction implements AlmSettingsWsAction { String projectKey = request.mandatoryParam(PARAM_PROJECT); String repository = request.mandatoryParam(PARAM_REPOSITORY); try (DbSession dbSession = dbClient.openSession(false)) { - ComponentDto project = componentFinder.getByKey(dbSession, projectKey); - userSession.checkComponentPermission(ADMIN, project); - AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSetting) - .orElseThrow(() -> new NotFoundException(format("No ALM setting with almSetting '%s' has been found", almSetting))); + ComponentDto project = almSettingsSupport.getProject(dbSession, projectKey); + AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, almSetting); dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, new ProjectAlmSettingDto() .setProjectUuid(project.uuid()) .setAlmSettingUuid(almSettingDto.getUuid()) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateAzureAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateAzureAction.java index 8425a189399..b5beeab5f07 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateAzureAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateAzureAction.java @@ -25,10 +25,8 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.alm.setting.AlmSettingDto; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; -import static java.lang.String.format; import static org.apache.commons.lang.StringUtils.isNotBlank; public class UpdateAzureAction implements AlmSettingsWsAction { @@ -39,10 +37,12 @@ public class UpdateAzureAction implements AlmSettingsWsAction { private final DbClient dbClient; private UserSession userSession; + private final AlmSettingsSupport almSettingsSupport; - public UpdateAzureAction(DbClient dbClient, UserSession userSession) { + public UpdateAzureAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) { this.dbClient = dbClient; this.userSession = userSession; + this.almSettingsSupport = almSettingsSupport; } @Override @@ -81,15 +81,10 @@ public class UpdateAzureAction implements AlmSettingsWsAction { String pat = request.mandatoryParam(PARAM_PERSONAL_ACCESS_TOKEN); try (DbSession dbSession = dbClient.openSession(false)) { - AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, key) - .orElseThrow(() -> new NotFoundException(format("No ALM setting with key '%s' has been found", key))); + AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key); if (isNotBlank(newKey) && !newKey.equals(key)) { - dbClient.almSettingDao().selectByKey(dbSession, newKey) - .ifPresent(almSetting -> { - throw new IllegalArgumentException(format("ALM setting with key '%s' already exists", almSetting.getKey())); - }); + almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey); } - dbClient.almSettingDao().update(dbSession, almSettingDto .setKey(isNotBlank(newKey) ? newKey : key) .setPersonalAccessToken(pat)); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateBitbucketAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateBitbucketAction.java index 7661c903aaf..58aee2f9b6f 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateBitbucketAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateBitbucketAction.java @@ -25,10 +25,8 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.alm.setting.AlmSettingDto; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; -import static java.lang.String.format; import static org.apache.commons.lang.StringUtils.isNotBlank; public class UpdateBitbucketAction implements AlmSettingsWsAction { @@ -40,10 +38,12 @@ public class UpdateBitbucketAction implements AlmSettingsWsAction { private final DbClient dbClient; private UserSession userSession; + private final AlmSettingsSupport almSettingsSupport; - public UpdateBitbucketAction(DbClient dbClient, UserSession userSession) { + public UpdateBitbucketAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) { this.dbClient = dbClient; this.userSession = userSession; + this.almSettingsSupport = almSettingsSupport; } @Override @@ -87,15 +87,10 @@ public class UpdateBitbucketAction implements AlmSettingsWsAction { String pat = request.mandatoryParam(PARAM_PERSONAL_ACCESS_TOKEN); try (DbSession dbSession = dbClient.openSession(false)) { - AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, key) - .orElseThrow(() -> new NotFoundException(format("No ALM setting with key '%s' has been found", key))); + AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key); if (isNotBlank(newKey) && !newKey.equals(key)) { - dbClient.almSettingDao().selectByKey(dbSession, newKey) - .ifPresent(almSetting -> { - throw new IllegalArgumentException(format("ALM setting with key '%s' already exists", almSetting.getKey())); - }); + almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey); } - dbClient.almSettingDao().update(dbSession, almSettingDto .setKey(isNotBlank(newKey) ? newKey : key) .setUrl(url) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateGitHubAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateGitHubAction.java index face95be6bb..5b4f388c504 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateGitHubAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/UpdateGitHubAction.java @@ -25,10 +25,8 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.alm.setting.AlmSettingDto; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; -import static java.lang.String.format; import static org.apache.commons.lang.StringUtils.isNotBlank; public class UpdateGitHubAction implements AlmSettingsWsAction { @@ -41,10 +39,12 @@ public class UpdateGitHubAction implements AlmSettingsWsAction { private final DbClient dbClient; private final UserSession userSession; + private final AlmSettingsSupport almSettingsSupport; - public UpdateGitHubAction(DbClient dbClient, UserSession userSession) { + public UpdateGitHubAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) { this.dbClient = dbClient; this.userSession = userSession; + this.almSettingsSupport = almSettingsSupport; } @Override @@ -93,14 +93,9 @@ public class UpdateGitHubAction implements AlmSettingsWsAction { String privateKey = request.mandatoryParam(PARAM_PRIVATE_KEY); try (DbSession dbSession = dbClient.openSession(false)) { - - AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, key) - .orElseThrow(() -> new NotFoundException(format("No ALM setting with key '%s' has been found", key))); + AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key); if (isNotBlank(newKey) && !newKey.equals(key)) { - dbClient.almSettingDao().selectByKey(dbSession, newKey) - .ifPresent(almSetting -> { - throw new IllegalArgumentException(format("ALM setting with key '%s' already exists", almSetting.getKey())); - }); + almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey); } dbClient.almSettingDao().update(dbSession, almSettingDto .setKey(isNotBlank(newKey) ? newKey : key) diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CountBindingActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CountBindingActionTest.java index fbc638ff1ce..8ea3c03fc45 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CountBindingActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CountBindingActionTest.java @@ -28,6 +28,7 @@ import org.sonar.db.DbTester; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.db.component.ComponentDto; import org.sonar.db.user.UserDto; +import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; @@ -47,7 +48,8 @@ public class CountBindingActionTest { @Rule public DbTester db = DbTester.create(); - private WsActionTester ws = new WsActionTester(new CountBindingAction(db.getDbClient(), userSession)); + private WsActionTester ws = new WsActionTester(new CountBindingAction(db.getDbClient(), userSession, + new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null)))); @Test public void count_binding() { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateAzureActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateAzureActionTest.java index cb023d3da17..9bb7f646316 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateAzureActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateAzureActionTest.java @@ -27,6 +27,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbTester; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.db.user.UserDto; +import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; @@ -43,7 +44,8 @@ public class CreateAzureActionTest { @Rule public DbTester db = DbTester.create(); - private WsActionTester ws = new WsActionTester(new CreateAzureAction(db.getDbClient(), userSession)); + private WsActionTester ws = new WsActionTester(new CreateAzureAction(db.getDbClient(), userSession, + new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null)))); @Test public void create() { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateBitbucketActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateBitbucketActionTest.java index a698e8ba64e..1eb8de7f220 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateBitbucketActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateBitbucketActionTest.java @@ -27,6 +27,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbTester; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.db.user.UserDto; +import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; @@ -43,7 +44,8 @@ public class CreateBitbucketActionTest { @Rule public DbTester db = DbTester.create(); - private WsActionTester ws = new WsActionTester(new CreateBitBucketAction(db.getDbClient(), userSession)); + private WsActionTester ws = new WsActionTester(new CreateBitBucketAction(db.getDbClient(), userSession, + new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null)))); @Test public void create() { @@ -90,6 +92,7 @@ public class CreateBitbucketActionTest { .setParam("personalAccessToken", "98765432100") .execute(); } + @Test public void definition() { WebService.Action def = ws.getDef(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateGithubActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateGithubActionTest.java index 3e48df770b5..c793c356ad0 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateGithubActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/CreateGithubActionTest.java @@ -27,6 +27,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbTester; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.db.user.UserDto; +import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; @@ -43,7 +44,8 @@ public class CreateGithubActionTest { @Rule public DbTester db = DbTester.create(); - private WsActionTester ws = new WsActionTester(new CreateGithubAction(db.getDbClient(), userSession)); + private WsActionTester ws = new WsActionTester(new CreateGithubAction(db.getDbClient(), userSession, + new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null)))); @Test public void create() { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/DeleteActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/DeleteActionTest.java index 3e6a7fd65ff..8e0298b8bbf 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/DeleteActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/DeleteActionTest.java @@ -27,6 +27,7 @@ import org.sonar.db.DbTester; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.db.component.ComponentDto; import org.sonar.db.user.UserDto; +import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; @@ -44,7 +45,8 @@ public class DeleteActionTest { @Rule public DbTester db = DbTester.create(); - private WsActionTester ws = new WsActionTester(new DeleteAction(db.getDbClient(), userSession)); + private WsActionTester ws = new WsActionTester(new DeleteAction(db.getDbClient(), userSession, + new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null)))); @Test public void delete() { @@ -86,7 +88,7 @@ public class DeleteActionTest { userSession.logIn(user).setSystemAdministrator(); expectedException.expect(NotFoundException.class); - expectedException.expectMessage("No ALM setting with key 'unknown' has been found"); + expectedException.expectMessage("ALM setting with key 'unknown' cannot be found"); ws.newRequest() .setParam("key", "unknown") diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/SetGithubBindingActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/SetGithubBindingActionTest.java index 78250eb1964..fea1a14657b 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/SetGithubBindingActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/SetGithubBindingActionTest.java @@ -48,7 +48,8 @@ public class SetGithubBindingActionTest { @Rule public DbTester db = DbTester.create(); - private WsActionTester ws = new WsActionTester(new SetGithubBindingAction(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null))); + private WsActionTester ws = new WsActionTester(new SetGithubBindingAction(db.getDbClient(), + new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null)))); @Test public void set_github_project_binding() { @@ -95,7 +96,7 @@ public class SetGithubBindingActionTest { userSession.logIn(user).addProjectPermission(ADMIN, project); expectedException.expect(NotFoundException.class); - expectedException.expectMessage("No ALM setting with almSetting 'unknown' has been found"); + expectedException.expectMessage("ALM setting with key 'unknown' cannot be found"); ws.newRequest() .setParam("almSetting", "unknown") diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateAzureActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateAzureActionTest.java index 73bd0a6a53e..02d3e42ac00 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateAzureActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateAzureActionTest.java @@ -27,6 +27,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbTester; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.db.user.UserDto; +import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; @@ -35,7 +36,6 @@ import org.sonar.server.ws.WsActionTester; import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.groups.Tuple.tuple; -import static org.sonar.db.almsettings.AlmSettingsTesting.newAzureAlmSettingDto; public class UpdateAzureActionTest { @@ -46,7 +46,8 @@ public class UpdateAzureActionTest { @Rule public DbTester db = DbTester.create(); - private WsActionTester ws = new WsActionTester(new UpdateAzureAction(db.getDbClient(), userSession)); + private WsActionTester ws = new WsActionTester(new UpdateAzureAction(db.getDbClient(), userSession, + new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null)))); @Test public void update() { @@ -87,7 +88,7 @@ public class UpdateAzureActionTest { userSession.logIn(user).setSystemAdministrator(); expectedException.expect(NotFoundException.class); - expectedException.expectMessage("No ALM setting with key 'unknown' has been found"); + expectedException.expectMessage("ALM setting with key 'unknown' cannot be found"); ws.newRequest() .setParam("key", "unknown") @@ -103,7 +104,7 @@ public class UpdateAzureActionTest { AlmSettingDto almSetting2 = db.almSettings().insertAzureAlmSetting(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(format("ALM setting with key '%s' already exists", almSetting2.getKey())); + expectedException.expectMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey())); ws.newRequest() .setParam("key", almSetting1.getKey()) diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateBitbucketActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateBitbucketActionTest.java index 054c68d741b..3140a8e3093 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateBitbucketActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateBitbucketActionTest.java @@ -27,6 +27,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbTester; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.db.user.UserDto; +import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; @@ -35,7 +36,6 @@ import org.sonar.server.ws.WsActionTester; import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.groups.Tuple.tuple; -import static org.sonar.db.almsettings.AlmSettingsTesting.newBitbucketAlmSettingDto; public class UpdateBitbucketActionTest { @@ -46,7 +46,8 @@ public class UpdateBitbucketActionTest { @Rule public DbTester db = DbTester.create(); - private WsActionTester ws = new WsActionTester(new UpdateBitbucketAction(db.getDbClient(), userSession)); + private WsActionTester ws = new WsActionTester(new UpdateBitbucketAction(db.getDbClient(), userSession, + new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null)))); @Test public void update() { @@ -89,7 +90,7 @@ public class UpdateBitbucketActionTest { userSession.logIn(user).setSystemAdministrator(); expectedException.expect(NotFoundException.class); - expectedException.expectMessage("No ALM setting with key 'unknown' has been found"); + expectedException.expectMessage("ALM setting with key 'unknown' cannot be found"); ws.newRequest() .setParam("key", "unknown") @@ -106,7 +107,7 @@ public class UpdateBitbucketActionTest { AlmSettingDto almSetting2 = db.almSettings().insertBitbucketAlmSetting(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(format("ALM setting with key '%s' already exists", almSetting2.getKey())); + expectedException.expectMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey())); ws.newRequest() .setParam("key", almSetting1.getKey()) diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateGithubActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateGithubActionTest.java index c0deea9aa10..8bcef3e93d2 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateGithubActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/UpdateGithubActionTest.java @@ -27,6 +27,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbTester; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.db.user.UserDto; +import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; @@ -45,7 +46,8 @@ public class UpdateGithubActionTest { @Rule public DbTester db = DbTester.create(); - private WsActionTester ws = new WsActionTester(new UpdateGitHubAction(db.getDbClient(), userSession)); + private WsActionTester ws = new WsActionTester(new UpdateGitHubAction(db.getDbClient(), userSession, + new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null)))); @Test public void update() { @@ -90,7 +92,7 @@ public class UpdateGithubActionTest { userSession.logIn(user).setSystemAdministrator(); expectedException.expect(NotFoundException.class); - expectedException.expectMessage("No ALM setting with key 'unknown' has been found"); + expectedException.expectMessage("ALM setting with key 'unknown' cannot be found"); ws.newRequest() .setParam("key", "unknown") @@ -109,7 +111,7 @@ public class UpdateGithubActionTest { AlmSettingDto almSetting2 = db.almSettings().insertGitHubAlmSetting(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(format("ALM setting with key '%s' already exists", almSetting2.getKey())); + expectedException.expectMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey())); ws.newRequest() .setParam("key", almSetting1.getKey()) |