GroupApi.java 15.85 KiB
package org.gitlab4j.api;
import java.util.List;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.Visibility;
/**
 * This class implements the client side API for the GitLab groups calls.
public class GroupApi extends AbstractApi {
    public GroupApi(GitLabApi gitLabApi) {
        super(gitLabApi);
    /**
     * Get a list of groups. (As user: my groups, as admin: all groups)
     * GET /groups
     * @return the list of groups viewable by the authenticated user
     * @throws GitLabApiException if any exception occurs
    public List<Group> getGroups() throws GitLabApiException {
        Response response = get(Response.Status.OK, getDefaultPerPageParam(), "groups");
        return (response.readEntity(new GenericType<List<Group>>() {}));
    /**
     * Get a list of groups (As user: my groups, as admin: all groups) and in the specified page range.
     * GET /groups
     * @param page the page to get
     * @param perPage the number of Group instances per page
     * @return the list of groups viewable by the authenticated userin the specified page range
     * @throws GitLabApiException if any exception occurs
    public List<Group> getGroups(int page, int perPage) throws GitLabApiException {
        Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "groups");
        return (response.readEntity(new GenericType<List<Group>>() {}));
    /**
     * Get a Pager of groups. (As user: my groups, as admin: all groups)
     * GET /groups
     * @param itemsPerPage the number of Group instances that will be fetched per page
     * @return the list of groups viewable by the authenticated user
     * @throws GitLabApiException if any exception occurs
    public Pager<Group> getGroups(int itemsPerPage) throws GitLabApiException {
        return (new Pager<Group>(this, Group.class, itemsPerPage, null, "groups"));
    /**
     * Get all groups that match your string in their name or path.
     * @param search the group name or path search criteria
     * @return a List containing matching Group instances
     * @throws GitLabApiException if any exception occurs
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
*/ public List<Group> getGroups(String search) throws GitLabApiException { Form formData = new GitLabApiForm().withParam("search", search).withParam(PER_PAGE_PARAM, getDefaultPerPage()); Response response = get(Response.Status.OK, formData.asMap(), "groups"); return (response.readEntity(new GenericType<List<Group>>() {})); } /** * Get all groups that match your string in their name or path. * * @param search the group name or path search criteria * @param page the page to get * @param perPage the number of Group instances per page * @return a List containing matching Group instances * @throws GitLabApiException if any exception occurs */ public List<Group> getGroups(String search, int page, int perPage) throws GitLabApiException { Form formData = new GitLabApiForm().withParam("search", search).withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage); Response response = get(Response.Status.OK, formData.asMap(), "groups"); return (response.readEntity(new GenericType<List<Group>>() {})); } /** * Get all groups that match your string in their name or path. * * @param search the group name or path search criteria * @param itemsPerPage the number of Group instances that will be fetched per page * @return a List containing matching Group instances * @throws GitLabApiException if any exception occurs */ public Pager<Group> getGroups(String search, int itemsPerPage) throws GitLabApiException { Form formData = new GitLabApiForm().withParam("search", search); return (new Pager<Group>(this, Group.class, itemsPerPage, formData.asMap(), "groups")); } /** * Get a list of projects belonging to the specified group ID. * * GET /groups/:id/projects * * @param groupId the group ID to list the projects for * @return a list of projects belonging to the specified group ID * @throws GitLabApiException if any exception occurs */ public List<Project> getProjects(int groupId) throws GitLabApiException { Response response = get(Response.Status.OK, getDefaultPerPageParam(), "groups", groupId, "projects"); return (response.readEntity(new GenericType<List<Project>>() {})); } /** * Get a list of projects belonging to the specified group ID in the specified page range. * * GET /groups/:id/projects * * @param groupId the group ID to list the projects for * @param page the page to get * @param perPage the number of Project instances per page * @return a list of projects belonging to the specified group ID in the specified page range * @throws GitLabApiException if any exception occurs */ public List<Project> getProjects(int groupId, int page, int perPage) throws GitLabApiException { Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "groups", groupId, "projects"); return (response.readEntity(new GenericType<List<Project>>() {})); } /** * Get a Pager of projects belonging to the specified group ID. * * GET /groups/:id/projects *
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
* @param groupId the group ID to list the projects for * @param itemsPerPage the number of Project instances that will be fetched per page * @return a Pager of projects belonging to the specified group ID * @throws GitLabApiException if any exception occurs */ public Pager<Project> getProjects(int groupId, int itemsPerPage) throws GitLabApiException { return (new Pager<Project>(this, Project.class, itemsPerPage, null, "groups", groupId, "projects")); } /** * Get all details of a group. * * GET /groups/:id * * @param groupId the group ID to get * @return the Group instance for the specified group ID * @throws GitLabApiException if any exception occurs */ public Group getGroup(Integer groupId) throws GitLabApiException { return getGroup(groupId.toString()); } /** * Get all details of a group. * * GET /groups/:id * * @param groupPath the path of the group to get details for * @return the Group instance for the specified group path * @throws GitLabApiException if any exception occurs */ public Group getGroup(String groupPath) throws GitLabApiException { Response response = get(Response.Status.OK, null, "groups", urlEncode(groupPath)); return (response.readEntity(Group.class)); } /** * Creates a new project group. Available only for users who can create groups. * * POST /groups * * @param name the name of the group to add * @param path the path for the group * @throws GitLabApiException if any exception occurs */ public void addGroup(String name, String path) throws GitLabApiException { Form formData = new Form(); formData.param("name", name); formData.param("path", path); post(Response.Status.CREATED, formData, "groups"); } /** * Creates a new project group. Available only for users who can create groups. * * POST /groups * * @param name the name of the group to add * @param path the path for the group * @param description (optional) - The group's description * @param membershipLock (optional, boolean) - Prevent adding new members to project membership within this group * @param shareWithGroupLock (optional, boolean) - Prevent sharing a project with another group within this group * @param visibility (optional) - The group's visibility. Can be private, internal, or public. * @param lfsEnabled (optional) - Enable/disable Large File Storage (LFS) for the projects in this group * @param requestAccessEnabled (optional) - Allow users to request member access. * @param parentId (optional) - The parent group id for creating nested group. * @param sharedRunnersMinutesLimit (optional) - (admin-only) Pipeline minutes quota for this group * @throws GitLabApiException if any exception occurs */
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
public void addGroup(String name, String path, String description, Boolean membershipLock, Boolean shareWithGroupLock, Visibility visibility, Boolean lfsEnabled, Boolean requestAccessEnabled, Integer parentId, Integer sharedRunnersMinutesLimit) throws GitLabApiException { Form formData = new GitLabApiForm() .withParam("name", name) .withParam("path", path) .withParam("description", description) .withParam("membership_lock", membershipLock) .withParam("share_with_group_lock", shareWithGroupLock) .withParam("visibility", visibility) .withParam("lfs_enabled", lfsEnabled) .withParam("request_access_enabled", requestAccessEnabled) .withParam("parent_id", parentId) .withParam("shared_runners_minutes_limit", sharedRunnersMinutesLimit); post(Response.Status.CREATED, formData, "groups"); } /** * Creates a new project group. Available only for users who can create groups. * * PUT /groups * * @param groupId the ID of the group to update * @param name the name of the group to add * @param path the path for the group * @param description (optional) - The group's description * @param membershipLock (optional, boolean) - Prevent adding new members to project membership within this group * @param shareWithGroupLock (optional, boolean) - Prevent sharing a project with another group within this group * @param visibility (optional) - The group's visibility. Can be private, internal, or public. * @param lfsEnabled (optional) - Enable/disable Large File Storage (LFS) for the projects in this group * @param requestAccessEnabled (optional) - Allow users to request member access. * @param parentId (optional) - The parent group id for creating nested group. * @param sharedRunnersMinutesLimit (optional) - (admin-only) Pipeline minutes quota for this group * @return the updated Group instance * @throws GitLabApiException if any exception occurs */ public Group updateGroup(Integer groupId, String name, String path, String description, Boolean membershipLock, Boolean shareWithGroupLock, Visibility visibility, Boolean lfsEnabled, Boolean requestAccessEnabled, Integer parentId, Integer sharedRunnersMinutesLimit) throws GitLabApiException { Form formData = new GitLabApiForm() .withParam("name", name) .withParam("path", path) .withParam("description", description) .withParam("membership_lock", membershipLock) .withParam("share_with_group_lock", shareWithGroupLock) .withParam("visibility", visibility) .withParam("lfs_enabled", lfsEnabled) .withParam("request_access_enabled", requestAccessEnabled) .withParam("parent_id", parentId) .withParam("shared_runners_minutes_limit", sharedRunnersMinutesLimit); Response response = put(Response.Status.OK, formData.asMap(), "groups", groupId); return (response.readEntity(Group.class)); } /** * Removes group with all projects inside. * * DELETE /groups/:id * * @param groupId the group ID to delete * @throws GitLabApiException if any exception occurs */ public void deleteGroup(Integer groupId) throws GitLabApiException { if (groupId == null) { throw new RuntimeException("groupId cannot be null"); }
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, null, "groups", groupId); } /** * Removes group with all projects inside. * * DELETE /groups/:id * * @param group the Group instance to delete * @throws GitLabApiException if any exception occurs */ public void deleteGroup(Group group) throws GitLabApiException { deleteGroup(group.getId()); } /** * Get a list of group members viewable by the authenticated user. * * GET /groups/:id/members * * @param groupId the group ID to list the members for * @return a list of group members viewable by the authenticated user * @throws GitLabApiException if any exception occurs */ public List<Member> getMembers(int groupId) throws GitLabApiException { Response response = get(Response.Status.OK, getDefaultPerPageParam(), "groups", groupId, "members"); return (response.readEntity(new GenericType<List<Member>>() {})); } /** * Get a list of group members viewable by the authenticated user in the specified page range. * * GET /groups/:id/members * * @param groupId the group ID to list the members for * @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 in the specified page range * @throws GitLabApiException if any exception occurs */ public List<Member> getMembers(int groupId, int page, int perPage) throws GitLabApiException { Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "groups", groupId, "members"); return (response.readEntity(new GenericType<List<Member>>() {})); } /** * Get a Pager of group members viewable by the authenticated user. * * GET /groups/:id/members * * @param groupId the group ID to list the members for * @param itemsPerPage the number of Member instances that will be fetched per page * @return a list of group members viewable by the authenticated user * @throws GitLabApiException if any exception occurs */ public Pager<Member> getMembers(int groupId, int itemsPerPage) throws GitLabApiException { return (new Pager<Member>(this, Member.class, itemsPerPage, null, "groups", groupId, "members")); } /** * Adds a user to the list of group members. * * POST /groups/:id/members * * @param groupId the project ID to add the member to * @param userId the user ID of the member to add * @param accessLevel the access level for the new member * @return a Member instance for the added user
351352353354355356357358359360361362363364365366367368369370371372373374375376
* @throws GitLabApiException if any exception occurs */ public Member addMember(Integer groupId, Integer userId, Integer accessLevel) throws GitLabApiException { Form formData = new Form(); formData.param("user_id", userId.toString()); formData.param("access_level", accessLevel.toString()); Response response = post(Response.Status.CREATED, formData, "groups", groupId, "members"); return (response.readEntity(Member.class)); } /** * Removes member from the group team. * * DELETE /groups/:id/members/:user_id * * @param projectId the project ID to remove the member from * @param userId the user ID of the member to remove * @throws GitLabApiException if any exception occurs */ public void removeMember(Integer projectId, Integer userId) throws GitLabApiException { Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, null, "groups", projectId, "members", userId); } }