Merge "Return empty context if the comment range is outside file boundaries"

This commit is contained in:
Youssef Elghareeb
2021-02-18 13:11:09 +00:00
committed by Gerrit Code Review
2 changed files with 8 additions and 15 deletions

View File

@@ -26,7 +26,6 @@ import com.google.gerrit.common.Nullable;
import com.google.gerrit.entities.Comment;
import com.google.gerrit.entities.CommentContext;
import com.google.gerrit.entities.Project;
import com.google.gerrit.exceptions.StorageException;
import com.google.gerrit.extensions.common.ContextLineInfo;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.patch.ComparisonType;
@@ -158,12 +157,10 @@ public class CommentContextLoader {
private static CommentContext createContext(Text src, Range commentRange, int contextPadding) {
if (commentRange.start() < 1 || commentRange.end() - 1 > src.size()) {
throw new StorageException(
"Invalid comment range "
+ commentRange
+ ". Text only contains "
+ src.size()
+ " lines.");
// TODO(ghareeb): We should throw an exception in this case. See
// https://bugs.chromium.org/p/gerrit/issues/detail?id=14102 which is an example where the
// diff contains an extra line not in the original file.
return CommentContext.empty();
}
commentRange = adjustRange(commentRange, contextPadding, src.size());
ImmutableMap.Builder<Integer, String> context =

View File

@@ -19,11 +19,9 @@ import static com.google.gerrit.acceptance.PushOneCommit.FILE_NAME;
import static com.google.gerrit.entities.Patch.COMMIT_MSG;
import static com.google.gerrit.entities.Patch.MERGE_LIST;
import static com.google.gerrit.entities.Patch.PATCHSET_LEVEL;
import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.MoreCollectors;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.PushOneCommit;
@@ -148,12 +146,10 @@ public class CommentContextIT extends AbstractDaemonTest {
CommentsUtil.newComment(COMMIT_MSG, Side.REVISION, 100, "comment", false);
CommentsUtil.addComments(gApi, changeId, ps1, comment);
Throwable thrown =
assertThrows(
UncheckedExecutionException.class,
() -> gApi.changes().id(changeId).commentsRequest().withContext(true).getAsList());
assertThat(thrown).hasCauseThat().hasMessageThat().contains("Invalid comment range");
List<CommentInfo> comments =
gApi.changes().id(changeId).commentsRequest().withContext(true).getAsList();
assertThat(comments).hasSize(1);
assertThat(comments.get(0).contextLines).isEmpty();
}
@Test