diff --git a/src/main/java/org/gitlab4j/api/GroupApi.java b/src/main/java/org/gitlab4j/api/GroupApi.java index ce5f7e15d1950168c2f0f0f64bcef7109d6a4f26..4ad864610ee19e2082f58e5bed61732808f597a9 100644 --- a/src/main/java/org/gitlab4j/api/GroupApi.java +++ b/src/main/java/org/gitlab4j/api/GroupApi.java @@ -21,6 +21,8 @@ import org.gitlab4j.api.models.Visibility; /** * This class implements the client side API for the GitLab groups calls. + * @see Groups API at GitLab + * @see Group and project members API at GitLab */ public class GroupApi extends AbstractApi { @@ -729,7 +731,8 @@ public class GroupApi extends AbstractApi { * @throws GitLabApiException if any exception occurs */ public Member getMember(Object groupIdOrPath, int userId) throws GitLabApiException { - Response response = get(Response.Status.OK, getDefaultPerPageParam(), "groups", getGroupIdOrPath(groupIdOrPath), "members", userId); + Response response = get(Response.Status.OK, getDefaultPerPageParam(), + "groups", getGroupIdOrPath(groupIdOrPath), "members", userId); return (response.readEntity(new GenericType() {})); } @@ -750,6 +753,75 @@ public class GroupApi extends AbstractApi { } } + /** + * Gets a list of group members viewable by the authenticated user, including inherited members + * through ancestor groups. Returns multiple times the same user (with different member attributes) + * when the user is a member of the group and of one or more ancestor group. + * + *
GitLab Endpoint: GET /groups/:id/members/all
+ * + * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path + * @return a list of group members viewable by the authenticated user, including inherited members + * through ancestor groups + * @throws GitLabApiException if any exception occurs + */ + public List getAllMembers(Object groupIdOrPath) throws GitLabApiException { + return (getAllMembers(groupIdOrPath, getDefaultPerPage()).all()); + } + + /** + * Gets a list of group members viewable by the authenticated user, including inherited members + * through ancestor groups. Returns multiple times the same user (with different member attributes) + * when the user is a member of the group and of one or more ancestor group. + * + *
GitLab Endpoint: GET /groups/:id/members/all
+ * + * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path + * @param page the page to get + * @param perPage the number of Member instances per page + * @return a list of group members viewable by the authenticated user, including inherited members + * through ancestor groups in the specified page range + * @throws GitLabApiException if any exception occurs + */ + public List getAllMembers(Object groupIdOrPath, int page, int perPage) throws GitLabApiException { + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), + "groups", getGroupIdOrPath(groupIdOrPath), "members", "all"); + return (response.readEntity(new GenericType>() {})); + } + + /** + * Gets a Pager of group members viewable by the authenticated user, including inherited members + * through ancestor groups. Returns multiple times the same user (with different member attributes) + * when the user is a member of the group and of one or more ancestor group. + * + *
GitLab Endpoint: GET /groups/:id/members/all
+ * + * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path + * @param itemsPerPage the number of Member instances that will be fetched per page + * @return a Pager of group members viewable by the authenticated user, including inherited members + * through ancestor groups + * @throws GitLabApiException if any exception occurs + */ + public Pager getAllMembers(Object groupIdOrPath, int itemsPerPage) throws GitLabApiException { + return (new Pager(this, Member.class, itemsPerPage, null, "groups", getGroupIdOrPath(groupIdOrPath), "members", "all")); + } + + /** + * Gets a Stream of group members viewable by the authenticated user, including inherited members + * through ancestor groups. Returns multiple times the same user (with different member attributes) + * when the user is a member of the group and of one or more ancestor group. + * + *
GitLab Endpoint: GET /groups/:id/members/all
+ * + * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path + * @return a Stream of group members viewable by the authenticated user, including inherited members + * through ancestor groups + * @throws GitLabApiException if any exception occurs + */ + public Stream getAllMembersStream(Object groupIdOrPath) throws GitLabApiException { + return (getAllMembers(groupIdOrPath, getDefaultPerPage()).stream()); + } + /** * Adds a user to the list of group members. * diff --git a/src/main/java/org/gitlab4j/api/ProjectApi.java b/src/main/java/org/gitlab4j/api/ProjectApi.java index 474dda256a5bb26f4dd8eb2332d1e60783ab5ec0..10e7c6202e2fdc87ece32bffe84b758e24351bae 100644 --- a/src/main/java/org/gitlab4j/api/ProjectApi.java +++ b/src/main/java/org/gitlab4j/api/ProjectApi.java @@ -54,6 +54,8 @@ import org.gitlab4j.api.models.Visibility; /** * This class provides an entry point to all the GitLab API project calls. + * @see Projects API at GitLab + * @see Group and project members API at GitLab */ public class ProjectApi extends AbstractApi implements Constants { @@ -1213,6 +1215,78 @@ public class ProjectApi extends AbstractApi implements Constants { return (getMembers(projectIdOrPath, getDefaultPerPage()).stream()); } + /** + * Gets a list of project members viewable by the authenticated user, + * including inherited members through ancestor groups. Returns multiple + * times the same user (with different member attributes) when the user is + * a member of the project/group and of one or more ancestor group. + * + *
GET /projects/:id/members/all
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance + * @return the project members viewable by the authenticated user, including inherited members through ancestor groups + * @throws GitLabApiException if any exception occurs + */ + public List getAllMembers(Object projectIdOrPath) throws GitLabApiException { + return (getAllMembers(projectIdOrPath, getDefaultPerPage()).all()); + } + + /** + * Gets a list of project members viewable by the authenticated user, + * including inherited members through ancestor groups. Returns multiple + * times the same user (with different member attributes) when the user is + * a member of the project/group and of one or more ancestor group. + * + *
GET /projects/:id/members
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance + * @param page the page to get + * @param perPage the number of Member instances per page + * @return the project members viewable by the authenticated user, including inherited members through ancestor groups + * @throws GitLabApiException if any exception occurs + */ + public List getAllMembers(Object projectIdOrPath, int page, int perPage) throws GitLabApiException { + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), + "projects", getProjectIdOrPath(projectIdOrPath), "members", "all"); + return (response.readEntity(new GenericType>() {})); + } + + /** + * Gets a Pager of project members viewable by the authenticated user, + * including inherited members through ancestor groups. Returns multiple + * times the same user (with different member attributes) when the user is + * a member of the project/group and of one or more ancestor group. + * + *
GET /projects/:id/members/all
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance + * @param itemsPerPage the number of Project instances that will be fetched per page + * @return a Pager of the project members viewable by the authenticated user, + * including inherited members through ancestor groups + * @throws GitLabApiException if any exception occurs + */ + public Pager getAllMembers(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { + return (new Pager(this, Member.class, itemsPerPage, null, + "projects", getProjectIdOrPath(projectIdOrPath), "members", "all")); + } + + /** + * Gets a Stream of project members viewable by the authenticated user, + * including inherited members through ancestor groups. Returns multiple + * times the same user (with different member attributes) when the user is + * a member of the project/group and of one or more ancestor group. + * + *
GET /projects/:id/members/all
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance + * @return a Stream of the project members viewable by the authenticated user, + * including inherited members through ancestor groups + * @throws GitLabApiException if any exception occurs + */ + public Stream getAllMembersStream(Object projectIdOrPath) throws GitLabApiException { + return (getAllMembers(projectIdOrPath, getDefaultPerPage()).stream()); + } + /** * Gets a project team member. * diff --git a/src/test/java/org/gitlab4j/api/TestGroupApi.java b/src/test/java/org/gitlab4j/api/TestGroupApi.java index 186235eded082740ab04c1cad235ace39bfe53ba..92b10c017debd1c1f542bf516472d6463b00c2c9 100644 --- a/src/test/java/org/gitlab4j/api/TestGroupApi.java +++ b/src/test/java/org/gitlab4j/api/TestGroupApi.java @@ -112,11 +112,57 @@ public class TestGroupApi extends AbstractIntegrationTest { @Test public void testMemberOperations() throws GitLabApiException { + // Arrange and Act Member member = gitLabApi.getGroupApi().addMember(testGroup.getId(), testUser.getId(), AccessLevel.DEVELOPER); + + // Assert + assertNotNull(member); + assertEquals(testUser.getId(), member.getId()); + assertEquals(AccessLevel.DEVELOPER, member.getAccessLevel()); + + // Act + Optional optionalMember = gitLabApi.getGroupApi().getOptionalMember(testGroup, testUser.getId()); + + // Assert + assertTrue(optionalMember.isPresent()); + + // Act + List members = gitLabApi.getGroupApi().getMembers(testGroup); + + // Assert + assertNotNull(members); + Boolean found = (members.stream().filter(m -> m.getId().equals(member.getId())).findAny().orElse(null) != null); + assertTrue(found); + + // Act + gitLabApi.getGroupApi().removeMember(testGroup.getId(), testUser.getId()); + + // Act + optionalMember = gitLabApi.getGroupApi().getOptionalMember(testGroup, testUser.getId()); + + // Assert + assertFalse(optionalMember.isPresent()); + } + + @Test + public void testAllMemberOperations() throws GitLabApiException { + + // Arrange and Act + Member member = gitLabApi.getGroupApi().addMember(testGroup.getId(), testUser.getId(), AccessLevel.DEVELOPER); + + // Assert assertNotNull(member); assertEquals(testUser.getId(), member.getId()); assertEquals(AccessLevel.DEVELOPER, member.getAccessLevel()); + // Act + List members = gitLabApi.getGroupApi().getAllMembers(testGroup); + + // Assert + assertNotNull(members); + Boolean found = (members.stream().filter(m -> m.getId().equals(member.getId())).findAny().orElse(null) != null); + assertTrue(found); + gitLabApi.getGroupApi().removeMember(testGroup.getId(), testUser.getId()); } diff --git a/src/test/java/org/gitlab4j/api/TestProjectApi.java b/src/test/java/org/gitlab4j/api/TestProjectApi.java index 4a6b9f21bfd5bff1292cfe28c7b70d5e79dad3f0..9ce546221cd27e06e5eb5b85b62e30c79ecbcccc 100644 --- a/src/test/java/org/gitlab4j/api/TestProjectApi.java +++ b/src/test/java/org/gitlab4j/api/TestProjectApi.java @@ -41,6 +41,7 @@ import javax.ws.rs.core.Response; import org.gitlab4j.api.models.AccessLevel; import org.gitlab4j.api.models.Group; +import org.gitlab4j.api.models.Member; import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Variable; import org.gitlab4j.api.models.Visibility; @@ -79,7 +80,9 @@ public class TestProjectApi extends AbstractIntegrationTest { private static final String TEST_PROJECT_NAME_UPDATE = "test-gitlab4j-create-project-update"; private static final String TEST_XFER_PROJECT_NAME = "test-gitlab4j-xfer-project"; private static final String TEST_VARIABLE_KEY_PREFIX = "TEST_VARIABLE_KEY_"; + private static GitLabApi gitLabApi; + private static Project testProject; public TestProjectApi() { super(); @@ -90,6 +93,7 @@ public class TestProjectApi extends AbstractIntegrationTest { // Must setup the connection to the GitLab test server gitLabApi = baseTestSetup(); + testProject = getTestProject(); deleteAllTestProjects(); } @@ -126,16 +130,15 @@ public class TestProjectApi extends AbstractIntegrationTest { if (TEST_GROUP != null && TEST_PROJECT_NAME != null) { try { - Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); List groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP); - gitLabApi.getProjectApi().unshareProject(project.getId(), groups.get(0).getId()); + gitLabApi.getProjectApi().unshareProject(testProject, groups.get(0).getId()); - List variables = gitLabApi.getProjectApi().getVariables(project); + List variables = gitLabApi.getProjectApi().getVariables(testProject); if (variables != null) { for (Variable variable : variables) { if (variable.getKey().startsWith(TEST_VARIABLE_KEY_PREFIX)) { - gitLabApi.getProjectApi().deleteVariable(project, variable.getKey()); + gitLabApi.getProjectApi().deleteVariable(testProject, variable.getKey()); } } } @@ -353,18 +356,17 @@ public class TestProjectApi extends AbstractIntegrationTest { @Test public void testListStarredProjects() throws GitLabApiException { - Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); - assertNotNull(project); + assumeNotNull(testProject); try { - gitLabApi.getProjectApi().starProject(project); + gitLabApi.getProjectApi().starProject(testProject); } catch (Exception ignore) { } List projects = gitLabApi.getProjectApi().getStarredProjects(); try { - gitLabApi.getProjectApi().unstarProject(project); + gitLabApi.getProjectApi().unstarProject(testProject); } catch (Exception ignore) { } @@ -377,11 +379,10 @@ public class TestProjectApi extends AbstractIntegrationTest { @Test public void testListStarredProjectsWithParams() throws GitLabApiException { - Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); - assertNotNull(project); + assumeNotNull(testProject); try { - gitLabApi.getProjectApi().starProject(project); + gitLabApi.getProjectApi().starProject(testProject); } catch (Exception ignore) { } @@ -389,7 +390,7 @@ public class TestProjectApi extends AbstractIntegrationTest { Constants.ProjectOrderBy.NAME, Constants.SortOrder.DESC, TEST_PROJECT_NAME, true, true, true, true, true); try { - gitLabApi.getProjectApi().unstarProject(project); + gitLabApi.getProjectApi().unstarProject(testProject); } catch (Exception ignore) { } @@ -493,25 +494,21 @@ public class TestProjectApi extends AbstractIntegrationTest { assumeTrue(TEST_GROUP != null && TEST_GROUP_PROJECT != null); assumeTrue(TEST_GROUP.trim().length() > 0 && TEST_GROUP_PROJECT.trim().length() > 0); - - Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); - assertNotNull(project); + assumeNotNull(testProject); List groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP); assertNotNull(groups); Group shareGroup = groups.get(0); - gitLabApi.getProjectApi().shareProject(project.getId(), shareGroup.getId(), AccessLevel.DEVELOPER, null); - gitLabApi.getProjectApi().unshareProject(project.getId(), shareGroup.getId()); + gitLabApi.getProjectApi().shareProject(testProject, shareGroup.getId(), AccessLevel.DEVELOPER, null); + gitLabApi.getProjectApi().unshareProject(testProject, shareGroup.getId()); } @Test public void testArchiveProject() throws GitLabApiException { - Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); - assertNotNull(project); - - assertEquals(true, gitLabApi.getProjectApi().archiveProject(project.getId()).getArchived()); - assertEquals(false, gitLabApi.getProjectApi().unarchiveProject(project.getId()).getArchived()); + assertNotNull(testProject); + assertEquals(true, gitLabApi.getProjectApi().archiveProject(testProject.getId()).getArchived()); + assertEquals(false, gitLabApi.getProjectApi().unarchiveProject(testProject).getArchived()); } @Test @@ -542,19 +539,18 @@ public class TestProjectApi extends AbstractIntegrationTest { @Test public void testStarAndUnstarProject() throws GitLabApiException { - Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); - assertNotNull(project); + assumeNotNull(testProject); try { - gitLabApi.getProjectApi().unstarProject(project); + gitLabApi.getProjectApi().unstarProject(testProject); } catch (Exception ignore) { } - Project starredProject = gitLabApi.getProjectApi().starProject(project); + Project starredProject = gitLabApi.getProjectApi().starProject(testProject); assertNotNull(starredProject); assertEquals(1, (int)starredProject.getStarCount()); - Project unstarredProject = gitLabApi.getProjectApi().unstarProject(project); + Project unstarredProject = gitLabApi.getProjectApi().unstarProject(testProject); assertNotNull(unstarredProject); assertEquals(0, (int)unstarredProject.getStarCount()); } @@ -582,18 +578,17 @@ public class TestProjectApi extends AbstractIntegrationTest { @Test public void testVariables() throws GitLabApiException { - Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); - assertNotNull(project); + assumeNotNull(testProject); String key = TEST_VARIABLE_KEY_PREFIX + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt(); String value = "TEST_VARIABLE_VALUE_" + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt(); - Variable variable = gitLabApi.getProjectApi().createVariable(project, key, value, null, null); + Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, null, null); assertNotNull(variable); assertEquals(key, variable.getKey()); assertEquals(value, variable.getValue()); - Stream variables = gitLabApi.getProjectApi().getVariablesStream(project); + Stream variables = gitLabApi.getProjectApi().getVariablesStream(testProject); assertNotNull(variables); Variable matchingVariable = variables.filter(v -> v.getKey().equals(key)).findAny().orElse(null); @@ -603,19 +598,43 @@ public class TestProjectApi extends AbstractIntegrationTest { assertFalse(matchingVariable.getProtected()); assertNull(matchingVariable.getEnvironmentScope()); - gitLabApi.getProjectApi().updateVariable(project, key, "NONE", true, "DEV"); - variable = gitLabApi.getProjectApi().getVariable(project, key); + gitLabApi.getProjectApi().updateVariable(testProject, key, "NONE", true, "DEV"); + variable = gitLabApi.getProjectApi().getVariable(testProject, key); assertNotNull(variable); assertEquals(key, variable.getKey()); assertEquals("NONE", variable.getValue()); assertTrue(variable.getProtected()); - gitLabApi.getProjectApi().deleteVariable(project, key); - variables = gitLabApi.getProjectApi().getVariablesStream(project); + gitLabApi.getProjectApi().deleteVariable(testProject, key); + variables = gitLabApi.getProjectApi().getVariablesStream(testProject); assertNotNull(variables); matchingVariable = variables.filter(v -> v.getKey().equals(key)).findAny().orElse(null); assertNull(matchingVariable); } + + @Test + public void testGetMembers() throws GitLabApiException { + + assumeNotNull(testProject); + + // Act + List members = gitLabApi.getProjectApi().getMembers(testProject); + + // Assert + assertNotNull(members); + } + + @Test + public void testAllMemberOperations() throws GitLabApiException { + + assumeNotNull(testProject); + + // Act + List members = gitLabApi.getProjectApi().getAllMembers(testProject); + + // Assert + assertNotNull(members); + } }