From 45e9747732c2b9fbc2c190643a71e5afcb5c17a0 Mon Sep 17 00:00:00 2001 From: Flemming Frandsen Date: Wed, 9 Mar 2022 08:50:52 +0100 Subject: [PATCH] Actually fetch all the memberships with UserAPI.getMemberships() and also provide a pager based method for lazy fetching --- src/main/java/org/gitlab4j/api/UserApi.java | 22 ++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/UserApi.java b/src/main/java/org/gitlab4j/api/UserApi.java index 8643dbda1..a8c9b59fe 100644 --- a/src/main/java/org/gitlab4j/api/UserApi.java +++ b/src/main/java/org/gitlab4j/api/UserApi.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; import java.util.stream.Stream; import javax.ws.rs.core.Form; @@ -1306,9 +1307,24 @@ public void deleteGpgKey(final Integer userId, final Integer keyId) throws GitLa * @throws GitLabApiException if any exception occurs * @since GitLab 12.8 */ - public List getMemberships(Integer userId) throws GitLabApiException { + public List getMemberships(int userId) throws GitLabApiException { + return getMembershipsPager(userId).stream().collect(Collectors.toList()); + } + + /** + * Returns a Pager that lists all projects and groups a user is a member of. (admin only) + * + * This allows lazy-fetching of huge numbers of memberships. + * + *
GitLab Endpoint: GET /users/:id/memberships
+ * + * @param userId the ID of the user to get the memberships for + * @return the list of memberships of the given user + * @throws GitLabApiException if any exception occurs + * @since GitLab 12.8 + */ + public Pager getMembershipsPager(int userId) throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm(); - Response response = get(Response.Status.OK, formData.asMap(), "users", userId, "memberships"); - return (response.readEntity(new GenericType>() {})); + return (new Pager<>(this, Membership.class, 100, formData.asMap(), "users", userId, "memberships")); } }