Skip to content

Commit b15fdab

Browse files
authored
Merge pull request #815 from dren-dk/fix-getMemberships-by-adding-pager
Actually fetch all the memberships with UserAPI.getMemberships() and …
2 parents 85e0085 + 45e9747 commit b15fdab

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/main/java/org/gitlab4j/api/UserApi.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66
import java.util.Objects;
77
import java.util.Optional;
8+
import java.util.stream.Collectors;
89
import java.util.stream.Stream;
910

1011
import javax.ws.rs.core.Form;
@@ -1306,9 +1307,24 @@ public void deleteGpgKey(final Integer userId, final Integer keyId) throws GitLa
13061307
* @throws GitLabApiException if any exception occurs
13071308
* @since GitLab 12.8
13081309
*/
1309-
public List<Membership> getMemberships(Integer userId) throws GitLabApiException {
1310+
public List<Membership> getMemberships(int userId) throws GitLabApiException {
1311+
return getMembershipsPager(userId).stream().collect(Collectors.toList());
1312+
}
1313+
1314+
/**
1315+
* Returns a Pager that lists all projects and groups a user is a member of. (admin only)
1316+
*
1317+
* This allows lazy-fetching of huge numbers of memberships.
1318+
*
1319+
* <pre><code>GitLab Endpoint: GET /users/:id/memberships</code></pre>
1320+
*
1321+
* @param userId the ID of the user to get the memberships for
1322+
* @return the list of memberships of the given user
1323+
* @throws GitLabApiException if any exception occurs
1324+
* @since GitLab 12.8
1325+
*/
1326+
public Pager<Membership> getMembershipsPager(int userId) throws GitLabApiException {
13101327
GitLabApiForm formData = new GitLabApiForm();
1311-
Response response = get(Response.Status.OK, formData.asMap(), "users", userId, "memberships");
1312-
return (response.readEntity(new GenericType<List<Membership>>() {}));
1328+
return (new Pager<>(this, Membership.class, 100, formData.asMap(), "users", userId, "memberships"));
13131329
}
13141330
}

0 commit comments

Comments
 (0)