Unverified Commit d72fb684 authored by Greg Messner's avatar Greg Messner Committed by GitHub
Browse files

Feature-351: Add support for get all members to ProjectApi and GroupApi

* Added getAllMembers() methods (#348).
* Added tests for getAllMembers() methods (#348).
parent a6b7c789
......@@ -21,6 +21,8 @@ import org.gitlab4j.api.models.Visibility;
/**
* This class implements the client side API for the GitLab groups calls.
* @see <a href="https://docs.gitlab.com/ce/api/groups.html">Groups API at GitLab</a>
* @see <a href="https://docs.gitlab.com/ee/api/members.html">Group and project members API at GitLab</a>
*/
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<Member>() {}));
}
......@@ -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.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/members/all</code></pre>
*
* @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<Member> 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.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/members/all</code></pre>
*
* @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<Member> 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<List<Member>>() {}));
}
/**
* 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.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/members/all</code></pre>
*
* @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<Member> getAllMembers(Object groupIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<Member>(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.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/members/all</code></pre>
*
* @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<Member> getAllMembersStream(Object groupIdOrPath) throws GitLabApiException {
return (getAllMembers(groupIdOrPath, getDefaultPerPage()).stream());
}
/**
* Adds a user to the list of group members.
*
......
......@@ -54,6 +54,8 @@ import org.gitlab4j.api.models.Visibility;
/**
* This class provides an entry point to all the GitLab API project calls.
* @see <a href="https://docs.gitlab.com/ce/api/projects.html">Projects API at GitLab</a>
* @see <a href="https://docs.gitlab.com/ee/api/members.html">Group and project members API at GitLab</a>
*/
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.
*
* <pre><code>GET /projects/:id/members/all</code></pre>
*
* @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<Member> 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.
*
* <pre><code>GET /projects/:id/members</code></pre>
*
* @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<Member> 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<List<Member>>() {}));
}
/**
* 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.
*
* <pre><code>GET /projects/:id/members/all</code></pre>
*
* @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<Member> getAllMembers(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<Member>(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.
*
* <pre><code>GET /projects/:id/members/all</code></pre>
*
* @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<Member> getAllMembersStream(Object projectIdOrPath) throws GitLabApiException {
return (getAllMembers(projectIdOrPath, getDefaultPerPage()).stream());
}
/**
* Gets a project team member.
*
......
......@@ -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<Member> optionalMember = gitLabApi.getGroupApi().getOptionalMember(testGroup, testUser.getId());
// Assert
assertTrue(optionalMember.isPresent());
// Act
List<Member> 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<Member> 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());
}
......
......@@ -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<Group> groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP);
gitLabApi.getProjectApi().unshareProject(project.getId(), groups.get(0).getId());
gitLabApi.getProjectApi().unshareProject(testProject, groups.get(0).getId());
List<Variable> variables = gitLabApi.getProjectApi().getVariables(project);
List<Variable> 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<Project> 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<Group> 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<Variable> variables = gitLabApi.getProjectApi().getVariablesStream(project);
Stream<Variable> 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<Member> members = gitLabApi.getProjectApi().getMembers(testProject);
// Assert
assertNotNull(members);
}
@Test
public void testAllMemberOperations() throws GitLabApiException {
assumeNotNull(testProject);
// Act
List<Member> members = gitLabApi.getProjectApi().getAllMembers(testProject);
// Assert
assertNotNull(members);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment