Plugin API: Add create project API

Change-Id: Ibc4de0747f10336eaa51a818672e01cfd55c4a16
This commit is contained in:
David Ostrovsky
2014-03-24 22:36:26 -07:00
committed by David Ostrovsky
parent a0d6c3ae3f
commit 8f3931bb0d
4 changed files with 92 additions and 4 deletions

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.acceptance.api.project;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.api.projects.ProjectInput;
import com.google.gerrit.extensions.restapi.RestApiException;
import org.eclipse.jgit.api.errors.GitAPIException;
@@ -27,6 +28,22 @@ import java.io.IOException;
@NoHttpd
public class ProjectIT extends AbstractDaemonTest {
@Test
public void createProjectFoo() throws RestApiException {
gApi.projects()
.name("foo")
.create();
}
@Test(expected = RestApiException.class)
public void createProjectFooBar() throws RestApiException {
ProjectInput in = new ProjectInput();
in.name = "bar";
gApi.projects()
.name("foo")
.create(in);
}
@Test
public void createBranch() throws GitAPIException,
IOException, RestApiException {

View File

@@ -14,6 +14,10 @@
package com.google.gerrit.extensions.api.projects;
import com.google.gerrit.extensions.restapi.RestApiException;
public interface ProjectApi {
ProjectApi create() throws RestApiException;
ProjectApi create(ProjectInput in) throws RestApiException;
BranchApi branch(String ref);
}

View File

@@ -14,28 +14,93 @@
package com.google.gerrit.server.api.projects;
import com.google.gerrit.common.errors.ProjectCreationFailedException;
import com.google.gerrit.extensions.api.projects.BranchApi;
import com.google.gerrit.extensions.api.projects.ProjectApi;
import com.google.gerrit.extensions.api.projects.ProjectInput;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.TopLevelResource;
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
import com.google.gerrit.server.project.CreateProject;
import com.google.gerrit.server.project.ProjectResource;
import com.google.inject.Inject;
import com.google.gerrit.server.project.ProjectsCollection;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import java.io.IOException;
public class ProjectApiImpl implements ProjectApi {
interface Factory {
ProjectApiImpl create(ProjectResource project);
ProjectApiImpl create(String name);
}
private final Provider<CreateProject.Factory> createProjectFactory;
private final ProjectApiImpl.Factory projectApi;
private final ProjectsCollection projects;
private final ProjectResource project;
private final String name;
private final BranchApiImpl.Factory branchApi;
@Inject
ProjectApiImpl(
@AssistedInject
ProjectApiImpl(Provider<CreateProject.Factory> createProjectFactory,
ProjectApiImpl.Factory projectApi,
ProjectsCollection projects,
BranchApiImpl.Factory branchApiFactory,
@Assisted ProjectResource project) {
this(createProjectFactory, projectApi, projects, branchApiFactory, project,
null);
}
@AssistedInject
ProjectApiImpl(Provider<CreateProject.Factory> createProjectFactory,
ProjectApiImpl.Factory projectApi,
ProjectsCollection projects,
BranchApiImpl.Factory branchApiFactory,
@Assisted String name) {
this(createProjectFactory, projectApi, projects, branchApiFactory, null,
name);
}
private ProjectApiImpl(Provider<CreateProject.Factory> createProjectFactory,
ProjectApiImpl.Factory projectApi,
ProjectsCollection projects,
BranchApiImpl.Factory branchApiFactory,
ProjectResource project,
String name) {
this.createProjectFactory = createProjectFactory;
this.projectApi = projectApi;
this.projects = projects;
this.project = project;
this.name = name;
this.branchApi = branchApiFactory;
}
@Override
public ProjectApi create() throws RestApiException {
return create(new ProjectInput());
}
@Override
public ProjectApi create(ProjectInput in) throws RestApiException {
try {
if (in.name != null && !name.equals(in.name)) {
throw new RestApiException("name must match input.name");
}
createProjectFactory.get().create(name)
.apply(TopLevelResource.INSTANCE, in);
return projectApi.create(projects.parse(name));
} catch (BadRequestException | UnprocessableEntityException
| ResourceConflictException | ResourceNotFoundException
| ProjectCreationFailedException | IOException e) {
throw new RestApiException("Cannot create project", e);
}
}
@Override
public BranchApi branch(String ref) {
return branchApi.create(project, ref);

View File

@@ -37,7 +37,9 @@ class ProjectsImpl implements Projects {
public ProjectApi name(String name) throws RestApiException {
try {
return api.create(projects.parse(name));
} catch (IOException | UnprocessableEntityException e) {
} catch (UnprocessableEntityException e) {
return api.create(name);
} catch (IOException e) {
throw new RestApiException("Cannot retrieve project");
}
}