Projects: Add convenience methods for creating projects

The old idiom:
  api.projects().name("foo").create(in);
more closely matches the REST API, but makes less sense in Java where
we don't necessarily require a hierarchical structure, and we can
infer the project name from the input.

The above becomes the simpler:
  api.projects().create(in);

Also include a method to create with just a name.

Change-Id: Ib789854bd22e1a15ceebf37c57382ca4079c4459
This commit is contained in:
Dave Borowitz
2015-03-12 09:51:51 -07:00
parent 3525b2dcc3
commit 5c8c20b28f
6 changed files with 49 additions and 12 deletions

View File

@@ -38,8 +38,7 @@ public class ProjectIT extends AbstractDaemonTest {
String name = "foo"; String name = "foo";
assertThat(name).isEqualTo( assertThat(name).isEqualTo(
gApi.projects() gApi.projects()
.name(name) .create(name)
.create()
.get() .get()
.name); .name);
} }
@@ -49,8 +48,7 @@ public class ProjectIT extends AbstractDaemonTest {
String name = "foo"; String name = "foo";
assertThat(name).isEqualTo( assertThat(name).isEqualTo(
gApi.projects() gApi.projects()
.name(name + ".git") .create(name + ".git")
.create()
.get() .get()
.name); .name);
} }
@@ -69,10 +67,8 @@ public class ProjectIT extends AbstractDaemonTest {
ProjectInput in = new ProjectInput(); ProjectInput in = new ProjectInput();
in.name = "baz"; in.name = "baz";
gApi.projects() gApi.projects()
.name("baz")
.create(in); .create(in);
gApi.projects() gApi.projects()
.name("baz")
.create(in); .create(in);
} }
@@ -89,8 +85,8 @@ public class ProjectIT extends AbstractDaemonTest {
public void listProjects() throws Exception { public void listProjects() throws Exception {
List<ProjectInfo> initialProjects = gApi.projects().list().get(); List<ProjectInfo> initialProjects = gApi.projects().list().get();
gApi.projects().name("foo").create(); gApi.projects().create("foo");
gApi.projects().name("bar").create(); gApi.projects().create("bar");
List<ProjectInfo> allProjects = gApi.projects().list().get(); List<ProjectInfo> allProjects = gApi.projects().list().get();
assertThat(allProjects).hasSize(initialProjects.size() + 2); assertThat(allProjects).hasSize(initialProjects.size() + 2);

View File

@@ -49,7 +49,7 @@ public class CreateProjectIT extends AbstractDaemonTest {
@Test @Test
public void testCreateProjectApi() throws Exception { public void testCreateProjectApi() throws Exception {
final String newProjectName = "newProject"; final String newProjectName = "newProject";
ProjectInfo p = gApi.projects().name(newProjectName).create().get(); ProjectInfo p = gApi.projects().create(newProjectName).get();
assertThat(p.name).isEqualTo(newProjectName); assertThat(p.name).isEqualTo(newProjectName);
ProjectState projectState = projectCache.get(new Project.NameKey(newProjectName)); ProjectState projectState = projectCache.get(new Project.NameKey(newProjectName));
assertThat(projectState).isNotNull(); assertThat(projectState).isNotNull();
@@ -60,7 +60,7 @@ public class CreateProjectIT extends AbstractDaemonTest {
@Test @Test
public void testCreateProjectApiWithGitSuffix() throws Exception { public void testCreateProjectApiWithGitSuffix() throws Exception {
final String newProjectName = "newProject"; final String newProjectName = "newProject";
ProjectInfo p = gApi.projects().name(newProjectName + ".git").create().get(); ProjectInfo p = gApi.projects().create(newProjectName + ".git").get();
assertThat(p.name).isEqualTo(newProjectName); assertThat(p.name).isEqualTo(newProjectName);
ProjectState projectState = projectCache.get(new Project.NameKey(newProjectName)); ProjectState projectState = projectCache.get(new Project.NameKey(newProjectName));
assertThat(projectState).isNotNull(); assertThat(projectState).isNotNull();

View File

@@ -81,7 +81,7 @@ public class ListProjectsIT extends AbstractDaemonTest {
ProjectInput projectInput = new ProjectInput(); ProjectInput projectInput = new ProjectInput();
projectInput.name = "some-project"; projectInput.name = "some-project";
projectInput.description = "Description of some-project"; projectInput.description = "Description of some-project";
gApi.projects().name(projectInput.name).create(projectInput); gApi.projects().create(projectInput);
// description not be included in the results by default. // description not be included in the results by default.
Map<String, ProjectInfo> result = gApi.projects().list().getAsMap(); Map<String, ProjectInfo> result = gApi.projects().list().getAsMap();

View File

@@ -40,6 +40,24 @@ public interface Projects {
*/ */
ProjectApi name(String name) throws RestApiException; ProjectApi name(String name) throws RestApiException;
/**
* Create a project using the default configuration.
*
* @param name project name.
* @return API for accessing the newly-created project.
* @throws RestApiException if an error occurred.
*/
ProjectApi create(String name) throws RestApiException;
/**
* Create a project.
*
* @param in project creation input; name must be set.
* @return API for accessing the newly-created project.
* @throws RestApiException if an error occurred.
*/
ProjectApi create(ProjectInput in) throws RestApiException;
ListRequest list(); ListRequest list();
public abstract class ListRequest { public abstract class ListRequest {
@@ -163,6 +181,16 @@ public interface Projects {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override
public ProjectApi create(ProjectInput in) throws RestApiException {
throw new NotImplementedException();
}
@Override
public ProjectApi create(String name) throws RestApiException {
throw new NotImplementedException();
}
@Override @Override
public ListRequest list() { public ListRequest list() {
throw new NotImplementedException(); throw new NotImplementedException();

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.server.api.projects; package com.google.gerrit.server.api.projects;
import com.google.gerrit.extensions.api.projects.ProjectApi; import com.google.gerrit.extensions.api.projects.ProjectApi;
import com.google.gerrit.extensions.api.projects.ProjectInput;
import com.google.gerrit.extensions.api.projects.Projects; import com.google.gerrit.extensions.api.projects.Projects;
import com.google.gerrit.extensions.common.ProjectInfo; import com.google.gerrit.extensions.common.ProjectInfo;
import com.google.gerrit.extensions.restapi.BadRequestException; import com.google.gerrit.extensions.restapi.BadRequestException;
@@ -56,6 +57,18 @@ class ProjectsImpl implements Projects {
} }
} }
@Override
public ProjectApi create(String name) throws RestApiException {
ProjectInput in = new ProjectInput();
in.name = name;
return create(in);
}
@Override
public ProjectApi create(ProjectInput in) throws RestApiException {
return name(in.name).create(in);
}
@Override @Override
public ListRequest list() { public ListRequest list() {
return new ListRequest() { return new ListRequest() {

View File

@@ -181,7 +181,7 @@ final class CreateProjectCommand extends SshCommand {
input.pluginConfigValues = parsePluginConfigValues(pluginConfigValues); input.pluginConfigValues = parsePluginConfigValues(pluginConfigValues);
} }
gApi.projects().name(projectName).create(input); gApi.projects().create(input);
} else { } else {
List<Project.NameKey> parentCandidates = List<Project.NameKey> parentCandidates =
suggestParentCandidates.getNameKeys(); suggestParentCandidates.getNameKeys();