Skip to content

Commit 463c6e9

Browse files
committed
forkProject() methods now return the newly forked Project instance (#106).
1 parent b863c78 commit 463c6e9

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -841,10 +841,11 @@ public void deleteProject(Project project) throws GitLabApiException {
841841
* @param namespace path of the namespace that the project will be forked to
842842
* @throws GitLabApiException if any exception occurs
843843
*/
844-
public void forkProject(Integer id, String namespace) throws GitLabApiException {
844+
public Project forkProject(Integer id, String namespace) throws GitLabApiException {
845845
GitLabApiForm formData = new GitLabApiForm().withParam("namespace", namespace, true);
846-
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
847-
post(expectedStatus, formData, "projects", id, "fork");
846+
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.CREATED);
847+
Response response = post(expectedStatus, formData, "projects", id, "fork");
848+
return (response.readEntity(Project.class));
848849
}
849850

850851
/**
@@ -858,8 +859,8 @@ public void forkProject(Integer id, String namespace) throws GitLabApiException
858859
* @param namespace path of the namespace that the project will be forked to
859860
* @throws GitLabApiException if any exception occurs
860861
*/
861-
public void forkProject(Project project, String namespace) throws GitLabApiException {
862-
forkProject(project.getId(), namespace);
862+
public Project forkProject(Project project, String namespace) throws GitLabApiException {
863+
return (forkProject(project.getId(), namespace));
863864
}
864865

865866
/**
@@ -873,10 +874,11 @@ public void forkProject(Project project, String namespace) throws GitLabApiExcep
873874
* @param namespaceId ID of the namespace that the project will be forked to
874875
* @throws GitLabApiException if any exception occurs
875876
*/
876-
public void forkProject(Integer id, Integer namespaceId) throws GitLabApiException {
877+
public Project forkProject(Integer id, Integer namespaceId) throws GitLabApiException {
877878
GitLabApiForm formData = new GitLabApiForm().withParam("namespace", namespaceId, true);
878-
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
879-
post(expectedStatus, formData, "projects", id, "fork");
879+
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.CREATED);
880+
Response response = post(expectedStatus, formData, "projects", id, "fork");
881+
return (response.readEntity(Project.class));
880882
}
881883

882884
/**
@@ -890,8 +892,8 @@ public void forkProject(Integer id, Integer namespaceId) throws GitLabApiExcepti
890892
* @param namespaceId ID of the namespace that the project will be forked to
891893
* @throws GitLabApiException if any exception occurs
892894
*/
893-
public void forkProject(Project project, Integer namespaceId) throws GitLabApiException {
894-
forkProject(project.getId(), namespaceId);
895+
public Project forkProject(Project project, Integer namespaceId) throws GitLabApiException {
896+
return (forkProject(project.getId(), namespaceId));
895897
}
896898

897899
/**

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* TEST_PROJECT_NAME
4848
* TEST_HOST_URL
4949
* TEST_PRIVATE_TOKEN
50+
* TEST_GROUP_PROJECT
5051
*
5152
* If any of the above are NULL, all tests in this class will be skipped.
5253
*
@@ -60,12 +61,16 @@ public class TestProjectApi {
6061
private static final String TEST_PROJECT_NAME;
6162
private static final String TEST_HOST_URL;
6263
private static final String TEST_PRIVATE_TOKEN;
64+
private static final String TEST_GROUP;
65+
private static final String TEST_GROUP_PROJECT;
6366

6467
static {
6568
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
6669
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
6770
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
6871
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
72+
TEST_GROUP = TestUtils.getProperty("TEST_GROUP");
73+
TEST_GROUP_PROJECT = TestUtils.getProperty("TEST_GROUP_PROJECT");
6974
}
7075

7176
private static final String TEST_PROJECT_NAME_1 = "test-gitlab4j-create-project";
@@ -123,6 +128,14 @@ private static void deleteAllTestProjects() {
123128
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_UPDATE);
124129
gitLabApi.getProjectApi().deleteProject(project);
125130
} catch (GitLabApiException ignore) {}
131+
132+
if (TEST_GROUP != null && TEST_GROUP_PROJECT != null) {
133+
try {
134+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_GROUP_PROJECT);
135+
gitLabApi.getProjectApi().deleteProject(project);
136+
} catch (GitLabApiException ignore) {
137+
}
138+
}
126139
}
127140
}
128141

@@ -382,4 +395,16 @@ public void testMemberProjectsPerPage() throws GitLabApiException {
382395
assertTrue(projects != null);
383396
assertTrue(projects.size() > 0);
384397
}
398+
399+
@Test
400+
public void testForkProject() throws GitLabApiException {
401+
402+
assumeTrue(TEST_GROUP != null && TEST_GROUP_PROJECT != null);
403+
assumeTrue(TEST_GROUP.trim().length() > 0 && TEST_GROUP_PROJECT.trim().length() > 0);
404+
405+
Project project = gitLabApi.getProjectApi().getProject(TEST_GROUP, TEST_GROUP_PROJECT);
406+
assertNotNull(project);
407+
Project forkedProject = gitLabApi.getProjectApi().forkProject(project.getId(), TEST_NAMESPACE);
408+
assertNotNull(forkedProject);
409+
}
385410
}

0 commit comments

Comments
 (0)