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

Mods to support creating a project in a namespace (#380).

parent 0fd6f264
...@@ -12,7 +12,7 @@ To utilize GitLab4J™ API in your Java project, simply add the following de ...@@ -12,7 +12,7 @@ To utilize GitLab4J™ API in your Java project, simply add the following de
```java ```java
dependencies { dependencies {
... ...
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.11.7' compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.11.8'
} }
``` ```
...@@ -23,7 +23,7 @@ dependencies { ...@@ -23,7 +23,7 @@ dependencies {
<dependency> <dependency>
<groupId>org.gitlab4j</groupId> <groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId> <artifactId>gitlab4j-api</artifactId>
<version>4.11.7</version> <version>4.11.8</version>
</dependency> </dependency>
``` ```
......
...@@ -44,6 +44,7 @@ import org.gitlab4j.api.models.Event; ...@@ -44,6 +44,7 @@ import org.gitlab4j.api.models.Event;
import org.gitlab4j.api.models.FileUpload; import org.gitlab4j.api.models.FileUpload;
import org.gitlab4j.api.models.Issue; import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.Member; import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Namespace;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectFilter; import org.gitlab4j.api.models.ProjectFilter;
import org.gitlab4j.api.models.ProjectHook; import org.gitlab4j.api.models.ProjectHook;
...@@ -767,19 +768,38 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -767,19 +768,38 @@ public class ProjectApi extends AbstractApi implements Constants {
} }
/** /**
* Create a new project in the specified group. * Create a new project belonging to the namespace ID. A namespace ID is either a user or group ID.
* *
* @param groupId the group ID to create the project under * @param namespaceId the namespace ID to create the project under
* @param projectName the name of the project top create * @param projectName the name of the project top create
* @return the created project * @return the created project
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
public Project createProject(Integer groupId, String projectName) throws GitLabApiException { public Project createProject(Integer namespaceId, String projectName) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("namespace_id", groupId).withParam("name", projectName, true); GitLabApiForm formData = new GitLabApiForm().withParam("namespace_id", namespaceId).withParam("name", projectName, true);
Response response = post(Response.Status.CREATED, formData, "projects"); Response response = post(Response.Status.CREATED, formData, "projects");
return (response.readEntity(Project.class)); return (response.readEntity(Project.class));
} }
/**
* Create a new project belonging to the namespace ID and project configuration. A namespace ID is either a user or group ID.
*
* @param namespaceId the namespace ID to create the project under
* @param project the Project instance holding the new project configuration
* @return the created project
* @throws GitLabApiException if any exception occurs
*/
public Project createProject(Integer namespaceId, Project project) throws GitLabApiException {
if (project == null) {
throw new RuntimeException("Project instance cannot be null.");
}
Namespace namespace = new Namespace().withId(namespaceId);
project.setNamespace(namespace);
return (createProject(project));
}
/** /**
* Create a new project with the current user's namespace. * Create a new project with the current user's namespace.
* *
...@@ -879,6 +899,11 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -879,6 +899,11 @@ public class ProjectApi extends AbstractApi implements Constants {
.withParam("initialize_with_readme", project.getInitializeWithReadme()) .withParam("initialize_with_readme", project.getInitializeWithReadme())
.withParam("packages_enabled", project.getPackagesEnabled()); .withParam("packages_enabled", project.getPackagesEnabled());
Namespace namespace = project.getNamespace();
if (namespace != null && namespace.getId() != null) {
formData.withParam("namespace_id", namespace.getId());
}
if (isApiVersion(ApiVersion.V3)) { if (isApiVersion(ApiVersion.V3)) {
boolean isPublic = (project.getPublic() != null ? project.getPublic() : project.getVisibility() == Visibility.PUBLIC); boolean isPublic = (project.getPublic() != null ? project.getPublic() : project.getVisibility() == Visibility.PUBLIC);
formData.withParam("public", isPublic); formData.withParam("public", isPublic);
......
...@@ -6,6 +6,7 @@ import java.util.WeakHashMap; ...@@ -6,6 +6,7 @@ import java.util.WeakHashMap;
import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.User;
/** /**
* In order for the integration tests to run you must set the following properties in test-gitlab4j.properties * In order for the integration tests to run you must set the following properties in test-gitlab4j.properties
...@@ -120,4 +121,28 @@ public class AbstractIntegrationTest implements PropertyConstants { ...@@ -120,4 +121,28 @@ public class AbstractIntegrationTest implements PropertyConstants {
return (null); return (null);
} }
} }
/**
* Get the current user (the testing user).
*
* @return the user that is being used for testing
*/
protected static User getCurrentUser() {
Throwable t = new Throwable();
StackTraceElement directCaller = t.getStackTrace()[1];
String callingClassName = directCaller.getClassName();
BaseTestResources baseResources = baseTestResourcesMap.get(callingClassName);
if (baseResources == null || baseResources.gitLabApi == null) {
System.err.println("Problems fetching current user: GitLabApi instance is null");
return (null);
}
try {
return (baseResources.gitLabApi.getUserApi().getCurrentUser());
} catch (Exception e) {
System.err.println("Problems fetching current user: " + e.getMessage());
return (null);
}
}
} }
...@@ -259,8 +259,8 @@ public class IntegrationTestSuite implements PropertyConstants { ...@@ -259,8 +259,8 @@ public class IntegrationTestSuite implements PropertyConstants {
.withDefaultBranch("master") .withDefaultBranch("master")
.withPublic(true) .withPublic(true)
.withInitializeWithReadme(true); .withInitializeWithReadme(true);
Project groupProject = gitLabApi.getProjectApi().createProject(projectSettings); Project groupProject = gitLabApi.getProjectApi().createProject(testGroup.getId(), projectSettings);
System.out.format("Created %s project%n", projectSettings.getName()); System.out.format("Created %s project%n", groupProject.getNameWithNamespace());
// Update the contents of README.md, so we have at minimum 2 commits // Update the contents of README.md, so we have at minimum 2 commits
RepositoryFile repoFile = new RepositoryFile(); RepositoryFile repoFile = new RepositoryFile();
...@@ -269,9 +269,6 @@ public class IntegrationTestSuite implements PropertyConstants { ...@@ -269,9 +269,6 @@ public class IntegrationTestSuite implements PropertyConstants {
gitLabApi.getRepositoryFileApi().updateFile(groupProject, repoFile, "master", "Updated contents"); gitLabApi.getRepositoryFileApi().updateFile(groupProject, repoFile, "master", "Updated contents");
System.out.format("Updated content of %s repository file%n", repoFile.getFilePath()); System.out.format("Updated content of %s repository file%n", repoFile.getFilePath());
gitLabApi.getGroupApi().transferProject(testGroup, groupProject);
System.out.format("Transfered %s project to %s group%n", TEST_GROUP_PROJECT_NAME, TEST_GROUP);
} else if (!gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, "README.md", "master").isPresent()) { } else if (!gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, "README.md", "master").isPresent()) {
// Create the README.md file since it does not exists // Create the README.md file since it does not exists
......
...@@ -82,12 +82,14 @@ public class TestProjectApi extends AbstractIntegrationTest { ...@@ -82,12 +82,14 @@ public class TestProjectApi extends AbstractIntegrationTest {
private static final String TEST_PROJECT_NAME_1 = "test-gitlab4j-create-project"; private static final String TEST_PROJECT_NAME_1 = "test-gitlab4j-create-project";
private static final String TEST_PROJECT_NAME_2 = "test-gitlab4j-create-project-2"; private static final String TEST_PROJECT_NAME_2 = "test-gitlab4j-create-project-2";
private static final String TEST_NAMESPACE_PROJECT_NAME = "test-gitlab4j-create-namespace-project";
private static final String TEST_PROJECT_NAME_UPDATE = "test-gitlab4j-create-project-update"; private static final String TEST_PROJECT_NAME_UPDATE = "test-gitlab4j-create-project-update";
private static final String TEST_XFER_PROJECT_NAME = "test-gitlab4j-xfer-project"; private static final String TEST_XFER_PROJECT_NAME = "test-gitlab4j-xfer-project";
private static final String TEST_VARIABLE_KEY_PREFIX = "TEST_VARIABLE_KEY_"; private static final String TEST_VARIABLE_KEY_PREFIX = "TEST_VARIABLE_KEY_";
private static GitLabApi gitLabApi; private static GitLabApi gitLabApi;
private static Project testProject; private static Project testProject;
private static User currentUser;
public TestProjectApi() { public TestProjectApi() {
super(); super();
...@@ -99,6 +101,7 @@ public class TestProjectApi extends AbstractIntegrationTest { ...@@ -99,6 +101,7 @@ public class TestProjectApi extends AbstractIntegrationTest {
// Must setup the connection to the GitLab test server // Must setup the connection to the GitLab test server
gitLabApi = baseTestSetup(); gitLabApi = baseTestSetup();
testProject = getTestProject(); testProject = getTestProject();
currentUser = getCurrentUser();
deleteAllTestProjects(); deleteAllTestProjects();
} }
...@@ -109,28 +112,29 @@ public class TestProjectApi extends AbstractIntegrationTest { ...@@ -109,28 +112,29 @@ public class TestProjectApi extends AbstractIntegrationTest {
} }
private static void deleteAllTestProjects() { private static void deleteAllTestProjects() {
if (gitLabApi == null) { if (gitLabApi == null) {
return; return;
} }
try { try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_1); gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_PROJECT_NAME_1));
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {} } catch (GitLabApiException ignore) {}
try { try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_2); gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_PROJECT_NAME_2));
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {} } catch (GitLabApiException ignore) {}
try { try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_UPDATE); gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_PROJECT_NAME_UPDATE));
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {} } catch (GitLabApiException ignore) {}
try { try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_XFER_PROJECT_NAME); gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_XFER_PROJECT_NAME));
gitLabApi.getProjectApi().deleteProject(project); } catch (GitLabApiException ignore) {}
try {
gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_NAMESPACE_PROJECT_NAME));
} catch (GitLabApiException ignore) {} } catch (GitLabApiException ignore) {}
if (TEST_GROUP != null && TEST_PROJECT_NAME != null) { if (TEST_GROUP != null && TEST_PROJECT_NAME != null) {
...@@ -153,15 +157,13 @@ public class TestProjectApi extends AbstractIntegrationTest { ...@@ -153,15 +157,13 @@ public class TestProjectApi extends AbstractIntegrationTest {
if (TEST_GROUP != null && TEST_GROUP_PROJECT != null) { if (TEST_GROUP != null && TEST_GROUP_PROJECT != null) {
try { try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_GROUP_PROJECT); gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_NAMESPACE, TEST_GROUP_PROJECT));
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {} } catch (GitLabApiException ignore) {}
} }
if (TEST_XFER_NAMESPACE != null) { if (TEST_XFER_NAMESPACE != null) {
try { try {
Project project = gitLabApi.getProjectApi().getProject(TEST_XFER_NAMESPACE, TEST_XFER_PROJECT_NAME); gitLabApi.getProjectApi().deleteProject(Project.getPathWithNammespace(TEST_XFER_NAMESPACE, TEST_XFER_PROJECT_NAME));
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {} } catch (GitLabApiException ignore) {}
} }
...@@ -536,6 +538,24 @@ public class TestProjectApi extends AbstractIntegrationTest { ...@@ -536,6 +538,24 @@ public class TestProjectApi extends AbstractIntegrationTest {
assertNotNull(projectLanguages); assertNotNull(projectLanguages);
} }
@Test
public void testCreateProjectInNamespace() throws GitLabApiException {
assumeNotNull(currentUser);
Project namespaceProject = null;
try {
namespaceProject = gitLabApi.getProjectApi().createProject(currentUser.getId(), TEST_NAMESPACE_PROJECT_NAME);
assertNotNull(namespaceProject);
} finally {
if (namespaceProject != null) {
try {
gitLabApi.getProjectApi().deleteProject(namespaceProject);
} catch (Exception ignore) {}
}
}
}
@Test @Test
public void testForkProject() throws GitLabApiException { public void testForkProject() throws GitLabApiException {
...@@ -544,8 +564,18 @@ public class TestProjectApi extends AbstractIntegrationTest { ...@@ -544,8 +564,18 @@ public class TestProjectApi extends AbstractIntegrationTest {
Project project = gitLabApi.getProjectApi().getProject(TEST_GROUP, TEST_GROUP_PROJECT); Project project = gitLabApi.getProjectApi().getProject(TEST_GROUP, TEST_GROUP_PROJECT);
assertNotNull(project); assertNotNull(project);
Project forkedProject = gitLabApi.getProjectApi().forkProject(project.getId(), TEST_NAMESPACE);
assertNotNull(forkedProject); Project forkedProject = null;
try {
forkedProject = gitLabApi.getProjectApi().forkProject(project.getId(), TEST_NAMESPACE);
assertNotNull(forkedProject);
} finally {
if (forkedProject != null) {
try {
gitLabApi.getProjectApi().deleteProject(forkedProject);
} catch (Exception ignore) {}
}
}
} }
@Test @Test
......
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