Merge "GetDiff: Reject edit as base as ‘400 Bad Request’"

This commit is contained in:
Edwin Kempin
2020-01-08 07:11:26 +00:00
committed by Gerrit Code Review
2 changed files with 25 additions and 2 deletions

View File

@@ -29,6 +29,7 @@ import com.google.gerrit.extensions.common.DiffInfo;
import com.google.gerrit.extensions.common.DiffWebLinkInfo;
import com.google.gerrit.extensions.common.WebLinkInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.CacheControl;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
@@ -102,8 +103,8 @@ public class GetDiff implements RestReadView<FileResource> {
@Override
public Response<DiffInfo> apply(FileResource resource)
throws ResourceConflictException, ResourceNotFoundException, AuthException,
InvalidChangeOperationException, IOException, PermissionBackendException {
throws BadRequestException, ResourceConflictException, ResourceNotFoundException,
AuthException, InvalidChangeOperationException, IOException, PermissionBackendException {
DiffPreferencesInfo prefs = new DiffPreferencesInfo();
if (whitespace != null) {
prefs.ignoreWhitespace = whitespace;
@@ -130,6 +131,9 @@ public class GetDiff implements RestReadView<FileResource> {
RevisionResource baseResource =
revisions.parse(resource.getRevision().getChangeResource(), IdString.fromDecoded(base));
basePatchSet = baseResource.getPatchSet();
if (basePatchSet.id().get() == 0) {
throw new BadRequestException("edit not allowed as base");
}
psf = patchScriptFactoryFactory.create(notes, fileName, basePatchSet.id(), pId, prefs);
} else if (parentNum > 0) {
psf = patchScriptFactoryFactory.create(notes, fileName, parentNum - 1, pId, prefs);

View File

@@ -21,6 +21,7 @@ import static com.google.gerrit.entities.Patch.MERGE_LIST;
import static com.google.gerrit.extensions.common.testing.DiffInfoSubject.assertThat;
import static com.google.gerrit.extensions.common.testing.FileInfoSubject.assertThat;
import static com.google.gerrit.git.ObjectIds.abbreviateName;
import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toMap;
@@ -41,6 +42,7 @@ import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import com.google.gerrit.extensions.common.ChangeType;
import com.google.gerrit.extensions.common.DiffInfo;
import com.google.gerrit.extensions.common.FileInfo;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.testing.ConfigSuite;
@@ -2718,6 +2720,23 @@ public class RevisionDiffIT extends AbstractDaemonTest {
assertThat(diffInfo).content().element(0).numberOfSkippedLines().isGreaterThan(0);
}
@Test
public void editNotAllowedAsBase() throws Exception {
gApi.changes().id(changeId).edit().create();
BadRequestException e =
assertThrows(
BadRequestException.class,
() -> getDiffRequest(changeId, CURRENT, FILE_NAME).withBase("edit").get());
assertThat(e).hasMessageThat().isEqualTo("edit not allowed as base");
e =
assertThrows(
BadRequestException.class,
() -> getDiffRequest(changeId, CURRENT, FILE_NAME).withBase("0").get());
assertThat(e).hasMessageThat().isEqualTo("edit not allowed as base");
}
private static CommentInput createCommentInput(
int startLine, int startCharacter, int endLine, int endCharacter, String message) {
CommentInput comment = new CommentInput();