InlineEdit: Add POST /changes/<id>/edit REST endpoint

Add post REST endpoint to create new change edit without content or
restore deleted file from change edit. When change doesn't exist yet
for this change it is created.

Change-Id: I7ff308b1e875b5591bf8492273048ae61cba3cff
This commit is contained in:
David Ostrovsky
2014-08-15 21:31:43 +02:00
parent a5ab829a4c
commit 138edb474a
3 changed files with 137 additions and 2 deletions

View File

@@ -21,7 +21,9 @@ import com.google.gerrit.common.Nullable;
import com.google.gerrit.extensions.common.EditInfo;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.AcceptsCreate;
import com.google.gerrit.extensions.restapi.AcceptsPost;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ChildCollection;
import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.IdString;
@@ -52,21 +54,25 @@ import java.io.IOException;
@Singleton
public class ChangeEdits implements
ChildCollection<ChangeResource, ChangeEditResource>,
AcceptsCreate<ChangeResource> {
AcceptsCreate<ChangeResource>,
AcceptsPost<ChangeResource> {
private final DynamicMap<RestView<ChangeEditResource>> views;
private final Create.Factory createFactory;
private final Detail detail;
private final ChangeEditUtil editUtil;
private final Post post;
@Inject
ChangeEdits(DynamicMap<RestView<ChangeEditResource>> views,
Create.Factory createFactory,
Detail detail,
ChangeEditUtil editUtil) {
ChangeEditUtil editUtil,
Post post) {
this.views = views;
this.createFactory = createFactory;
this.detail = detail;
this.editUtil = editUtil;
this.post = post;
}
@Override
@@ -97,6 +103,13 @@ public class ChangeEdits implements
return createFactory.create(parent.getChange(), id.get());
}
@SuppressWarnings("unchecked")
@Override
public Post post(ChangeResource parent) throws RestApiException {
return post;
}
/**
* Create handler that is activated when collection element is accessed
* but doesn't exist, e. g. PUT request with a path was called but
@@ -183,6 +196,59 @@ public class ChangeEdits implements
}
}
/**
* Post to edit collection resource. Two different operations are
* supported:
* <ul>
* <li>Create non existing change edit</li>
* <li>Restore path in existing change edit</li>
* </ul>
* The combination of two operations in one request is supported.
*/
@Singleton
public static class Post implements
RestModifyView<ChangeResource, Post.Input> {
public static class Input {
public String restorePath;
}
private final Provider<ReviewDb> db;
private final ChangeEditUtil editUtil;
private final ChangeEditModifier editModifier;
@Inject
Post(Provider<ReviewDb> db,
ChangeEditUtil editUtil,
ChangeEditModifier editModifier) {
this.db = db;
this.editUtil = editUtil;
this.editModifier = editModifier;
}
@Override
public Response<?> apply(ChangeResource resource, Post.Input input)
throws AuthException, InvalidChangeOperationException, IOException,
ResourceConflictException, OrmException {
Optional<ChangeEdit> edit = editUtil.byChange(resource.getChange());
if (!edit.isPresent()) {
edit = createEdit(resource.getChange());
}
if (input != null && !Strings.isNullOrEmpty(input.restorePath)) {
editModifier.restoreFile(edit.get(), input.restorePath);
}
return Response.none();
}
private Optional<ChangeEdit> createEdit(Change change)
throws AuthException, IOException, ResourceConflictException,
OrmException, InvalidChangeOperationException {
editModifier.createEdit(change,
db.get().patchSets().get(change.currentPatchSetId()));
return editUtil.byChange(change);
}
}
/**
* Put handler that is activated when PUT request is called on
* collection element.