Commit e4f38608 authored by Greg Messner's avatar Greg Messner
Browse files

Added createGroup() and updateGroup() with GroupParams (#482).

parent 4119f463
......@@ -15,6 +15,7 @@ import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Badge;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.GroupFilter;
import org.gitlab4j.api.models.GroupParams;
import org.gitlab4j.api.models.GroupProjectsFilter;
import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Project;
......@@ -472,6 +473,36 @@ public class GroupApi extends AbstractApi {
}
}
/**
* Creates a new project group. Available only for users who can create groups.
*
* <pre><code>GitLab Endpoint: POST /groups</code></pre>
*
* @param params a GroupParams instance holding the parameters for the group creation
* @return the created Group instance
* @throws GitLabApiException if any exception occurs
*/
public Group createGroup(GroupParams params) throws GitLabApiException {
Response response = post(Response.Status.CREATED, params.getForm(true), "groups");
return (response.readEntity(Group.class));
}
/**
* Updates the project group. Only available to group owners and administrators.
*
* <pre><code>GitLab Endpoint: PUT /groups</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @param params the GroupParams instance holding the properties to update
* @return updated Group instance
* @throws GitLabApiException at any exception
*/
public Group updateGroup(Object groupIdOrPath, GroupParams params) throws GitLabApiException {
Response response = putWithFormData(Response.Status.OK,
params.getForm(false), "groups", getGroupIdOrPath(groupIdOrPath));
return (response.readEntity(Group.class));
}
/**
* Creates a new project group. Available only for users who can create groups.
*
......
package org.gitlab4j.api.models;
import org.gitlab4j.api.GitLabApiForm;
public class GroupParams {
private String name;
private String path;
private String description;
private String visibility;
private Boolean shareWithGroupLock;
private Boolean requireTwoFactorAuthentication;
private Integer twoFactorGracePeriod;
private AccessLevel projectCreationLevel;
private Boolean autoDevopsEnabled;
private AccessLevel subgroupCreationLevel;
private Boolean emailsDisabled;
private Boolean lfsEnabled;
private Boolean requestAccessEnabled;
private Integer parentId;
private Integer sharedRunnersMinutesLimit;
private Integer extraSharedRunnersMinutesLimit;
private Boolean membershipLock;
private Integer fileTemplateProjectId;
/**
* The parent group ID for creating nested group. For create only.
*
* @param parentId the parent group ID for creating nested group
* @return this GroupParms instance
*/
public GroupParams withParentId(Integer parentId) {
this.parentId = parentId;
return (this);
}
/**
* Prevent adding new members to project membership within this group. For update only.
*
* @param membershipLock if true, prevent adding new members to project membership within this group
* @return this GroupParms instance
*/
public GroupParams withMembershipLock(Boolean membershipLock) {
this.membershipLock = membershipLock;
return (this);
}
/**
* The ID of a project to load custom file templates from. For update only.
*
* @param fileTemplateProjectId the ID of a project to load custom file templates from
* @return this GroupParms instance
*/
public GroupParams withFileTemplateProjectId(Integer fileTemplateProjectId) {
this.fileTemplateProjectId = fileTemplateProjectId;
return (this);
}
public GroupParams withName(String name) {
this.name = name;
return (this);
}
public GroupParams withPath(String path) {
this.path = path;
return (this);
}
public GroupParams withDescription(String description) {
this.description = description;
return (this);
}
public GroupParams withVisibility(String visibility) {
this.visibility = visibility;
return (this);
}
public GroupParams withShareWithGroupLock(Boolean shareWithGroupLock) {
this.shareWithGroupLock = shareWithGroupLock;
return (this);
}
public GroupParams withRequireTwoFactorAuthentication(Boolean requireTwoFactorAuthentication) {
this.requireTwoFactorAuthentication = requireTwoFactorAuthentication;
return (this);
}
public GroupParams withTwoFactorGracePeriod(Integer twoFactorGracePeriod) {
this.twoFactorGracePeriod = twoFactorGracePeriod;
return (this);
}
public GroupParams withProjectCreationLevel(AccessLevel projectCreationLevel) {
this.projectCreationLevel = projectCreationLevel;
return (this);
}
public GroupParams withAutoDevopsEnabled(Boolean autoDevopsEnabled) {
this.autoDevopsEnabled = autoDevopsEnabled;
return (this);
}
public GroupParams withSubgroupCreationLevel(AccessLevel subgroupCreationLevel) {
this.subgroupCreationLevel = subgroupCreationLevel;
return (this);
}
public GroupParams withEmailsDisabled(Boolean emailsDisabled) {
this.emailsDisabled = emailsDisabled;
return (this);
}
public GroupParams withLfsEnabled(Boolean lfsEnabled) {
this.lfsEnabled = lfsEnabled;
return (this);
}
public GroupParams withRequestAccessEnabled(Boolean requestAccessEnabled) {
this.requestAccessEnabled = requestAccessEnabled;
return (this);
}
public GroupParams withSharedRunnersMinutesLimit(Integer sharedRunnersMinutesLimit) {
this.sharedRunnersMinutesLimit = sharedRunnersMinutesLimit;
return (this);
}
public GroupParams withExtraSharedRunnersMinutesLimit(Integer extraSharedRunnersMinutesLimit) {
this.extraSharedRunnersMinutesLimit = extraSharedRunnersMinutesLimit;
return (this);
}
/**
* Get the form params for a group create oir update call.
*
* @param isCreate set to true for a create group call, false for update
* @return a GitLabApiForm instance holding the parameters for the group create or update operation
* @throws RuntimeException if required parameters are missing
*/
public GitLabApiForm getForm(boolean isCreate) {
GitLabApiForm form = new GitLabApiForm()
.withParam("name", name, isCreate)
.withParam("path", path, isCreate)
.withParam("description", description)
.withParam("visibility", visibility)
.withParam("share_with_group_lock", shareWithGroupLock)
.withParam("require_two_factor_authentication", requireTwoFactorAuthentication)
.withParam("two_factor_grace_period", twoFactorGracePeriod)
.withParam("project_creation_level", projectCreationLevel)
.withParam("auto_devops_enabled", autoDevopsEnabled)
.withParam("subgroup_creation_level", subgroupCreationLevel)
.withParam("emails_disabled", emailsDisabled)
.withParam("lfs_enabled", lfsEnabled)
.withParam("request_access_enabled", requestAccessEnabled)
.withParam("shared_runners_minutes_limit", sharedRunnersMinutesLimit)
.withParam("extra_shared_runners_minutes_limit", extraSharedRunnersMinutesLimit);
if (isCreate) {
form.withParam("parent_id", parentId);
} else {
form.withParam("membership_lock", membershipLock)
.withParam("file_template_project_id", fileTemplateProjectId);
}
return (form);
}
}
......@@ -16,6 +16,7 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.GroupParams;
import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.User;
import org.junit.AfterClass;
......@@ -280,4 +281,19 @@ public class TestGroupApi extends AbstractIntegrationTest {
}
}
}
@Test
public void updateGroup() throws GitLabApiException {
String description = "Test Group (" + HelperUtils.getRandomInt(1000) + ")";
GroupParams params = new GroupParams().withDescription(description);
Group updatedGroup = gitLabApi.getGroupApi().updateGroup(testGroup, params);
assertEquals(description, updatedGroup.getDescription());
Optional<Group> optional = gitLabApi.getGroupApi().getOptionalGroup(TEST_GROUP);
assertTrue(optional.isPresent());
assertEquals(testGroup.getId(), optional.get().getId());
assertEquals(description, optional.get().getDescription());
}
}
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