InlineEdit: Report same commit message error to client

Change-Id: Iabacdf49351f0430629b7ab4de53a2c1e7b9f9c8
This commit is contained in:
David Ostrovsky
2014-11-08 15:38:04 +01:00
committed by David Pursehouse
parent 3fedc09a5d
commit ab09cc7fc1
4 changed files with 42 additions and 6 deletions

View File

@@ -45,6 +45,7 @@ import com.google.gerrit.server.change.FileContentUtil;
import com.google.gerrit.server.edit.ChangeEdit; import com.google.gerrit.server.edit.ChangeEdit;
import com.google.gerrit.server.edit.ChangeEditModifier; import com.google.gerrit.server.edit.ChangeEditModifier;
import com.google.gerrit.server.edit.ChangeEditUtil; import com.google.gerrit.server.edit.ChangeEditUtil;
import com.google.gerrit.server.edit.UnchangedCommitMessageException;
import com.google.gerrit.server.project.InvalidChangeOperationException; import com.google.gerrit.server.project.InvalidChangeOperationException;
import com.google.gwtorm.server.SchemaFactory; import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject; import com.google.inject.Inject;
@@ -247,8 +248,8 @@ public class ChangeEditIT extends AbstractDaemonTest {
modifier.modifyMessage( modifier.modifyMessage(
edit.get(), edit.get(),
edit.get().getEditCommit().getFullMessage()); edit.get().getEditCommit().getFullMessage());
fail("InvalidChangeOperationException expected"); fail("UnchangedCommitMessageException expected");
} catch (InvalidChangeOperationException ex) { } catch (UnchangedCommitMessageException ex) {
assertThat(ex.getMessage()).isEqualTo( assertThat(ex.getMessage()).isEqualTo(
"New commit message cannot be same as existing commit message"); "New commit message cannot be same as existing commit message");
} }

View File

@@ -44,6 +44,7 @@ import com.google.gerrit.server.edit.ChangeEdit;
import com.google.gerrit.server.edit.ChangeEditJson; import com.google.gerrit.server.edit.ChangeEditJson;
import com.google.gerrit.server.edit.ChangeEditModifier; import com.google.gerrit.server.edit.ChangeEditModifier;
import com.google.gerrit.server.edit.ChangeEditUtil; import com.google.gerrit.server.edit.ChangeEditUtil;
import com.google.gerrit.server.edit.UnchangedCommitMessageException;
import com.google.gerrit.server.patch.PatchListNotAvailableException; import com.google.gerrit.server.patch.PatchListNotAvailableException;
import com.google.gerrit.server.project.InvalidChangeOperationException; import com.google.gerrit.server.project.InvalidChangeOperationException;
import com.google.gerrit.server.project.NoSuchChangeException; import com.google.gerrit.server.project.NoSuchChangeException;
@@ -372,6 +373,11 @@ public class ChangeEdits implements
@Override @Override
public Response<?> apply(ChangeEditResource rsrc, Input input) public Response<?> apply(ChangeEditResource rsrc, Input input)
throws AuthException, ResourceConflictException, IOException { throws AuthException, ResourceConflictException, IOException {
String path = rsrc.getPath();
if (Strings.isNullOrEmpty(path) || path.charAt(0) == '/') {
throw new ResourceConflictException("Invalid path: " + path);
}
try { try {
editModifier.modifyFile( editModifier.modifyFile(
rsrc.getChangeEdit(), rsrc.getChangeEdit(),
@@ -474,7 +480,12 @@ public class ChangeEdits implements
throw new BadRequestException("commit message must be provided"); throw new BadRequestException("commit message must be provided");
} }
editModifier.modifyMessage(edit.get(), input.message); try {
editModifier.modifyMessage(edit.get(), input.message);
} catch (UnchangedCommitMessageException ucm) {
throw new ResourceConflictException(ucm.getMessage());
}
return Response.none(); return Response.none();
} }
} }

View File

@@ -227,9 +227,11 @@ public class ChangeEditModifier {
* @throws AuthException * @throws AuthException
* @throws InvalidChangeOperationException * @throws InvalidChangeOperationException
* @throws IOException * @throws IOException
* @throws UnchangedCommitMessageException
*/ */
public RefUpdate.Result modifyMessage(ChangeEdit edit, String msg) public RefUpdate.Result modifyMessage(ChangeEdit edit, String msg)
throws AuthException, InvalidChangeOperationException, IOException { throws AuthException, InvalidChangeOperationException, IOException,
UnchangedCommitMessageException {
checkState(!Strings.isNullOrEmpty(msg), "message cannot be null"); checkState(!Strings.isNullOrEmpty(msg), "message cannot be null");
if (!currentUser.get().isIdentifiedUser()) { if (!currentUser.get().isIdentifiedUser()) {
throw new AuthException("Authentication required"); throw new AuthException("Authentication required");
@@ -241,8 +243,7 @@ public class ChangeEditModifier {
} }
if (prevEdit.getFullMessage().equals(msg)) { if (prevEdit.getFullMessage().equals(msg)) {
throw new InvalidChangeOperationException( throw new UnchangedCommitMessageException();
"New commit message cannot be same as existing commit message");
} }
IdentifiedUser me = (IdentifiedUser) currentUser.get(); IdentifiedUser me = (IdentifiedUser) currentUser.get();

View File

@@ -0,0 +1,23 @@
// 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.edit;
public class UnchangedCommitMessageException extends Exception {
private static final long serialVersionUID = 1L;
public UnchangedCommitMessageException() {
super("New commit message cannot be same as existing commit message");
}
}