Plugin API: Add create change method

Change-Id: I34c3302b5dab3fd705fe8d0cc5b0f4c71c0fd4d6
This commit is contained in:
David Ostrovsky
2014-05-08 20:23:21 +02:00
committed by David Ostrovsky
parent 76bab29eb4
commit 2f08ed4c47
3 changed files with 45 additions and 1 deletions

View File

@@ -27,6 +27,7 @@ import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestApiException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
import org.junit.Test;
import java.io.IOException;
@@ -110,4 +111,19 @@ public class ChangeIT extends AbstractDaemonTest {
.id("p~master~" + r.getChangeId())
.addReviewer(in);
}
@Test
public void createEmptyChange() throws RestApiException {
ChangeInfo in = new ChangeInfo();
in.branch = Constants.MASTER;
in.subject = "Create a change from the API";
in.project = project.get();
ChangeInfo info = gApi
.changes()
.create(in)
.get();
assertEquals(in.project, info.project);
assertEquals(in.branch, info.branch);
assertEquals(in.subject, info.subject);
}
}

View File

@@ -28,6 +28,7 @@ public interface Changes {
ChangeApi id(String triplet) throws RestApiException;
ChangeApi id(String project, String branch, String id)
throws RestApiException;
ChangeApi create(ChangeInfo in) throws RestApiException;
/**
* Shorthand for {@link #query(QueryParameter)} without any conditions (i.e. lists all changes).
@@ -113,6 +114,11 @@ public interface Changes {
throw new NotImplementedException();
}
@Override
public ChangeApi create(ChangeInfo in) throws RestApiException {
throw new NotImplementedException();
}
@Override
public List<ChangeInfo> query() throws RestApiException {
throw new NotImplementedException();

View File

@@ -18,22 +18,32 @@ import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.extensions.api.changes.ChangeApi;
import com.google.gerrit.extensions.api.changes.Changes;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.TopLevelResource;
import com.google.gerrit.extensions.restapi.Url;
import com.google.gerrit.server.change.ChangeJson;
import com.google.gerrit.server.change.ChangesCollection;
import com.google.gerrit.server.change.CreateChange;
import com.google.gerrit.server.project.InvalidChangeOperationException;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import java.io.IOException;
class ChangesImpl extends Changes.NotImplemented implements Changes {
private final ChangesCollection changes;
private final ChangeApiImpl.Factory api;
private final CreateChange.Factory createChangeFactory;
@Inject
ChangesImpl(ChangesCollection changes, ChangeApiImpl.Factory api) {
ChangesImpl(ChangesCollection changes,
ChangeApiImpl.Factory api,
CreateChange.Factory createChangeFactory) {
this.changes = changes;
this.api = api;
this.createChangeFactory = createChangeFactory;
}
@Override
@@ -60,4 +70,16 @@ class ChangesImpl extends Changes.NotImplemented implements Changes {
throw new RestApiException("Cannot parse change", e);
}
}
@Override
public ChangeApi create(ChangeInfo in) throws RestApiException {
try {
ChangeJson.ChangeInfo out = createChangeFactory.create().apply(
TopLevelResource.INSTANCE, in).value();
return api.create(changes.parse(TopLevelResource.INSTANCE,
IdString.fromUrl(out.changeId)));
} catch (OrmException | IOException | InvalidChangeOperationException e) {
throw new RestApiException("Cannot create change", e);
}
}
}