aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Camus <julien.camus@sonarsource.com>2024-10-23 15:57:19 +0200
committersonartech <sonartech@sonarsource.com>2024-10-23 20:02:47 +0000
commitc11922595bf67a70a38588a6f7763fb6f6c14501 (patch)
tree6646ef381ecc6835a98de18cce9ee258d86a4ee7
parent24bd8856af66bf2e1148c42279739225b254e84e (diff)
downloadsonarqube-c11922595bf67a70a38588a6f7763fb6f6c14501.tar.gz
sonarqube-c11922595bf67a70a38588a6f7763fb6f6c14501.zip
SONAR-23431 Adjust maximum page size parameter when retrieving GitLab repositories
-rw-r--r--server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposActionIT.java86
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposAction.java2
2 files changed, 87 insertions, 1 deletions
diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposActionIT.java
index 42be74f8e87..a01c726145f 100644
--- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposActionIT.java
+++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposActionIT.java
@@ -21,6 +21,8 @@ package org.sonar.server.almintegration.ws.gitlab;
import java.util.Arrays;
import java.util.LinkedList;
+import java.util.List;
+import java.util.stream.IntStream;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.alm.client.gitlab.GitlabApplicationClient;
@@ -43,10 +45,12 @@ import org.sonarqube.ws.AlmIntegrations.GitlabRepository;
import org.sonarqube.ws.AlmIntegrations.SearchGitlabReposWsResponse;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto;
@@ -107,6 +111,88 @@ public class SearchGitlabReposActionIT {
}
@Test
+ public void handle_whenPageSizeIsValid_shouldReturnRepositories() {
+ int pageSize = 50; // Page size within the maximum limit of 100
+
+ Project gitlabProject1 = new Project(1L, "repoName1", "repoNamePath1", "repo-slug-1", "repo-path-slug-1", "url-1");
+
+ when(gitlabApplicationClient.searchProjects(any(), any(), any(), anyInt(), eq(pageSize)))
+ .thenReturn(new ProjectList(List.of(gitlabProject1), 1, pageSize, 1));
+
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+ AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
+
+ db.almPats().insert(dto -> {
+ dto.setAlmSettingUuid(almSetting.getUuid());
+ dto.setUserUuid(user.getUuid());
+ dto.setPersonalAccessToken("some-pat");
+ });
+
+ SearchGitlabReposWsResponse response = ws.newRequest()
+ .setParam("almSetting", almSetting.getKey())
+ .setParam("ps", String.valueOf(pageSize))
+ .executeProtobuf(SearchGitlabReposWsResponse.class);
+
+ assertThat(response.getRepositoriesCount()).isEqualTo(1);
+ assertThat(response.getRepositoriesList())
+ .extracting(AlmIntegrations.GitlabRepository::getName)
+ .containsExactly("repoName1");
+ }
+
+ @Test
+ public void handle_whenPageSizeIs10AndThereAre25Repos_shouldReturnCorrectTotal() {
+ int pageNumber = 1; // First page
+ int pageSize = 10; // Page size within the limit
+ int totalRepos = 25; // Total number of repositories on GitLab
+
+ List<Project> firstPageProjects = IntStream.range(1, pageSize + 1)
+ .mapToObj(i -> new Project(i, "repoName" + i, "repoNamePath" + i, "repo-slug-" + i, "repo-path-slug-" + i, "url-" + i))
+ .toList();
+
+ when(gitlabApplicationClient.searchProjects(any(), any(), any(), eq(pageNumber), eq(pageSize)))
+ .thenReturn(new ProjectList(firstPageProjects, pageNumber, pageSize, totalRepos));
+
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+ AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
+
+ db.almPats().insert(dto -> {
+ dto.setAlmSettingUuid(almSetting.getUuid());
+ dto.setUserUuid(user.getUuid());
+ dto.setPersonalAccessToken("some-pat");
+ });
+
+ SearchGitlabReposWsResponse response = ws.newRequest()
+ .setParam("almSetting", almSetting.getKey())
+ .setParam("ps", String.valueOf(pageSize))
+ .setParam("p", String.valueOf(pageNumber))
+ .executeProtobuf(SearchGitlabReposWsResponse.class);
+
+ assertThat(response.getPaging().getPageIndex()).isEqualTo(pageNumber);
+ assertThat(response.getRepositoriesCount()).isEqualTo(pageSize);
+ assertThat(response.getPaging().getTotal()).isEqualTo(totalRepos);
+ assertThat(response.getRepositoriesList())
+ .extracting(AlmIntegrations.GitlabRepository::getName)
+ .containsExactly("repoName1", "repoName2", "repoName3", "repoName4", "repoName5", "repoName6", "repoName7", "repoName8", "repoName9", "repoName10");
+ }
+
+ @Test
+ public void handle_whenPageSizeExceedsMaxLimit_shouldThrowException() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+ AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
+
+ TestRequest request = ws.newRequest()
+ .setParam("almSetting", almSetting.getKey())
+ .setParam("ps", "101"); // Page size exceeding the maximum limit of 100
+
+ assertThatIllegalArgumentException()
+ .isThrownBy(request::execute)
+ .withMessage("'ps' value (101) must be less than 100");
+ }
+
+ @Test
public void list_gitlab_repos_some_projects_already_set_up() {
Project gitlabProject1 = new Project(1L, "repoName1", "repoNamePath1", "repo-slug-1", "repo-path-slug-1", "url-1");
Project gitlabProject2 = new Project(2L, "repoName2", "path1 / repoName2", "repo-slug-2", "path-1/repo-slug-2", "url-2");
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposAction.java
index 65553c26617..df65a1c407e 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposAction.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposAction.java
@@ -54,7 +54,7 @@ public class SearchGitlabReposAction implements AlmIntegrationsWsAction {
private static final String PARAM_ALM_SETTING = "almSetting";
private static final String PARAM_PROJECT_NAME = "projectName";
private static final int DEFAULT_PAGE_SIZE = 20;
- private static final int MAX_PAGE_SIZE = 500;
+ private static final int MAX_PAGE_SIZE = 100;
private final DbClient dbClient;
private final UserSession userSession;