From 79a918e87636c7d56e71e2fc42dca6d2caca0c51 Mon Sep 17 00:00:00 2001 From: Aurelien Poscia Date: Mon, 14 Aug 2023 13:27:27 +0200 Subject: [PATCH] SONAR-19783 Add method to fetch organization details from GitHub --- .../org/sonar/alm/client/github/GithubBinding.java | 2 ++ .../github/GithubPaginatedHttpClientImpl.java | 3 +++ .../github/GithubPaginatedHttpClientImplTest.java | 13 ++++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubBinding.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubBinding.java index 8296839a3e1..208b3040892 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubBinding.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubBinding.java @@ -32,6 +32,8 @@ public class GithubBinding { // nothing to do } + public record GsonOrganization(@SerializedName("login") String login, @SerializedName("default_repository_permission") String defaultRepositoryPermissions){} + public static class GsonApp { @SerializedName("installations_count") diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubPaginatedHttpClientImpl.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubPaginatedHttpClientImpl.java index d2391d0420d..36d5768ca59 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubPaginatedHttpClientImpl.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubPaginatedHttpClientImpl.java @@ -50,6 +50,9 @@ public class GithubPaginatedHttpClientImpl implements GithubPaginatedHttpClient public List get(String appUrl, AccessToken token, String query, Function> responseDeserializer) throws IOException { List results = new ArrayList<>(); String nextEndpoint = query + "?per_page=100"; + if (query.contains("?")) { + nextEndpoint = query + "&per_page=100"; + } GithubApplicationHttpClient.RateLimit rateLimit = null; while (nextEndpoint != null) { checkRateLimit(rateLimit); diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/GithubPaginatedHttpClientImplTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/GithubPaginatedHttpClientImplTest.java index 8061e0c6dfc..bc11a17e531 100644 --- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/GithubPaginatedHttpClientImplTest.java +++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/GithubPaginatedHttpClientImplTest.java @@ -75,7 +75,6 @@ public class GithubPaginatedHttpClientImplTest { @InjectMocks private GithubPaginatedHttpClientImpl underTest; - @Test public void get_whenNoPagination_ReturnsCorrectResponse() throws IOException { @@ -88,6 +87,18 @@ public class GithubPaginatedHttpClientImplTest { .containsExactly("result1", "result2"); } + @Test + public void get_whenEndpointAlreadyContainsPathParameter_shouldAddANewParameter() throws IOException { + ArgumentCaptor urlCaptor = ArgumentCaptor.forClass(String.class); + + GetResponse response = mockResponseWithoutPagination("[\"result1\", \"result2\"]"); + when(appHttpClient.get(eq(APP_URL), eq(accessToken), urlCaptor.capture())).thenReturn(response); + + underTest.get(APP_URL, accessToken, ENDPOINT + "?alreadyExistingArg=2", result -> gson.fromJson(result, STRING_LIST_TYPE)); + + assertThat(urlCaptor.getValue()).isEqualTo(ENDPOINT + "?alreadyExistingArg=2&per_page=100"); + } + private static GetResponse mockResponseWithoutPagination(String content) { GetResponse response = mock(GetResponse.class); when(response.getCode()).thenReturn(200); -- 2.39.5