aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorPierre <pierre.guillot@sonarsource.com>2021-10-08 11:15:06 +0200
committersonartech <sonartech@sonarsource.com>2021-10-08 20:03:10 +0000
commit59158adb1e30227f6a42a9da28ccbbf8b6b547bf (patch)
treec382b5df6073b78cc2c6c152b871c77549f7de97 /server
parentac63a87fff684c5c4eb3b0a2562526e2643378be (diff)
downloadsonarqube-59158adb1e30227f6a42a9da28ccbbf8b6b547bf.tar.gz
sonarqube-59158adb1e30227f6a42a9da28ccbbf8b6b547bf.zip
SONAR-15488 x-total header is optional
Diffstat (limited to 'server')
-rw-r--r--server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabHttpClient.java4
-rw-r--r--server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/ProjectList.java9
-rw-r--r--server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabHttpClientTest.java36
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/SearchGitlabReposAction.java13
4 files changed, 53 insertions, 9 deletions
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabHttpClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabHttpClient.java
index 1dc3b4f2e89..62e2a04fa45 100644
--- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabHttpClient.java
+++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabHttpClient.java
@@ -34,6 +34,7 @@ import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
+import org.apache.logging.log4j.util.Strings;
import org.sonar.alm.client.TimeoutConfiguration;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.log.Logger;
@@ -312,7 +313,8 @@ public class GitlabHttpClient {
List<Project> projectList = Project.parseJsonArray(response.body().string());
int returnedPageNumber = parseAndGetIntegerHeader(headers.get("X-Page"));
int returnedPageSize = parseAndGetIntegerHeader(headers.get("X-Per-Page"));
- int totalProjects = parseAndGetIntegerHeader(headers.get("X-Total"));
+ String xtotal = headers.get("X-Total");
+ Integer totalProjects = Strings.isEmpty(xtotal) ? null : parseAndGetIntegerHeader(xtotal);
return new ProjectList(projectList, returnedPageNumber, returnedPageSize, totalProjects);
} catch (JsonSyntaxException e) {
throw new IllegalArgumentException("Could not parse GitLab answer to search projects. Got a non-json payload as result.");
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/ProjectList.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/ProjectList.java
index 6548e669922..9450b67d220 100644
--- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/ProjectList.java
+++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/ProjectList.java
@@ -20,15 +20,17 @@
package org.sonar.alm.client.gitlab;
import java.util.List;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
public class ProjectList {
private final List<Project> projects;
private final int pageNumber;
private final int pageSize;
- private final int total;
+ private final Integer total;
- public ProjectList(List<Project> projects, int pageNumber, int pageSize, int total) {
+ public ProjectList(List<Project> projects, int pageNumber, int pageSize, @Nullable Integer total) {
this.projects = projects;
this.pageNumber = pageNumber;
this.pageSize = pageSize;
@@ -47,7 +49,8 @@ public class ProjectList {
return pageSize;
}
- public int getTotal() {
+ @CheckForNull
+ public Integer getTotal() {
return total;
}
}
diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabHttpClientTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabHttpClientTest.java
index 229f26f6ebe..0c07902d70a 100644
--- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabHttpClientTest.java
+++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabHttpClientTest.java
@@ -244,6 +244,42 @@ public class GitlabHttpClientTest {
}
@Test
+ public void search_projects_dont_fail_if_no_x_total() throws InterruptedException {
+ MockResponse projects = new MockResponse()
+ .setResponseCode(200)
+ .setBody("[\n"
+ + " {\n"
+ + " \"id\": 1,\n"
+ + " \"name\": \"SonarQube example 1\",\n"
+ + " \"name_with_namespace\": \"SonarSource / SonarQube / SonarQube example 1\",\n"
+ + " \"path\": \"sonarqube-example-1\",\n"
+ + " \"path_with_namespace\": \"sonarsource/sonarqube/sonarqube-example-1\",\n"
+ + " \"web_url\": \"https://example.gitlab.com/sonarsource/sonarqube/sonarqube-example-1\"\n"
+ + " }"
+ + "]");
+ projects.addHeader("X-Page", 1);
+ projects.addHeader("X-Per-Page", 10);
+ server.enqueue(projects);
+
+ ProjectList projectList = underTest.searchProjects(gitlabUrl, "pat", "example", 1, 10);
+
+ assertThat(projectList.getPageNumber()).isEqualTo(1);
+ assertThat(projectList.getPageSize()).isEqualTo(10);
+ assertThat(projectList.getTotal()).isNull();
+
+ assertThat(projectList.getProjects()).hasSize(1);
+ assertThat(projectList.getProjects()).extracting(
+ Project::getId, Project::getName, Project::getNameWithNamespace, Project::getPath, Project::getPathWithNamespace, Project::getWebUrl).containsExactly(
+ tuple(1L, "SonarQube example 1", "SonarSource / SonarQube / SonarQube example 1", "sonarqube-example-1", "sonarsource/sonarqube/sonarqube-example-1",
+ "https://example.gitlab.com/sonarsource/sonarqube/sonarqube-example-1"));
+
+ RecordedRequest projectGitlabRequest = server.takeRequest(10, TimeUnit.SECONDS);
+ String gitlabUrlCall = projectGitlabRequest.getRequestUrl().toString();
+ assertThat(gitlabUrlCall).isEqualTo(server.url("") + "projects?archived=false&simple=true&membership=true&order_by=name&sort=asc&search=example&page=1&per_page=10");
+ assertThat(projectGitlabRequest.getMethod()).isEqualTo("GET");
+ }
+
+ @Test
public void search_projects_with_case_insensitive_pagination_headers() throws InterruptedException {
MockResponse projects1 = new MockResponse()
.setResponseCode(200)
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 b3214bbd458..56c86113a76 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
@@ -123,13 +123,16 @@ public class SearchGitlabReposAction implements AlmIntegrationsWsAction {
.map(project -> toGitlabRepository(project, sqProjectsKeyByGitlabProjectId))
.collect(toList());
+ Paging.Builder pagingBuilder = Paging.newBuilder()
+ .setPageIndex(gitlabProjectList.getPageNumber())
+ .setPageSize(gitlabProjectList.getPageSize());
+ Integer gitlabProjectListTotal = gitlabProjectList.getTotal();
+ if (gitlabProjectListTotal != null) {
+ pagingBuilder.setTotal(gitlabProjectListTotal);
+ }
return AlmIntegrations.SearchGitlabReposWsResponse.newBuilder()
.addAllRepositories(gitlabRepositories)
- .setPaging(Paging.newBuilder()
- .setPageIndex(gitlabProjectList.getPageNumber())
- .setPageSize(gitlabProjectList.getPageSize())
- .setTotal(gitlabProjectList.getTotal())
- .build())
+ .setPaging(pagingBuilder.build())
.build();
}
}