Commit 6753e62f authored by Greg Messner's avatar Greg Messner
Browse files

Added share project support (#112).

parent 1790d4db
......@@ -1664,4 +1664,38 @@ public class ProjectApi extends AbstractApi implements Constants {
Response response = get(Response.Status.OK, null, "projects", projectId, "snippets", snippetId, "raw");
return (response.readEntity(String.class));
}
/**
* Share a project with the specified group.
*
* POST /projects/:id/share
*
* @param projectId the ID of the project to share, required
* @param groupId the ID of the group to share with, required
* @param accessLevel the permissions level to grant the group, required
* @param expiresAt the share expiration date, optional
* @throws GitLabApiException if any exception occurs
*/
public void shareProject(Integer projectId, Integer groupId, AccessLevel accessLevel, Date expiresAt)
throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("group_id", groupId, true)
.withParam("group_access", accessLevel.toValue(), true)
.withParam("expires_at", expiresAt);
post(Response.Status.CREATED, formData, "projects", projectId, "share");
}
/**
* Unshare the project from the group.
*
* DELETE /projects/:id/share/:group_id
*
* @param projectId the ID of the project to unshare, required
* @param groupId the ID of the group to unshare, required
* @throws GitLabApiException if any exception occurs
*/
public void unshareProject(Integer projectId, Integer groupId) throws GitLabApiException {
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
delete(expectedStatus, null, "projects", projectId, "share", groupId);
}
}
......@@ -31,6 +31,8 @@ import static org.junit.Assume.assumeTrue;
import java.util.List;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.Visibility;
import org.junit.AfterClass;
......@@ -129,6 +131,15 @@ public class TestProjectApi {
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {}
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());
} catch (GitLabApiException ignore) {
}
}
if (TEST_GROUP != null && TEST_GROUP_PROJECT != null) {
try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_GROUP_PROJECT);
......@@ -407,4 +418,21 @@ public class TestProjectApi {
Project forkedProject = gitLabApi.getProjectApi().forkProject(project.getId(), TEST_NAMESPACE);
assertNotNull(forkedProject);
}
@Test
public void testShareProject() throws GitLabApiException {
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);
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());
}
}
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