aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-webapi-v2
diff options
context:
space:
mode:
authorWojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com>2024-04-09 11:38:59 +0200
committersonartech <sonartech@sonarsource.com>2024-04-10 20:02:55 +0000
commitfe9ee0e4738995cd12246662bdd5ae3ece7fcb5f (patch)
tree4b04e4aafad74464bb0f7f56c18f306b947971e5 /server/sonar-webserver-webapi-v2
parenta5b00addd3dd30161f0e7b8a1754ea74a80c5260 (diff)
downloadsonarqube-fe9ee0e4738995cd12246662bdd5ae3ece7fcb5f.tar.gz
sonarqube-fe9ee0e4738995cd12246662bdd5ae3ece7fcb5f.zip
SONAR-21819 Add projectKey to project-bindings endpoint responses.
Diffstat (limited to 'server/sonar-webserver-webapi-v2')
-rw-r--r--server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/projectbindings/controller/DefaultProjectBindingsController.java31
-rw-r--r--server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/projectbindings/model/ProjectBinding.java5
-rw-r--r--server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/projectbindings/controller/DefaultProjectBindingsControllerTest.java72
3 files changed, 69 insertions, 39 deletions
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/projectbindings/controller/DefaultProjectBindingsController.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/projectbindings/controller/DefaultProjectBindingsController.java
index 58c84b76cea..cef1c2ab574 100644
--- a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/projectbindings/controller/DefaultProjectBindingsController.java
+++ b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/projectbindings/controller/DefaultProjectBindingsController.java
@@ -24,6 +24,7 @@ import java.util.Optional;
import org.sonar.db.alm.setting.ProjectAlmSettingDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.common.SearchResults;
+import org.sonar.server.common.projectbindings.service.ProjectBindingInformation;
import org.sonar.server.common.projectbindings.service.ProjectBindingsSearchRequest;
import org.sonar.server.common.projectbindings.service.ProjectBindingsService;
import org.sonar.server.exceptions.NotFoundException;
@@ -54,34 +55,44 @@ public class DefaultProjectBindingsController implements ProjectBindingsControll
ProjectDto projectDto = projectBindingsService.findProjectFromBinding(projectAlmSettingDto.get())
.orElseThrow(() -> new IllegalStateException(String.format("Project (uuid '%s') not found for binding '%s'", projectAlmSettingDto.get().getProjectUuid(), id)));
userSession.checkEntityPermission(USER, projectDto);
- return toProjectBinding(projectAlmSettingDto.get());
+ return toProjectBinding(projectDto, projectAlmSettingDto.get());
} else {
throw new NotFoundException(String.format("Project binding '%s' not found", id));
}
}
+ private static ProjectBinding toProjectBinding(ProjectDto projectDto, ProjectAlmSettingDto projectAlmSettingDto) {
+ return new ProjectBinding(
+ projectAlmSettingDto.getUuid(),
+ projectAlmSettingDto.getAlmSettingUuid(),
+ projectAlmSettingDto.getProjectUuid(),
+ projectDto.getKey(),
+ projectAlmSettingDto.getAlmRepo(),
+ projectAlmSettingDto.getAlmSlug());
+ }
+
@Override
public ProjectBindingsSearchRestResponse searchProjectBindings(ProjectBindingsSearchRestRequest restRequest, RestPage restPage) {
userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
ProjectBindingsSearchRequest serviceRequest = new ProjectBindingsSearchRequest(restRequest.repository(), restRequest.dopSettingId(), restPage.pageIndex(), restPage.pageSize());
- SearchResults<ProjectAlmSettingDto> searchResults = projectBindingsService.findProjectBindingsByRequest(serviceRequest);
+ SearchResults<ProjectBindingInformation> searchResults = projectBindingsService.findProjectBindingsByRequest(serviceRequest);
List<ProjectBinding> projectBindings = toProjectBindings(searchResults);
return new ProjectBindingsSearchRestResponse(projectBindings, new PageRestResponse(restPage.pageIndex(), restPage.pageSize(), searchResults.total()));
}
- private static List<ProjectBinding> toProjectBindings(SearchResults<ProjectAlmSettingDto> searchResults) {
+ private static List<ProjectBinding> toProjectBindings(SearchResults<ProjectBindingInformation> searchResults) {
return searchResults.searchResults().stream()
.map(DefaultProjectBindingsController::toProjectBinding)
.toList();
}
- private static ProjectBinding toProjectBinding(ProjectAlmSettingDto dto) {
+ private static ProjectBinding toProjectBinding(ProjectBindingInformation projectBindingInformation) {
return new ProjectBinding(
- dto.getUuid(),
- dto.getAlmSettingUuid(),
- dto.getProjectUuid(),
- dto.getAlmRepo(),
- dto.getAlmSlug()
- );
+ projectBindingInformation.id(),
+ projectBindingInformation.devOpsPlatformSettingId(),
+ projectBindingInformation.projectId(),
+ projectBindingInformation.projectKey(),
+ projectBindingInformation.repository(),
+ projectBindingInformation.slug());
}
}
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/projectbindings/model/ProjectBinding.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/projectbindings/model/ProjectBinding.java
index 33cbde2320a..8f7e502d5ae 100644
--- a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/projectbindings/model/ProjectBinding.java
+++ b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/projectbindings/model/ProjectBinding.java
@@ -28,11 +28,14 @@ public record ProjectBinding (
String id,
@NotNull
- String dopSettings,
+ String devOpsPlatformSettingId,
@NotNull
String projectId,
+ @NotNull
+ String projectKey,
+
@Nullable
String repository,
diff --git a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/projectbindings/controller/DefaultProjectBindingsControllerTest.java b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/projectbindings/controller/DefaultProjectBindingsControllerTest.java
index 7912ad9228f..24216f7dced 100644
--- a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/projectbindings/controller/DefaultProjectBindingsControllerTest.java
+++ b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/projectbindings/controller/DefaultProjectBindingsControllerTest.java
@@ -27,6 +27,7 @@ import org.mockito.ArgumentCaptor;
import org.sonar.db.alm.setting.ProjectAlmSettingDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.common.SearchResults;
+import org.sonar.server.common.projectbindings.service.ProjectBindingInformation;
import org.sonar.server.common.projectbindings.service.ProjectBindingsSearchRequest;
import org.sonar.server.common.projectbindings.service.ProjectBindingsService;
import org.sonar.server.tester.UserSessionRule;
@@ -112,7 +113,10 @@ class DefaultProjectBindingsControllerTest {
@Test
void getProjectBinding_whenProjectBindingAndPermissions_returnsIt() throws Exception {
ProjectAlmSettingDto projectAlmSettingDto = mockProjectAlmSettingDto("1");
+
ProjectDto projectDto = mock();
+ when(projectDto.getKey()).thenReturn("projectKey_1");
+
userSession.logIn().addProjectPermission(ADMIN, projectDto);
when(projectBindingsService.findProjectBindingByUuid(UUID)).thenReturn(Optional.of(projectAlmSettingDto));
when(projectBindingsService.findProjectFromBinding(projectAlmSettingDto)).thenReturn(Optional.ofNullable(projectDto));
@@ -124,8 +128,9 @@ class DefaultProjectBindingsControllerTest {
content().json("""
{
"id": "uuid_1",
- "dopSettings": "almSettingUuid_1",
+ "devOpsPlatformSettingId": "almSettingUuid_1",
"projectId": "projectUuid_1",
+ "projectKey": "projectKey_1",
"repository": "almRepo_1",
"slug": "almSlug_1"
}
@@ -175,10 +180,10 @@ class DefaultProjectBindingsControllerTest {
void searchProjectBindings_whenResultsFound_shouldReturnsThem() throws Exception {
userSession.logIn().addPermission(PROVISION_PROJECTS);
- ProjectAlmSettingDto dto1 = mockProjectAlmSettingDto("1");
- ProjectAlmSettingDto dto2 = mockProjectAlmSettingDto("2");
+ ProjectBindingInformation dto1 = projectBindingInformation("1");
+ ProjectBindingInformation dto2 = projectBindingInformation("2");
- List<ProjectAlmSettingDto> expectedResults = List.of(dto1, dto2);
+ List<ProjectBindingInformation> expectedResults = List.of(dto1, dto2);
when(projectBindingsService.findProjectBindingsByRequest(any())).thenReturn(new SearchResults<>(expectedResults, expectedResults.size()));
mockMvc
@@ -190,33 +195,35 @@ class DefaultProjectBindingsControllerTest {
.andExpectAll(
status().isOk(),
content().json("""
- {
- "projectBindings": [
- {
- "id": "uuid_1",
- "dopSettings": "almSettingUuid_1",
- "projectId": "projectUuid_1",
- "repository": "almRepo_1",
- "slug": "almSlug_1"
- },
- {
- "id": "uuid_2",
- "dopSettings": "almSettingUuid_2",
- "projectId": "projectUuid_2",
- "repository": "almRepo_2",
- "slug": "almSlug_2"
+ {
+ "projectBindings": [
+ {
+ "id": "uuid_1",
+ "devOpsPlatformSettingId": "almSettingUuid_1",
+ "projectId": "projectUuid_1",
+ "projectKey": "projectKey_1",
+ "repository": "almRepo_1",
+ "slug": "almSlug_1"
+ },
+ {
+ "id": "uuid_2",
+ "devOpsPlatformSettingId": "almSettingUuid_2",
+ "projectId": "projectUuid_2",
+ "projectKey": "projectKey_2",
+ "repository": "almRepo_2",
+ "slug": "almSlug_2"
+ }
+ ],
+ "page": {
+ "pageIndex": 1,
+ "pageSize": 100,
+ "total": 2
}
- ],
- "page": {
- "pageIndex": 1,
- "pageSize": 100,
- "total": 2
}
- }
- """));
+ """));
}
- private ProjectAlmSettingDto mockProjectAlmSettingDto(String i) {
+ private static ProjectAlmSettingDto mockProjectAlmSettingDto(String i) {
ProjectAlmSettingDto dto = mock();
when(dto.getUuid()).thenReturn("uuid_" + i);
when(dto.getAlmSettingUuid()).thenReturn("almSettingUuid_" + i);
@@ -226,4 +233,13 @@ class DefaultProjectBindingsControllerTest {
return dto;
}
-} \ No newline at end of file
+ private static ProjectBindingInformation projectBindingInformation(String i) {
+ return new ProjectBindingInformation("uuid_" + i,
+ "almSettingUuid_" + i,
+ "projectUuid_" + i,
+ "projectKey_" + i,
+ "almRepo_" + i,
+ "almSlug_" + i);
+ }
+
+}