Skip to content

Commit 6753e62

Browse files
committed
Added share project support (#112).
1 parent 1790d4d commit 6753e62

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/main/java/org/gitlab4j/api/ProjectApi.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,4 +1664,38 @@ public String getRawSnippetContent(Integer projectId, Integer snippetId) throws
16641664
Response response = get(Response.Status.OK, null, "projects", projectId, "snippets", snippetId, "raw");
16651665
return (response.readEntity(String.class));
16661666
}
1667+
1668+
/**
1669+
* Share a project with the specified group.
1670+
*
1671+
* POST /projects/:id/share
1672+
*
1673+
* @param projectId the ID of the project to share, required
1674+
* @param groupId the ID of the group to share with, required
1675+
* @param accessLevel the permissions level to grant the group, required
1676+
* @param expiresAt the share expiration date, optional
1677+
* @throws GitLabApiException if any exception occurs
1678+
*/
1679+
public void shareProject(Integer projectId, Integer groupId, AccessLevel accessLevel, Date expiresAt)
1680+
throws GitLabApiException {
1681+
GitLabApiForm formData = new GitLabApiForm()
1682+
.withParam("group_id", groupId, true)
1683+
.withParam("group_access", accessLevel.toValue(), true)
1684+
.withParam("expires_at", expiresAt);
1685+
post(Response.Status.CREATED, formData, "projects", projectId, "share");
1686+
}
1687+
1688+
/**
1689+
* Unshare the project from the group.
1690+
*
1691+
* DELETE /projects/:id/share/:group_id
1692+
*
1693+
* @param projectId the ID of the project to unshare, required
1694+
* @param groupId the ID of the group to unshare, required
1695+
* @throws GitLabApiException if any exception occurs
1696+
*/
1697+
public void unshareProject(Integer projectId, Integer groupId) throws GitLabApiException {
1698+
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
1699+
delete(expectedStatus, null, "projects", projectId, "share", groupId);
1700+
}
16671701
}

src/test/java/org/gitlab4j/api/TestProjectApi.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.List;
3232

3333
import org.gitlab4j.api.GitLabApi.ApiVersion;
34+
import org.gitlab4j.api.models.AccessLevel;
35+
import org.gitlab4j.api.models.Group;
3436
import org.gitlab4j.api.models.Project;
3537
import org.gitlab4j.api.models.Visibility;
3638
import org.junit.AfterClass;
@@ -129,6 +131,15 @@ private static void deleteAllTestProjects() {
129131
gitLabApi.getProjectApi().deleteProject(project);
130132
} catch (GitLabApiException ignore) {}
131133

134+
if (TEST_GROUP != null && TEST_PROJECT_NAME != null) {
135+
try {
136+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
137+
List<Group> groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP);
138+
gitLabApi.getProjectApi().unshareProject(project.getId(), groups.get(0).getId());
139+
} catch (GitLabApiException ignore) {
140+
}
141+
}
142+
132143
if (TEST_GROUP != null && TEST_GROUP_PROJECT != null) {
133144
try {
134145
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_GROUP_PROJECT);
@@ -407,4 +418,21 @@ public void testForkProject() throws GitLabApiException {
407418
Project forkedProject = gitLabApi.getProjectApi().forkProject(project.getId(), TEST_NAMESPACE);
408419
assertNotNull(forkedProject);
409420
}
421+
422+
@Test
423+
public void testShareProject() throws GitLabApiException {
424+
425+
assumeTrue(TEST_GROUP != null && TEST_GROUP_PROJECT != null);
426+
assumeTrue(TEST_GROUP.trim().length() > 0 && TEST_GROUP_PROJECT.trim().length() > 0);
427+
428+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
429+
assertNotNull(project);
430+
431+
List<Group> groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP);
432+
assertNotNull(groups);
433+
434+
Group shareGroup = groups.get(0);
435+
gitLabApi.getProjectApi().shareProject(project.getId(), shareGroup.getId(), AccessLevel.DEVELOPER, null);
436+
gitLabApi.getProjectApi().unshareProject(project.getId(), shareGroup.getId());
437+
}
410438
}

0 commit comments

Comments
 (0)