Browse Source

SONAR-21898 make ALM/DOP types consistent between v1 and v2 endpoints.

tags/10.5.0.89998
Wojtek Wajerowicz 3 weeks ago
parent
commit
819c96c042

+ 39
- 0
server/sonar-webserver-common/src/main/java/org/sonar/server/common/AlmSettingMapper.java View File

@@ -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;
};
}
}

+ 40
- 0
server/sonar-webserver-common/src/test/java/org/sonar/server/common/AlmSettingMapperTest.java View File

@@ -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);
}

}

+ 3
- 3
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsController.java View File

@@ -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());
}

}

+ 1
- 1
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/dop/response/DopSettingsResource.java View File

@@ -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

+ 18
- 7
server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/dop/controller/DefaultDopSettingsControllerTest.java View File

@@ -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);

+ 0
- 18
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsSupport.java View File

@@ -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.");
}

+ 2
- 2
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GetBindingAction.java View File

@@ -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);

+ 2
- 1
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ListAction.java View File

@@ -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() + "/");

Loading…
Cancel
Save