aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-webapi
diff options
context:
space:
mode:
authorZipeng WU <zipeng.wu@sonarsource.com>2021-04-15 15:46:35 +0200
committersonartech <sonartech@sonarsource.com>2021-04-16 20:03:44 +0000
commitc1a09c7964cb3e000ba992c97a592d8e4110c256 (patch)
treea6e9243525cf1e4e46f69cd825b1b5dc87573cf1 /server/sonar-webserver-webapi
parent3ebbd70a22802524f2fefe133cb51c3785f655ee (diff)
downloadsonarqube-c1a09c7964cb3e000ba992c97a592d8e4110c256.tar.gz
sonarqube-c1a09c7964cb3e000ba992c97a592d8e4110c256.zip
SONAR-14683 fix cannot delete an ALM configuration in Community
Diffstat (limited to 'server/sonar-webserver-webapi')
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsWsModule.java1
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/CountBindingAction.java80
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java2
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/CountBindingActionTest.java149
-rw-r--r--server/sonar-webserver-webapi/src/test/resources/org/sonar/server/almsettings/ws/count_binding-example.json4
5 files changed, 235 insertions, 1 deletions
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsWsModule.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsWsModule.java
index dc5335abd80..c00fea8da2a 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsWsModule.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsWsModule.java
@@ -31,6 +31,7 @@ public class AlmSettingsWsModule extends Module {
ListAction.class,
ListDefinitionsAction.class,
ValidateAction.class,
+ CountBindingAction.class,
GetBindingAction.class,
//Azure alm settings,
CreateAzureAction.class,
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/CountBindingAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/CountBindingAction.java
new file mode 100644
index 00000000000..751c3738405
--- /dev/null
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/CountBindingAction.java
@@ -0,0 +1,80 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.ws;
+
+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.server.user.UserSession;
+import org.sonarqube.ws.AlmSettings.CountBindingWsResponse;
+
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+
+public class CountBindingAction implements AlmSettingsWsAction {
+
+ private static final String PARAM_ALM_SETTING = "almSetting";
+
+ private final DbClient dbClient;
+ private final UserSession userSession;
+ private final AlmSettingsSupport almSettingsSupport;
+
+ public CountBindingAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) {
+ this.dbClient = dbClient;
+ this.userSession = userSession;
+ this.almSettingsSupport = almSettingsSupport;
+ }
+
+ @Override
+ public void define(WebService.NewController context) {
+ WebService.NewAction action = context.createAction("count_binding")
+ .setDescription("Count number of project bound to an ALM setting.<br/>" +
+ "Requires the 'Administer System' permission")
+ .setSince("8.1")
+ .setResponseExample(getClass().getResource("count_binding-example.json"))
+ .setHandler(this);
+
+ action
+ .createParam(PARAM_ALM_SETTING)
+ .setDescription("ALM setting key")
+ .setRequired(true);
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ userSession.checkIsSystemAdministrator();
+ CountBindingWsResponse wsResponse = doHandle(request);
+ writeProtobuf(wsResponse, request, response);
+ }
+
+ private CountBindingWsResponse doHandle(Request request) {
+ String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ AlmSettingDto almSetting = almSettingsSupport.getAlmSetting(dbSession, almSettingKey);
+ int projectsBound = dbClient.projectAlmSettingDao().countByAlmSetting(dbSession, almSetting);
+ return CountBindingWsResponse.newBuilder()
+ .setKey(almSetting.getKey())
+ .setProjects(projectsBound)
+ .build();
+ }
+ }
+}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java
index d1b9d32d877..669a453b7e6 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java
@@ -31,7 +31,7 @@ public class AlmSettingsWsModuleTest {
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new AlmSettingsWsModule().configure(container);
- assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 15);
+ assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 16);
}
} \ No newline at end of file
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/CountBindingActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/CountBindingActionTest.java
new file mode 100644
index 00000000000..888000d4a7a
--- /dev/null
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/CountBindingActionTest.java
@@ -0,0 +1,149 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.project.ProjectDto;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
+import org.sonar.server.almsettings.ws.AlmSettingsSupport;
+import org.sonar.server.almsettings.ws.CountBindingAction;
+import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.AlmSettings.CountBindingWsResponse;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.groups.Tuple.tuple;
+import static org.mockito.Mockito.mock;
+import static org.sonar.test.JsonAssert.assertJson;
+
+public class CountBindingActionTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+ @Rule
+ public DbTester db = DbTester.create();
+
+ private WsActionTester ws = new WsActionTester(new CountBindingAction(db.getDbClient(), userSession,
+ new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
+ mock(MultipleAlmFeatureProvider.class))));
+
+ @Test
+ public void count_github_binding() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+ AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
+ ProjectDto project1 = db.components().insertPrivateProjectDto();
+ ProjectDto project2 = db.components().insertPrivateProjectDto();
+ db.almSettings().insertGitHubProjectAlmSetting(almSetting, project1);
+ db.almSettings().insertGitHubProjectAlmSetting(almSetting, project2);
+
+ CountBindingWsResponse response = ws.newRequest()
+ .setParam("almSetting", almSetting.getKey())
+ .executeProtobuf(CountBindingWsResponse.class);
+
+ assertThat(response.getKey()).isEqualTo(almSetting.getKey());
+ assertThat(response.getProjects()).isEqualTo(2);
+ }
+
+ @Test
+ public void count_azure_binding() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+ AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
+ ProjectDto project1 = db.components().insertPrivateProjectDto();
+ db.almSettings().insertAzureProjectAlmSetting(almSetting, project1);
+
+ CountBindingWsResponse response = ws.newRequest()
+ .setParam("almSetting", almSetting.getKey())
+ .executeProtobuf(CountBindingWsResponse.class);
+
+ assertThat(response.getKey()).isEqualTo(almSetting.getKey());
+ assertThat(response.getProjects()).isEqualTo(1);
+ }
+
+ @Test
+ public void fail_when_alm_setting_does_not_exist() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
+
+ ws.newRequest()
+ .setParam("almSetting", "unknown")
+ .execute();
+ }
+
+ @Test
+ public void fail_when_missing_system_administer_permission() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user);
+ AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
+
+ expectedException.expect(ForbiddenException.class);
+
+ ws.newRequest()
+ .setParam("almSetting", githubAlmSetting.getKey())
+ .execute();
+ }
+
+ @Test
+ public void json_example() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+ AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(
+ almSettingDto -> almSettingDto
+ .setKey("GitHub Server - Dev Team")
+ .setAppId("12345")
+ .setPrivateKey("54684654"));
+ db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, db.components().insertPrivateProjectDto());
+ db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, db.components().insertPrivateProjectDto());
+ db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, db.components().insertPrivateProjectDto());
+
+ String response = ws.newRequest()
+ .setParam("almSetting", githubAlmSetting.getKey())
+ .execute().getInput();
+
+ assertJson(response).isSimilarTo(getClass().getResource("count_binding-example.json"));
+ }
+
+ @Test
+ public void definition() {
+ WebService.Action def = ws.getDef();
+
+ assertThat(def.since()).isEqualTo("8.1");
+ assertThat(def.isPost()).isFalse();
+ assertThat(def.params())
+ .extracting(WebService.Param::key, WebService.Param::isRequired)
+ .containsExactlyInAnyOrder(tuple("almSetting", true));
+ }
+
+}
diff --git a/server/sonar-webserver-webapi/src/test/resources/org/sonar/server/almsettings/ws/count_binding-example.json b/server/sonar-webserver-webapi/src/test/resources/org/sonar/server/almsettings/ws/count_binding-example.json
new file mode 100644
index 00000000000..d6c189ec17e
--- /dev/null
+++ b/server/sonar-webserver-webapi/src/test/resources/org/sonar/server/almsettings/ws/count_binding-example.json
@@ -0,0 +1,4 @@
+{
+ "key": "GitHub Server - Dev Team",
+ "projects": 3
+}