Merge topic 'inline-3'

* changes:
  InlineEdit: Allow to list files in change edit
  InlineEdit: Add DELETE /changes/<id>/edit REST endpoint
  InlineEdit: Add POST /changes/<id>/rebase_edit REST endpoint
  InlineEdit: Add POST /changes/<id>/publish_edit REST endpoint
  InlineEdit: Add GET /changes/<id>/edit/path%2fto%2ffile REST endpoint
  InlineEdit: Add DELETE /changes/<id>/edit/path%2fto%2ffile REST endpoint
This commit is contained in:
Dave Borowitz
2014-08-18 20:13:38 +00:00
committed by Gerrit Code Review
11 changed files with 707 additions and 22 deletions

View File

@@ -21,6 +21,7 @@ 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.AcceptsDelete;
import com.google.gerrit.extensions.restapi.AcceptsPost;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
@@ -36,11 +37,13 @@ import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.edit.ChangeEdit;
import com.google.gerrit.server.edit.ChangeEditJson;
import com.google.gerrit.server.edit.ChangeEditModifier;
import com.google.gerrit.server.edit.ChangeEditUtil;
import com.google.gerrit.server.patch.PatchListNotAvailableException;
import com.google.gerrit.server.project.InvalidChangeOperationException;
import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gwtorm.server.OrmException;
@@ -49,30 +52,36 @@ import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.assistedinject.Assisted;
import org.kohsuke.args4j.Option;
import java.io.IOException;
@Singleton
public class ChangeEdits implements
ChildCollection<ChangeResource, ChangeEditResource>,
AcceptsCreate<ChangeResource>,
AcceptsPost<ChangeResource> {
AcceptsPost<ChangeResource>,
AcceptsDelete<ChangeResource> {
private final DynamicMap<RestView<ChangeEditResource>> views;
private final Create.Factory createFactory;
private final Detail detail;
private final DeleteEdit.Factory deleteEditFactory;
private final Provider<Detail> detail;
private final ChangeEditUtil editUtil;
private final Post post;
@Inject
ChangeEdits(DynamicMap<RestView<ChangeEditResource>> views,
Create.Factory createFactory,
Detail detail,
Provider<Detail> detail,
ChangeEditUtil editUtil,
Post post) {
Post post,
DeleteEdit.Factory deleteEditFactory) {
this.views = views;
this.createFactory = createFactory;
this.detail = detail;
this.editUtil = editUtil;
this.post = post;
this.deleteEditFactory = deleteEditFactory;
}
@Override
@@ -82,7 +91,7 @@ public class ChangeEdits implements
@Override
public RestView<ChangeResource> list() {
return detail;
return detail.get();
}
@Override
@@ -116,6 +125,14 @@ public class ChangeEdits implements
* change edit wasn't created yet. Change edit is created and PUT
* handler is called.
*/
@SuppressWarnings("unchecked")
@Override
public DeleteEdit delete(ChangeResource parent, IdString id)
throws RestApiException {
return deleteEditFactory.create(parent.getChange(),
id != null ? id.get() : null);
}
static class Create implements
RestModifyView<ChangeResource, Put.Input> {
@@ -172,27 +189,108 @@ public class ChangeEdits implements
}
}
@Singleton
static class DeleteEdit implements
RestModifyView<ChangeResource, DeleteEdit.Input> {
public static class Input {
}
interface Factory {
DeleteEdit create(Change change, String path);
}
private final ChangeEditUtil editUtil;
private final ChangeEditModifier editModifier;
private final Provider<ReviewDb> db;
private final String path;
@Inject
DeleteEdit(ChangeEditUtil editUtil,
ChangeEditModifier editModifier,
Provider<ReviewDb> db,
@Assisted @Nullable String path) {
this.editUtil = editUtil;
this.editModifier = editModifier;
this.db = db;
this.path = path;
}
@Override
public Response<?> apply(ChangeResource rsrc, DeleteEdit.Input in)
throws IOException, AuthException, ResourceConflictException,
OrmException, InvalidChangeOperationException, BadRequestException {
Optional<ChangeEdit> edit = editUtil.byChange(rsrc.getChange());
if (edit.isPresent() && path == null) {
// Edit is wiped out
editUtil.delete(edit.get());
} else if (!edit.isPresent() && path != null) {
// Edit is created on top of current patch set by deleting path.
// Even if the latest patch set changed since the user triggered
// the operation, deleting the whole file is probably still what
// they intended.
editModifier.createEdit(rsrc.getChange(), db.get().patchSets().get(
rsrc.getChange().currentPatchSetId()));
edit = editUtil.byChange(rsrc.getChange());
editModifier.deleteFile(edit.get(), path);
} else {
// Bad request
throw new BadRequestException(
"change edit doesn't exist and no path was provided");
}
return Response.none();
}
}
static class Detail implements RestReadView<ChangeResource> {
private final ChangeEditUtil editUtil;
private final ChangeEditJson editJson;
private final FileInfoJson fileInfoJson;
private final Revisions revisions;
@Option(name = "--base", metaVar = "revision-id")
String base;
@Option(name = "--list", metaVar = "LIST")
boolean list;
@Inject
Detail(ChangeEditJson editJson,
ChangeEditUtil editUtil) {
Detail(ChangeEditUtil editUtil,
ChangeEditJson editJson,
FileInfoJson fileInfoJson,
Revisions revisions) {
this.editJson = editJson;
this.editUtil = editUtil;
this.fileInfoJson = fileInfoJson;
this.revisions = revisions;
}
@Override
public Response<EditInfo> apply(ChangeResource rsrc) throws AuthException,
IOException, NoSuchChangeException, InvalidChangeOperationException,
ResourceNotFoundException {
IOException, InvalidChangeOperationException,
ResourceNotFoundException, OrmException {
Optional<ChangeEdit> edit = editUtil.byChange(rsrc.getChange());
if (edit.isPresent()) {
return Response.ok(editJson.toEditInfo(edit.get()));
if (!edit.isPresent()) {
return Response.none();
}
return Response.none();
EditInfo editInfo = editJson.toEditInfo(edit.get());
if (list) {
PatchSet basePatchSet = null;
if (base != null) {
RevisionResource baseResource = revisions.parse(
rsrc, IdString.fromDecoded(base));
basePatchSet = baseResource.getPatchSet();
}
try {
editInfo.files =
fileInfoJson.toFileInfoMap(
rsrc.getChange(),
edit.get().getRevision(),
basePatchSet);
} catch (PatchListNotAvailableException e) {
throw new ResourceNotFoundException(e.getMessage());
}
}
return Response.ok(editInfo);
}
}
@@ -280,4 +378,58 @@ public class ChangeEdits implements
return Response.none();
}
}
/**
* Handler to delete a file.
* <p>
* This deletes the file from the repository completely. This is not the same
* as reverting or restoring a file to its previous contents.
*/
@Singleton
static class DeleteContent implements
RestModifyView<ChangeEditResource, DeleteContent.Input> {
public static class Input {
}
private final ChangeEditModifier editModifier;
@Inject
DeleteContent(ChangeEditModifier editModifier) {
this.editModifier = editModifier;
}
@Override
public Response<?> apply(ChangeEditResource rsrc, DeleteContent.Input input)
throws AuthException, ResourceConflictException {
try {
editModifier.deleteFile(rsrc.getChangeEdit(), rsrc.getPath());
} catch(InvalidChangeOperationException | IOException e) {
throw new ResourceConflictException(e.getMessage());
}
return Response.none();
}
}
@Singleton
static class Get implements RestReadView<ChangeEditResource> {
private final FileContentUtil fileContentUtil;
@Inject
Get(FileContentUtil fileContentUtil) {
this.fileContentUtil = fileContentUtil;
}
@Override
public Response<?> apply(ChangeEditResource rsrc)
throws ResourceNotFoundException, IOException {
try {
return Response.ok(fileContentUtil.getContent(
rsrc.getChangeEdit().getChange().getProject(),
rsrc.getChangeEdit().getRevision().get(),
rsrc.getPath()));
} catch (ResourceNotFoundException rnfe) {
return Response.none();
}
}
}
}

View File

@@ -21,6 +21,7 @@ import com.google.gerrit.reviewdb.client.AccountDiffPreference.Whitespace;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Patch;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.RevId;
import com.google.gerrit.server.patch.PatchList;
import com.google.gerrit.server.patch.PatchListCache;
import com.google.gerrit.server.patch.PatchListEntry;
@@ -44,15 +45,15 @@ public class FileInfoJson {
Map<String, FileInfo> toFileInfoMap(Change change, PatchSet patchSet)
throws PatchListNotAvailableException {
return toFileInfoMap(change, patchSet, null);
return toFileInfoMap(change, patchSet.getRevision(), null);
}
Map<String, FileInfo> toFileInfoMap(Change change, PatchSet patchSet, @Nullable PatchSet base)
Map<String, FileInfo> toFileInfoMap(Change change, RevId revision, @Nullable PatchSet base)
throws PatchListNotAvailableException {
ObjectId a = (base == null)
? null
: ObjectId.fromString(base.getRevision().get());
ObjectId b = ObjectId.fromString(patchSet.getRevision().get());
ObjectId b = ObjectId.fromString(revision.get());
PatchList list = patchListCache.get(
new PatchListKey(change.getProject(), a, b, Whitespace.IGNORE_NONE));

View File

@@ -141,7 +141,7 @@ public class Files implements ChildCollection<RevisionResource, FileResource> {
try {
Response<Map<String, FileInfo>> r = Response.ok(fileInfoJson.toFileInfoMap(
resource.getChange(),
resource.getPatchSet(),
resource.getPatchSet().getRevision(),
basePatchSet));
if (resource.isCacheable()) {
r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));

View File

@@ -102,7 +102,11 @@ public class Module extends RestApiModule {
get(FILE_KIND, "diff").to(GetDiff.class);
child(CHANGE_KIND, "edit").to(ChangeEdits.class);
child(CHANGE_KIND, "publish_edit").to(PublishChangeEdit.class);
child(CHANGE_KIND, "rebase_edit").to(RebaseChangeEdit.class);
put(CHANGE_EDIT_KIND, "/").to(ChangeEdits.Put.class);
delete(CHANGE_EDIT_KIND).to(ChangeEdits.DeleteContent.class);
get(CHANGE_EDIT_KIND, "/").to(ChangeEdits.Get.class);
install(new FactoryModule() {
@Override
@@ -113,6 +117,7 @@ public class Module extends RestApiModule {
factory(ChangeInserter.Factory.class);
factory(PatchSetInserter.Factory.class);
factory(ChangeEdits.Create.Factory.class);
factory(ChangeEdits.DeleteEdit.Factory.class);
}
});
}

View File

@@ -0,0 +1,99 @@
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.change;
import com.google.common.base.Optional;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.AcceptsPost;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ChildCollection;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.server.edit.ChangeEdit;
import com.google.gerrit.server.edit.ChangeEditUtil;
import com.google.gerrit.server.project.InvalidChangeOperationException;
import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
@Singleton
public class PublishChangeEdit implements
ChildCollection<ChangeResource, ChangeEditResource>,
AcceptsPost<ChangeResource> {
private final Publish publish;
@Inject
PublishChangeEdit(Publish publish) {
this.publish = publish;
}
@Override
public DynamicMap<RestView<ChangeEditResource>> views() {
throw new IllegalStateException("not yet implemented");
}
@Override
public RestView<ChangeResource> list() {
throw new IllegalStateException("not yet implemented");
}
@Override
public ChangeEditResource parse(ChangeResource parent, IdString id)
throws ResourceNotFoundException, Exception {
throw new IllegalStateException("not yet implemented");
}
@SuppressWarnings("unchecked")
@Override
public Publish post(ChangeResource parent) throws RestApiException {
return publish;
}
@Singleton
public static class Publish implements RestModifyView<ChangeResource, Publish.Input> {
public static class Input {
}
private final ChangeEditUtil editUtil;
@Inject
Publish(ChangeEditUtil editUtil) {
this.editUtil = editUtil;
}
@Override
public Response<?> apply(ChangeResource rsrc, Publish.Input in)
throws AuthException, ResourceConflictException, NoSuchChangeException,
IOException, InvalidChangeOperationException, OrmException {
Optional<ChangeEdit> edit = editUtil.byChange(rsrc.getChange());
if (!edit.isPresent()) {
throw new ResourceConflictException(String.format(
"no edit exists for change %s",
rsrc.getChange().getChangeId()));
}
editUtil.publish(edit.get());
return Response.none();
}
}
}

View File

@@ -0,0 +1,117 @@
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.change;
import com.google.common.base.Optional;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.AcceptsPost;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ChildCollection;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.edit.ChangeEdit;
import com.google.gerrit.server.edit.ChangeEditModifier;
import com.google.gerrit.server.edit.ChangeEditUtil;
import com.google.gerrit.server.project.InvalidChangeOperationException;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
@Singleton
public class RebaseChangeEdit implements
ChildCollection<ChangeResource, ChangeEditResource>,
AcceptsPost<ChangeResource> {
private final Rebase rebase;
@Inject
RebaseChangeEdit(Rebase rebase) {
this.rebase = rebase;
}
@Override
public DynamicMap<RestView<ChangeEditResource>> views() {
throw new IllegalStateException("not yet implemented");
}
@Override
public RestView<ChangeResource> list() {
throw new IllegalStateException("not yet implemented");
}
@Override
public ChangeEditResource parse(ChangeResource parent, IdString id)
throws ResourceNotFoundException, Exception {
throw new IllegalStateException("not yet implemented");
}
@SuppressWarnings("unchecked")
@Override
public Rebase post(ChangeResource parent) throws RestApiException {
return rebase;
}
@Singleton
public static class Rebase implements RestModifyView<ChangeResource, Publish.Input> {
public static class Input {
}
private final ChangeEditModifier editModifier;
private final ChangeEditUtil editUtil;
private final Provider<ReviewDb> db;
@Inject
Rebase(ChangeEditModifier editModifier,
ChangeEditUtil editUtil,
Provider<ReviewDb> db) {
this.editModifier = editModifier;
this.editUtil = editUtil;
this.db = db;
}
@Override
public Response<?> apply(ChangeResource rsrc, Publish.Input in)
throws AuthException, ResourceConflictException, IOException,
InvalidChangeOperationException, OrmException {
Optional<ChangeEdit> edit = editUtil.byChange(rsrc.getChange());
if (!edit.isPresent()) {
throw new ResourceConflictException(String.format(
"no edit exists for change %s",
rsrc.getChange().getChangeId()));
}
PatchSet current = db.get().patchSets().get(
rsrc.getChange().currentPatchSetId());
if (current.getId().equals(edit.get().getBasePatchSet().getId())) {
throw new ResourceConflictException(String.format(
"edit for change %s is already on latest patch set: %s",
rsrc.getChange().getChangeId(),
current.getId()));
}
editModifier.rebaseEdit(edit.get(), current);
return Response.none();
}
}
}