Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refetch next pages for groups and projects #103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/main/java/cd/go/authorization/gitlab/client/GitLabClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import okhttp3.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -118,7 +119,7 @@ public List<GitLabGroup> groups(String personalAccessToken) throws IOException {
final String groupsUrl = apiUrlWithPersonalAccessToken(gitLabConfiguration.gitLabBaseURL(), "groups");
final Request request = getRequestWithAccessToken(groupsUrl,personalAccessToken);

return executeRequest(request, response -> GitLabGroup.fromJSONArray(response.body().string()));
return executeRequestRepeated(request, response -> GitLabGroup.fromJSONArray(response.body().string()));
}

public List<GitLabProject> projects(String personalAccessToken) throws IOException {
Expand All @@ -127,7 +128,7 @@ public List<GitLabProject> projects(String personalAccessToken) throws IOExcepti
final String projectsUrl = apiUrlWithPersonalAccessToken(gitLabConfiguration.gitLabBaseURL(), "projects");
final Request request = getRequestWithAccessToken(projectsUrl,personalAccessToken);

return executeRequest(request, response -> GitLabProject.fromJSONArray(response.body().string()));
return executeRequestRepeated(request, response -> GitLabProject.fromJSONArray(response.body().string()));
}

public MembershipInfo groupMembershipInfo(String personalAccessToken, long groupId, long memberId) throws IOException {
Expand Down Expand Up @@ -175,6 +176,42 @@ private <T> T executeRequest(Request request, Callback<T> callback) throws IOExc
return callback.onResponse(response);
}

/**
* Repeatedly execute the request until all pages are loaded
*/
private <E, T extends List<E>> List<E> executeRequestRepeated(Request request, Callback<T> callback) throws IOException {
HttpUrl originalUrl = request.url();
List<E> result = new ArrayList<>();
do {
final Response response = httpClient.newCall(request).execute();

if (!response.isSuccessful()) {
final String responseBody = response.body().string();
final String errorMessage = isNotBlank(responseBody) ? responseBody : response.message();
throw new RuntimeException(format(API_ERROR_MSG, request.url().encodedPath(), errorMessage));
}

result.addAll(callback.onResponse(response));

// Check if there are more pages to load
String nextPage = response.header("x-next-page");
if (nextPage != null && !nextPage.trim().isEmpty()) {
HttpUrl url = originalUrl
.newBuilder()
.addQueryParameter("page", nextPage.trim())
.build();
request = request
.newBuilder()
.url(url)
.build();
continue;
}
break;
} while (true);

return result;
}

private void validateTokenInfo(TokenInfo tokenInfo) {
if (tokenInfo == null) {
throw new RuntimeException("TokenInfo must not be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,33 @@ public void shouldFetchGroupsForAUser() throws Exception {
assertEquals(personalAccessToken, request.getHeaders().get("Private-Token"));
}

@Test
public void shouldFetchPagedGroupsForAUser() throws Exception {
final String personalAccessToken = "some-random-token";
server.enqueue(new MockResponse()
.setResponseCode(200)
.addHeader("x-next-page", "2")
.setBody(GSON.toJson(asList(new GitLabGroup(1L, "foo-group")))));
server.enqueue(new MockResponse()
.setResponseCode(200)
.addHeader("x-next-page", "")
.setBody(GSON.toJson(asList(new GitLabGroup(2L, "bar-group")))));

when(gitLabConfiguration.gitLabBaseURL()).thenReturn(server.url("/").toString());

final List<GitLabGroup> gitLabGroups = gitLabClient.groups(personalAccessToken);

assertThat(gitLabGroups, hasSize(2));
assertThat(gitLabGroups.get(0).getName(), is("foo-group"));
assertThat(gitLabGroups.get(1).getName(), is("bar-group"));

RecordedRequest request = server.takeRequest();
assertEquals("GET /api/v4/groups HTTP/1.1", request.getRequestLine());
assertNotNull(request.getHeaders().get("Private-Token"));
assertEquals(personalAccessToken, request.getHeaders().get("Private-Token"));
}


@Test
public void shouldFetchProjectsForAUser() throws Exception {
final String personalAccessToken = "some-random-token";
Expand All @@ -167,6 +194,34 @@ public void shouldFetchProjectsForAUser() throws Exception {
assertEquals(personalAccessToken, request.getHeaders().get("Private-Token"));
}

@Test
public void shouldFetchPagedProjectsForAUser() throws Exception {
final String personalAccessToken = "some-random-token";
server.enqueue(new MockResponse()
.setResponseCode(200)
.addHeader("x-next-page", "2")
.setBody(GSON.toJson(asList(new GitLabProject(1L, "foo-project")))));
server.enqueue(new MockResponse()
.setResponseCode(200)
.addHeader("x-next-page", "")
.setBody(GSON.toJson(asList(new GitLabProject(2L, "bar-project")))));


when(gitLabConfiguration.gitLabBaseURL()).thenReturn(server.url("/").toString());

final List<GitLabProject> gitLabProjects = gitLabClient.projects(personalAccessToken);

assertThat(gitLabProjects, hasSize(2));
assertThat(gitLabProjects.get(0).getName(), is("foo-project"));
assertThat(gitLabProjects.get(1).getName(), is("bar-project"));

RecordedRequest request = server.takeRequest();
assertEquals("GET /api/v4/projects HTTP/1.1", request.getRequestLine());
assertNotNull(request.getHeaders().get("Private-Token"));
assertEquals(personalAccessToken, request.getHeaders().get("Private-Token"));
}


@Test
public void shouldFetchGroupMembershipForAUser() throws Exception {
final String personalAccessToken = "some-random-token";
Expand Down