From bcee7ff75a237f5bb9e16156f807853ad3ece6ef Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Mon, 22 Jun 2015 16:25:37 +0900 Subject: [PATCH] Handle commit validation errors when publishing change edit When a commit validator throws a CommitValidationException during publish of a new patch set from the inline editor, it is translated to an InvalidChangeOperationException which results in an internal server error message being displayed to the user. Instead, catch the InvalidChangeOperationException and throw a ResourceConflictException, which results in the actual error message being displayed to the user. The corresponding change in the cookbook plugin includes an example of commit validation to reproduce this case. Bug: Issue 3442 Change-Id: I95b88dc6f2bc76df088ebef50a52974b33cab5ad --- .../gerrit/server/change/PublishChangeEdit.java | 3 +-- .../gerrit/server/edit/ChangeEditUtil.java | 16 +++++++++------- plugins/cookbook-plugin | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/change/PublishChangeEdit.java b/gerrit-server/src/main/java/com/google/gerrit/server/change/PublishChangeEdit.java index 711cf1829e..b88931ed20 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/change/PublishChangeEdit.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/change/PublishChangeEdit.java @@ -29,7 +29,6 @@ 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; @@ -85,7 +84,7 @@ public class PublishChangeEdit implements @Override public Response apply(ChangeResource rsrc, Publish.Input in) throws AuthException, ResourceConflictException, NoSuchChangeException, - IOException, InvalidChangeOperationException, OrmException { + IOException, OrmException { Capable r = rsrc.getControl().getProjectControl().canPushToAtLeastOneRef(); if (r != Capable.OK) { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/edit/ChangeEditUtil.java b/gerrit-server/src/main/java/com/google/gerrit/server/edit/ChangeEditUtil.java index 12a89893f5..29bda1dcf0 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/edit/ChangeEditUtil.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/edit/ChangeEditUtil.java @@ -134,13 +134,11 @@ public class ChangeEditUtil { * @param edit change edit to publish * @throws NoSuchChangeException * @throws IOException - * @throws InvalidChangeOperationException * @throws OrmException * @throws ResourceConflictException */ public void publish(ChangeEdit edit) throws NoSuchChangeException, - IOException, InvalidChangeOperationException, - OrmException, ResourceConflictException { + IOException, OrmException, ResourceConflictException { Change change = edit.getChange(); try (Repository repo = gitManager.openRepository(change.getProject()); RevWalk rw = new RevWalk(repo); @@ -151,10 +149,14 @@ public class ChangeEditUtil { "only edit for current patch set can be published"); } - insertPatchSet(edit, change, repo, rw, basePatchSet, - squashEdit(rw, inserter, edit.getEditCommit(), basePatchSet)); - // TODO(davido): This should happen in the same BatchRefUpdate. - deleteRef(repo, edit); + try { + insertPatchSet(edit, change, repo, rw, basePatchSet, + squashEdit(rw, inserter, edit.getEditCommit(), basePatchSet)); + // TODO(davido): This should happen in the same BatchRefUpdate. + deleteRef(repo, edit); + } catch (InvalidChangeOperationException e) { + throw new ResourceConflictException(e.getMessage()); + } } } diff --git a/plugins/cookbook-plugin b/plugins/cookbook-plugin index 955332734b..abba494014 160000 --- a/plugins/cookbook-plugin +++ b/plugins/cookbook-plugin @@ -1 +1 @@ -Subproject commit 955332734bd554527064f8af32907ee1c1561d2c +Subproject commit abba4940141178bbc7c6ad38690262fd93533ded