Skip to content

Commit d72fb68

Browse files
authored
Feature-351: Add support for get all members to ProjectApi and GroupApi
* Added getAllMembers() methods (#348). * Added tests for getAllMembers() methods (#348).
1 parent a6b7c78 commit d72fb68

File tree

4 files changed

+247
-36
lines changed

4 files changed

+247
-36
lines changed

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
/**
2323
* This class implements the client side API for the GitLab groups calls.
24+
* @see <a href="https://docs.gitlab.com/ce/api/groups.html">Groups API at GitLab</a>
25+
* @see <a href="https://docs.gitlab.com/ee/api/members.html">Group and project members API at GitLab</a>
2426
*/
2527
public class GroupApi extends AbstractApi {
2628

@@ -729,7 +731,8 @@ public Stream<Member> getMembersStream(Object groupIdOrPath) throws GitLabApiExc
729731
* @throws GitLabApiException if any exception occurs
730732
*/
731733
public Member getMember(Object groupIdOrPath, int userId) throws GitLabApiException {
732-
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "groups", getGroupIdOrPath(groupIdOrPath), "members", userId);
734+
Response response = get(Response.Status.OK, getDefaultPerPageParam(),
735+
"groups", getGroupIdOrPath(groupIdOrPath), "members", userId);
733736
return (response.readEntity(new GenericType<Member>() {}));
734737
}
735738

@@ -750,6 +753,75 @@ public Optional<Member> getOptionalMember(Object groupIdOrPath, int userId) {
750753
}
751754
}
752755

756+
/**
757+
* Gets a list of group members viewable by the authenticated user, including inherited members
758+
* through ancestor groups. Returns multiple times the same user (with different member attributes)
759+
* when the user is a member of the group and of one or more ancestor group.
760+
*
761+
* <pre><code>GitLab Endpoint: GET /groups/:id/members/all</code></pre>
762+
*
763+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
764+
* @return a list of group members viewable by the authenticated user, including inherited members
765+
* through ancestor groups
766+
* @throws GitLabApiException if any exception occurs
767+
*/
768+
public List<Member> getAllMembers(Object groupIdOrPath) throws GitLabApiException {
769+
return (getAllMembers(groupIdOrPath, getDefaultPerPage()).all());
770+
}
771+
772+
/**
773+
* Gets a list of group members viewable by the authenticated user, including inherited members
774+
* through ancestor groups. Returns multiple times the same user (with different member attributes)
775+
* when the user is a member of the group and of one or more ancestor group.
776+
*
777+
* <pre><code>GitLab Endpoint: GET /groups/:id/members/all</code></pre>
778+
*
779+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
780+
* @param page the page to get
781+
* @param perPage the number of Member instances per page
782+
* @return a list of group members viewable by the authenticated user, including inherited members
783+
* through ancestor groups in the specified page range
784+
* @throws GitLabApiException if any exception occurs
785+
*/
786+
public List<Member> getAllMembers(Object groupIdOrPath, int page, int perPage) throws GitLabApiException {
787+
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage),
788+
"groups", getGroupIdOrPath(groupIdOrPath), "members", "all");
789+
return (response.readEntity(new GenericType<List<Member>>() {}));
790+
}
791+
792+
/**
793+
* Gets a Pager of group members viewable by the authenticated user, including inherited members
794+
* through ancestor groups. Returns multiple times the same user (with different member attributes)
795+
* when the user is a member of the group and of one or more ancestor group.
796+
*
797+
* <pre><code>GitLab Endpoint: GET /groups/:id/members/all</code></pre>
798+
*
799+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
800+
* @param itemsPerPage the number of Member instances that will be fetched per page
801+
* @return a Pager of group members viewable by the authenticated user, including inherited members
802+
* through ancestor groups
803+
* @throws GitLabApiException if any exception occurs
804+
*/
805+
public Pager<Member> getAllMembers(Object groupIdOrPath, int itemsPerPage) throws GitLabApiException {
806+
return (new Pager<Member>(this, Member.class, itemsPerPage, null, "groups", getGroupIdOrPath(groupIdOrPath), "members", "all"));
807+
}
808+
809+
/**
810+
* Gets a Stream of group members viewable by the authenticated user, including inherited members
811+
* through ancestor groups. Returns multiple times the same user (with different member attributes)
812+
* when the user is a member of the group and of one or more ancestor group.
813+
*
814+
* <pre><code>GitLab Endpoint: GET /groups/:id/members/all</code></pre>
815+
*
816+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
817+
* @return a Stream of group members viewable by the authenticated user, including inherited members
818+
* through ancestor groups
819+
* @throws GitLabApiException if any exception occurs
820+
*/
821+
public Stream<Member> getAllMembersStream(Object groupIdOrPath) throws GitLabApiException {
822+
return (getAllMembers(groupIdOrPath, getDefaultPerPage()).stream());
823+
}
824+
753825
/**
754826
* Adds a user to the list of group members.
755827
*

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454

5555
/**
5656
* This class provides an entry point to all the GitLab API project calls.
57+
* @see <a href="https://docs.gitlab.com/ce/api/projects.html">Projects API at GitLab</a>
58+
* @see <a href="https://docs.gitlab.com/ee/api/members.html">Group and project members API at GitLab</a>
5759
*/
5860
public class ProjectApi extends AbstractApi implements Constants {
5961

@@ -1213,6 +1215,78 @@ public Stream<Member> getMembersStream(Object projectIdOrPath) throws GitLabApiE
12131215
return (getMembers(projectIdOrPath, getDefaultPerPage()).stream());
12141216
}
12151217

1218+
/**
1219+
* Gets a list of project members viewable by the authenticated user,
1220+
* including inherited members through ancestor groups. Returns multiple
1221+
* times the same user (with different member attributes) when the user is
1222+
* a member of the project/group and of one or more ancestor group.
1223+
*
1224+
* <pre><code>GET /projects/:id/members/all</code></pre>
1225+
*
1226+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
1227+
* @return the project members viewable by the authenticated user, including inherited members through ancestor groups
1228+
* @throws GitLabApiException if any exception occurs
1229+
*/
1230+
public List<Member> getAllMembers(Object projectIdOrPath) throws GitLabApiException {
1231+
return (getAllMembers(projectIdOrPath, getDefaultPerPage()).all());
1232+
}
1233+
1234+
/**
1235+
* Gets a list of project members viewable by the authenticated user,
1236+
* including inherited members through ancestor groups. Returns multiple
1237+
* times the same user (with different member attributes) when the user is
1238+
* a member of the project/group and of one or more ancestor group.
1239+
*
1240+
* <pre><code>GET /projects/:id/members</code></pre>
1241+
*
1242+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
1243+
* @param page the page to get
1244+
* @param perPage the number of Member instances per page
1245+
* @return the project members viewable by the authenticated user, including inherited members through ancestor groups
1246+
* @throws GitLabApiException if any exception occurs
1247+
*/
1248+
public List<Member> getAllMembers(Object projectIdOrPath, int page, int perPage) throws GitLabApiException {
1249+
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage),
1250+
"projects", getProjectIdOrPath(projectIdOrPath), "members", "all");
1251+
return (response.readEntity(new GenericType<List<Member>>() {}));
1252+
}
1253+
1254+
/**
1255+
* Gets a Pager of project members viewable by the authenticated user,
1256+
* including inherited members through ancestor groups. Returns multiple
1257+
* times the same user (with different member attributes) when the user is
1258+
* a member of the project/group and of one or more ancestor group.
1259+
*
1260+
* <pre><code>GET /projects/:id/members/all</code></pre>
1261+
*
1262+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
1263+
* @param itemsPerPage the number of Project instances that will be fetched per page
1264+
* @return a Pager of the project members viewable by the authenticated user,
1265+
* including inherited members through ancestor groups
1266+
* @throws GitLabApiException if any exception occurs
1267+
*/
1268+
public Pager<Member> getAllMembers(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
1269+
return (new Pager<Member>(this, Member.class, itemsPerPage, null,
1270+
"projects", getProjectIdOrPath(projectIdOrPath), "members", "all"));
1271+
}
1272+
1273+
/**
1274+
* Gets a Stream of project members viewable by the authenticated user,
1275+
* including inherited members through ancestor groups. Returns multiple
1276+
* times the same user (with different member attributes) when the user is
1277+
* a member of the project/group and of one or more ancestor group.
1278+
*
1279+
* <pre><code>GET /projects/:id/members/all</code></pre>
1280+
*
1281+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
1282+
* @return a Stream of the project members viewable by the authenticated user,
1283+
* including inherited members through ancestor groups
1284+
* @throws GitLabApiException if any exception occurs
1285+
*/
1286+
public Stream<Member> getAllMembersStream(Object projectIdOrPath) throws GitLabApiException {
1287+
return (getAllMembers(projectIdOrPath, getDefaultPerPage()).stream());
1288+
}
1289+
12161290
/**
12171291
* Gets a project team member.
12181292
*

src/test/java/org/gitlab4j/api/TestGroupApi.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,57 @@ public void beforeMethod() {
112112
@Test
113113
public void testMemberOperations() throws GitLabApiException {
114114

115+
// Arrange and Act
115116
Member member = gitLabApi.getGroupApi().addMember(testGroup.getId(), testUser.getId(), AccessLevel.DEVELOPER);
117+
118+
// Assert
119+
assertNotNull(member);
120+
assertEquals(testUser.getId(), member.getId());
121+
assertEquals(AccessLevel.DEVELOPER, member.getAccessLevel());
122+
123+
// Act
124+
Optional<Member> optionalMember = gitLabApi.getGroupApi().getOptionalMember(testGroup, testUser.getId());
125+
126+
// Assert
127+
assertTrue(optionalMember.isPresent());
128+
129+
// Act
130+
List<Member> members = gitLabApi.getGroupApi().getMembers(testGroup);
131+
132+
// Assert
133+
assertNotNull(members);
134+
Boolean found = (members.stream().filter(m -> m.getId().equals(member.getId())).findAny().orElse(null) != null);
135+
assertTrue(found);
136+
137+
// Act
138+
gitLabApi.getGroupApi().removeMember(testGroup.getId(), testUser.getId());
139+
140+
// Act
141+
optionalMember = gitLabApi.getGroupApi().getOptionalMember(testGroup, testUser.getId());
142+
143+
// Assert
144+
assertFalse(optionalMember.isPresent());
145+
}
146+
147+
@Test
148+
public void testAllMemberOperations() throws GitLabApiException {
149+
150+
// Arrange and Act
151+
Member member = gitLabApi.getGroupApi().addMember(testGroup.getId(), testUser.getId(), AccessLevel.DEVELOPER);
152+
153+
// Assert
116154
assertNotNull(member);
117155
assertEquals(testUser.getId(), member.getId());
118156
assertEquals(AccessLevel.DEVELOPER, member.getAccessLevel());
119157

158+
// Act
159+
List<Member> members = gitLabApi.getGroupApi().getAllMembers(testGroup);
160+
161+
// Assert
162+
assertNotNull(members);
163+
Boolean found = (members.stream().filter(m -> m.getId().equals(member.getId())).findAny().orElse(null) != null);
164+
assertTrue(found);
165+
120166
gitLabApi.getGroupApi().removeMember(testGroup.getId(), testUser.getId());
121167
}
122168

0 commit comments

Comments
 (0)