]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-23431 Adjust maximum page size parameter when retrieving GitLab repositories
authorJulien Camus <julien.camus@sonarsource.com>
Wed, 23 Oct 2024 13:57:19 +0000 (15:57 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 23 Oct 2024 20:02:47 +0000 (20:02 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposAction.java

index 42be74f8e87b05aef971d202f2955dc7b8e67759..a01c726145f5d5ec64ac77ade0cc52291f1ac662 100644 (file)
@@ -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;
@@ -106,6 +110,88 @@ public class SearchGitlabReposActionIT {
         tuple(4L, "repoName4", "repoName4 / repoName4", "repo-slug-4", "repo-slug-4/repo-slug-4", "url-4", false, false));
   }
 
+  @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");
index 65553c26617f035aa9b95a2ec965ddd3b7708d42..df65a1c407eed07ff211d5352cf5b299f8cb2d1b 100644 (file)
@@ -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;