aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com>2024-04-11 09:55:12 +0200
committersonartech <sonartech@sonarsource.com>2024-04-11 20:02:47 +0000
commit819c96c042e6bfafb924cc40329e59f85a80d2d7 (patch)
tree93340f294c9d89317b46a3ab50f86f1dbefaa10d
parentef0cf89eccf70533372c46d150e38ee05ac70d3e (diff)
downloadsonarqube-819c96c042e6bfafb924cc40329e59f85a80d2d7.tar.gz
sonarqube-819c96c042e6bfafb924cc40329e59f85a80d2d7.zip
SONAR-21898 make ALM/DOP types consistent between v1 and v2 endpoints.
-rw-r--r--server/sonar-webserver-common/src/main/java/org/sonar/server/common/AlmSettingMapper.java39
-rw-r--r--server/sonar-webserver-common/src/test/java/org/sonar/server/common/AlmSettingMapperTest.java40
-rw-r--r--server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsController.java6
-rw-r--r--server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/response/DopSettingsResource.java2
-rw-r--r--server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsControllerTest.java25
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsSupport.java18
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GetBindingAction.java4
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ListAction.java3
8 files changed, 105 insertions, 32 deletions
diff --git a/server/sonar-webserver-common/src/main/java/org/sonar/server/common/AlmSettingMapper.java b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/AlmSettingMapper.java
new file mode 100644
index 00000000000..e6397e58fbc
--- /dev/null
+++ b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/AlmSettingMapper.java
@@ -0,0 +1,39 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.common;
+
+import org.sonar.db.alm.setting.ALM;
+import org.sonarqube.ws.AlmSettings;
+
+public class AlmSettingMapper {
+
+ private AlmSettingMapper() {
+ }
+
+ public static AlmSettings.Alm toResponseAlm(ALM alm) {
+ return switch (alm) {
+ case GITHUB -> AlmSettings.Alm.github;
+ case BITBUCKET -> AlmSettings.Alm.bitbucket;
+ case BITBUCKET_CLOUD -> AlmSettings.Alm.bitbucketcloud;
+ case AZURE_DEVOPS -> AlmSettings.Alm.azure;
+ case GITLAB -> AlmSettings.Alm.gitlab;
+ };
+ }
+}
diff --git a/server/sonar-webserver-common/src/test/java/org/sonar/server/common/AlmSettingMapperTest.java b/server/sonar-webserver-common/src/test/java/org/sonar/server/common/AlmSettingMapperTest.java
new file mode 100644
index 00000000000..0d14b2e9126
--- /dev/null
+++ b/server/sonar-webserver-common/src/test/java/org/sonar/server/common/AlmSettingMapperTest.java
@@ -0,0 +1,40 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.common;
+
+import org.junit.jupiter.api.Test;
+import org.sonar.db.alm.setting.ALM;
+import org.sonarqube.ws.AlmSettings;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class AlmSettingMapperTest {
+
+
+ @Test
+ void toResponseAlm_shouldCorrectlyMapAlms() {
+ assertThat(AlmSettingMapper.toResponseAlm(ALM.GITHUB)).isEqualTo(AlmSettings.Alm.github);
+ assertThat(AlmSettingMapper.toResponseAlm(ALM.BITBUCKET)).isEqualTo(AlmSettings.Alm.bitbucket);
+ assertThat(AlmSettingMapper.toResponseAlm(ALM.BITBUCKET_CLOUD)).isEqualTo(AlmSettings.Alm.bitbucketcloud);
+ assertThat(AlmSettingMapper.toResponseAlm(ALM.AZURE_DEVOPS)).isEqualTo(AlmSettings.Alm.azure);
+ assertThat(AlmSettingMapper.toResponseAlm(ALM.GITLAB)).isEqualTo(AlmSettings.Alm.gitlab);
+ }
+
+} \ No newline at end of file
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsController.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsController.java
index 49d4f1b697a..fa2d680a89c 100644
--- a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsController.java
+++ b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsController.java
@@ -29,6 +29,7 @@ import org.sonar.server.v2.api.dop.response.DopSettingsRestResponse;
import org.sonar.server.v2.api.response.PageRestResponse;
import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
+import static org.sonar.server.common.AlmSettingMapper.toResponseAlm;
public class DefaultDopSettingsController implements DopSettingsController {
@@ -57,11 +58,10 @@ public class DefaultDopSettingsController implements DopSettingsController {
private static DopSettingsResource toDopSettingsResource(AlmSettingDto almSettingDto) {
return new DopSettingsResource(
almSettingDto.getUuid(),
- almSettingDto.getRawAlm(),
+ toResponseAlm(almSettingDto.getAlm()).name(),
almSettingDto.getKey(),
almSettingDto.getUrl(),
- almSettingDto.getAppId()
- );
+ almSettingDto.getAppId());
}
}
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/response/DopSettingsResource.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/response/DopSettingsResource.java
index 830723f4e09..083d0eae56c 100644
--- a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/response/DopSettingsResource.java
+++ b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/response/DopSettingsResource.java
@@ -25,7 +25,7 @@ import javax.annotation.Nullable;
public record DopSettingsResource(
@Schema(accessMode = Schema.AccessMode.READ_ONLY)
String id,
- @Schema(description = "Supported DevOps Platform are: github, gitlab, azure_devops, bitbucket_cloud, bitbucket_server")
+ @Schema(description = "Supported DevOps Platform are: github, gitlab, azure, bitbucketcloud, bitbucket_server")
String type,
String key,
@Nullable
diff --git a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsControllerTest.java b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsControllerTest.java
index 70f6c72d509..52f9c4d4d7a 100644
--- a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsControllerTest.java
+++ b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsControllerTest.java
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.server.tester.UserSessionRule;
import org.sonar.server.v2.api.ControllerTester;
@@ -70,10 +71,13 @@ class DefaultDopSettingsControllerTest {
@Test
void fetchAllDopSettings_whenDbClientReturnsData_returnsResponse() throws Exception {
+ AlmSettingDto almSettingDto1 = generateAlmSettingsDto("github");
+ AlmSettingDto almSettingDto2 = generateAlmSettingsDto("azure_devops");
+ AlmSettingDto almSettingDto3 = generateAlmSettingsDto("bitbucket_cloud");
List<AlmSettingDto> dopSettings = List.of(
- generateAlmSettingsDto("github"),
- generateAlmSettingsDto("azure"),
- generateAlmSettingsDto("bitbucket_cloud")
+ almSettingDto1,
+ almSettingDto2,
+ almSettingDto3
);
when(dbClient.almSettingDao().selectAll(dbSession)).thenReturn(dopSettings);
@@ -83,14 +87,21 @@ class DefaultDopSettingsControllerTest {
.andExpect(status().isOk())
.andReturn();
DopSettingsRestResponse response = gson.fromJson(mvcResult.getResponse().getContentAsString(), DopSettingsRestResponse.class);
+
+ List<DopSettingsResource> expectedDopSettings = List.of(
+ toDopSettingsResource(almSettingDto1, "github"),
+ toDopSettingsResource(almSettingDto2, "azure"),
+ toDopSettingsResource(almSettingDto3, "bitbucketcloud")
+ );
+
assertThat(response.dopSettings())
- .containsExactlyInAnyOrderElementsOf(dopSettings.stream().map(DefaultDopSettingsControllerTest::toDopSettingsResource).toList());
+ .containsExactlyInAnyOrderElementsOf(expectedDopSettings);
}
- private static DopSettingsResource toDopSettingsResource(AlmSettingDto almSettingDto) {
+ private static DopSettingsResource toDopSettingsResource(AlmSettingDto almSettingDto, String alm) {
return new DopSettingsResource(
almSettingDto.getUuid(),
- almSettingDto.getRawAlm(),
+ alm,
almSettingDto.getKey(),
almSettingDto.getUrl(),
almSettingDto.getAppId()
@@ -100,7 +111,7 @@ class DefaultDopSettingsControllerTest {
private AlmSettingDto generateAlmSettingsDto(String dopType) {
AlmSettingDto dto = mock();
when(dto.getUuid()).thenReturn("uuid_" + dopType);
- when(dto.getRawAlm()).thenReturn(dopType);
+ when(dto.getAlm()).thenReturn(ALM.fromId(dopType));
when(dto.getKey()).thenReturn("key_" + dopType);
when(dto.getUrl()).thenReturn("url_" + dopType);
when(dto.getAppId()).thenReturn("appId_" + dopType);
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsSupport.java
index b0b3c247a7f..1f30c3082f1 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsSupport.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsSupport.java
@@ -32,7 +32,6 @@ import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
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.apache.commons.lang.StringUtils.isEmpty;
@@ -95,23 +94,6 @@ public class AlmSettingsSupport {
.orElseThrow(() -> new NotFoundException(format("DevOps Platform setting with key '%s' cannot be found", almSetting)));
}
- public static AlmSettings.Alm toAlmWs(ALM alm) {
- switch (alm) {
- case GITHUB:
- return AlmSettings.Alm.github;
- case BITBUCKET:
- return AlmSettings.Alm.bitbucket;
- case BITBUCKET_CLOUD:
- return AlmSettings.Alm.bitbucketcloud;
- case AZURE_DEVOPS:
- return AlmSettings.Alm.azure;
- case GITLAB:
- return AlmSettings.Alm.gitlab;
- default:
- throw new IllegalStateException(format("Unknown DevOps Platform '%s'", alm.name()));
- }
- }
-
public void checkPrivateKeyOnUrlUpdate(AlmSettingDto almSettingDto, String url, @Nullable String privateKey) {
checkCredentialArtifactOnUrlUpdate(url, almSettingDto, privateKey, "Please provide the Private Key to update the URL.");
}
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GetBindingAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GetBindingAction.java
index a82ea1c33a7..0bd5ee82b70 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GetBindingAction.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GetBindingAction.java
@@ -36,7 +36,7 @@ import org.sonarqube.ws.AlmSettings.GetBindingWsResponse;
import static java.lang.String.format;
import static java.util.Optional.ofNullable;
import static org.sonar.api.web.UserRole.USER;
-import static org.sonar.server.almsettings.ws.AlmSettingsSupport.toAlmWs;
+import static org.sonar.server.common.AlmSettingMapper.toResponseAlm;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class GetBindingAction implements AlmSettingsWsAction {
@@ -89,7 +89,7 @@ public class GetBindingAction implements AlmSettingsWsAction {
.orElseThrow(() -> new IllegalStateException(format("DevOps Platform setting with uuid '%s' cannot be found", projectAlmSetting.getAlmSettingUuid())));
GetBindingWsResponse.Builder builder = GetBindingWsResponse.newBuilder()
- .setAlm(toAlmWs(almSetting.getAlm()))
+ .setAlm(toResponseAlm(almSetting.getAlm()))
.setKey(almSetting.getKey());
ofNullable(projectAlmSetting.getAlmRepo()).ifPresent(builder::setRepository);
ofNullable(almSetting.getUrl()).ifPresent(builder::setUrl);
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ListAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ListAction.java
index be4c674a26d..ddfd74da21c 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ListAction.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ListAction.java
@@ -38,6 +38,7 @@ import org.sonarqube.ws.AlmSettings.ListWsResponse;
import static java.util.Optional.ofNullable;
import static org.sonar.api.web.UserRole.ADMIN;
import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
+import static org.sonar.server.common.AlmSettingMapper.toResponseAlm;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class ListAction implements AlmSettingsWsAction {
@@ -98,7 +99,7 @@ public class ListAction implements AlmSettingsWsAction {
.map(almSetting -> {
AlmSetting.Builder almSettingBuilder = AlmSetting.newBuilder()
.setKey(almSetting.getKey())
- .setAlm(AlmSettingsSupport.toAlmWs(almSetting.getAlm()));
+ .setAlm(toResponseAlm(almSetting.getAlm()));
if (almSetting.getAlm() == ALM.BITBUCKET_CLOUD) {
almSettingBuilder.setUrl(BITBUCKETCLOUD_ROOT_URL + almSetting.getAppId() + "/");