use of com.google.gerrit.extensions.api.changes.DeleteCommentInput in project gerrit by GerritCodeReview.
the class CommentsIT method deleteCommentCannotBeAppliedByUser.
@Test
public void deleteCommentCannotBeAppliedByUser() throws Exception {
PushOneCommit.Result result = createChange();
CommentInput targetComment = CommentsUtil.addComment(gApi, result.getChangeId());
Map<String, List<CommentInfo>> commentsMap = getPublishedComments(result.getChangeId(), result.getCommit().name());
assertThat(commentsMap).hasSize(1);
assertThat(commentsMap.get(FILE_NAME)).hasSize(1);
String uuid = commentsMap.get(targetComment.path).get(0).id;
DeleteCommentInput input = new DeleteCommentInput("contains confidential information");
requestScopeOperations.setApiUser(user.id());
assertThrows(AuthException.class, () -> gApi.changes().id(result.getChangeId()).current().comment(uuid).delete(input));
}
use of com.google.gerrit.extensions.api.changes.DeleteCommentInput in project gerrit by GerritCodeReview.
the class CommentsIT method deleteCommentByRewritingCommitHistory.
@Test
public void deleteCommentByRewritingCommitHistory() throws Exception {
// Creates the following commit history on the meta branch of the test change. Then tries to
// delete the comments one by one, which will rewrite most of the commits on the 'meta' branch.
// Commits will be rewritten N times for N added comments. After each deletion, the meta branch
// should keep its previous state except that the target comment's message should be updated.
// 1st commit: Create PS1.
PushOneCommit.Result result1 = createChange(SUBJECT, "a.txt", "a");
Change.Id id = result1.getChange().getId();
String changeId = result1.getChangeId();
String ps1 = result1.getCommit().name();
// 2nd commit: Add (c1) to PS1.
CommentInput c1 = CommentsUtil.newComment("a.txt", "comment 1");
CommentsUtil.addComments(gApi, changeId, ps1, c1);
// 3rd commit: Add (c2, c3) to PS1.
CommentInput c2 = CommentsUtil.newComment("a.txt", "comment 2");
CommentInput c3 = CommentsUtil.newComment("a.txt", "comment 3");
CommentsUtil.addComments(gApi, changeId, ps1, c2, c3);
// 4th commit: Add (c4) to PS1.
CommentInput c4 = CommentsUtil.newComment("a.txt", "comment 4");
CommentsUtil.addComments(gApi, changeId, ps1, c4);
// 5th commit: Create PS2.
PushOneCommit.Result result2 = amendChange(changeId, "refs/for/master", "b.txt", "b");
String ps2 = result2.getCommit().name();
// 6th commit: Add (c5) to PS1.
CommentInput c5 = CommentsUtil.newComment("a.txt", "comment 5");
CommentsUtil.addComments(gApi, changeId, ps1, c5);
// 7th commit: Add (c6) to PS2.
CommentInput c6 = CommentsUtil.newComment("b.txt", "comment 6");
CommentsUtil.addComments(gApi, changeId, ps2, c6);
// 8th commit: Create PS3.
PushOneCommit.Result result3 = amendChange(changeId);
String ps3 = result3.getCommit().name();
// 9th commit: Create PS4.
PushOneCommit.Result result4 = amendChange(changeId, "refs/for/master", "c.txt", "c");
String ps4 = result4.getCommit().name();
// 10th commit: Add (c7, c8) to PS4.
CommentInput c7 = CommentsUtil.newComment("c.txt", "comment 7");
CommentInput c8 = CommentsUtil.newComment("b.txt", "comment 8");
CommentsUtil.addComments(gApi, changeId, ps4, c7, c8);
// 11th commit: Add (c9) to PS2.
CommentInput c9 = CommentsUtil.newCommentWithOnlyMandatoryFields("b.txt", "comment 9");
CommentsUtil.addComments(gApi, changeId, ps2, c9);
List<CommentInfo> commentsBeforeDelete = getChangeSortedComments(id.get());
assertThat(commentsBeforeDelete).hasSize(9);
// PS1 has comments [c1, c2, c3, c4, c5].
assertThat(getRevisionComments(changeId, ps1)).hasSize(5);
// PS2 has comments [c6, c9].
assertThat(getRevisionComments(changeId, ps2)).hasSize(2);
// PS3 has no comment.
assertThat(getRevisionComments(changeId, ps3)).isEmpty();
// PS4 has comments [c7, c8].
assertThat(getRevisionComments(changeId, ps4)).hasSize(2);
requestScopeOperations.setApiUser(admin.id());
for (int i = 0; i < commentsBeforeDelete.size(); i++) {
List<RevCommit> commitsBeforeDelete = getChangeMetaCommitsInReverseOrder(id);
CommentInfo comment = commentsBeforeDelete.get(i);
String uuid = comment.id;
int patchSet = comment.patchSet;
// 'oldComment' has some fields unset compared with 'comment'.
CommentInfo oldComment = gApi.changes().id(changeId).revision(patchSet).comment(uuid).get();
DeleteCommentInput input = new DeleteCommentInput("delete comment " + uuid);
CommentInfo updatedComment = gApi.changes().id(changeId).revision(patchSet).comment(uuid).delete(input);
String expectedMsg = String.format("Comment removed by: %s; Reason: %s", admin.fullName(), input.reason);
assertThat(updatedComment.message).isEqualTo(expectedMsg);
oldComment.message = expectedMsg;
assertThat(updatedComment).isEqualTo(oldComment);
// Check the NoteDb state after the deletion.
assertMetaBranchCommitsAfterRewriting(commitsBeforeDelete, id, uuid, expectedMsg);
comment.message = expectedMsg;
commentsBeforeDelete.set(i, comment);
List<CommentInfo> commentsAfterDelete = getChangeSortedComments(id.get());
assertThat(commentsAfterDelete).isEqualTo(commentsBeforeDelete);
}
// Make sure that comments can still be added correctly.
CommentInput c10 = CommentsUtil.newComment("a.txt", "comment 10");
CommentInput c11 = CommentsUtil.newComment("b.txt", "comment 11");
CommentInput c12 = CommentsUtil.newComment("a.txt", "comment 12");
CommentInput c13 = CommentsUtil.newComment("c.txt", "comment 13");
CommentsUtil.addComments(gApi, changeId, ps1, c10);
CommentsUtil.addComments(gApi, changeId, ps2, c11);
CommentsUtil.addComments(gApi, changeId, ps3, c12);
CommentsUtil.addComments(gApi, changeId, ps4, c13);
assertThat(getChangeSortedComments(id.get())).hasSize(13);
assertThat(getRevisionComments(changeId, ps1)).hasSize(6);
assertThat(getRevisionComments(changeId, ps2)).hasSize(3);
assertThat(getRevisionComments(changeId, ps3)).hasSize(1);
assertThat(getRevisionComments(changeId, ps4)).hasSize(3);
}
use of com.google.gerrit.extensions.api.changes.DeleteCommentInput in project gerrit by GerritCodeReview.
the class CommentsIT method deleteOneCommentMultipleTimes.
@Test
public void deleteOneCommentMultipleTimes() throws Exception {
PushOneCommit.Result result = createChange();
Change.Id id = result.getChange().getId();
String changeId = result.getChangeId();
String ps1 = result.getCommit().name();
CommentInput c1 = CommentsUtil.newComment(FILE_NAME, "comment 1");
CommentInput c2 = CommentsUtil.newComment(FILE_NAME, "comment 2");
CommentInput c3 = CommentsUtil.newComment(FILE_NAME, "comment 3");
CommentsUtil.addComments(gApi, changeId, ps1, c1);
CommentsUtil.addComments(gApi, changeId, ps1, c2);
CommentsUtil.addComments(gApi, changeId, ps1, c3);
List<CommentInfo> commentsBeforeDelete = getChangeSortedComments(id.get());
assertThat(commentsBeforeDelete).hasSize(3);
Optional<CommentInfo> targetComment = commentsBeforeDelete.stream().filter(c -> c.message.equals("comment 2")).findFirst();
assertThat(targetComment).isPresent();
String uuid = targetComment.get().id;
CommentInfo oldComment = gApi.changes().id(changeId).revision(ps1).comment(uuid).get();
List<RevCommit> commitsBeforeDelete = getChangeMetaCommitsInReverseOrder(id);
requestScopeOperations.setApiUser(admin.id());
for (int i = 0; i < 3; i++) {
DeleteCommentInput input = new DeleteCommentInput("delete comment 2, iteration: " + i);
gApi.changes().id(changeId).revision(ps1).comment(uuid).delete(input);
}
CommentInfo updatedComment = gApi.changes().id(changeId).revision(ps1).comment(uuid).get();
String expectedMsg = String.format("Comment removed by: %s; Reason: %s", admin.fullName(), "delete comment 2, iteration: 2");
assertThat(updatedComment.message).isEqualTo(expectedMsg);
oldComment.message = expectedMsg;
assertThat(updatedComment).isEqualTo(oldComment);
assertMetaBranchCommitsAfterRewriting(commitsBeforeDelete, id, uuid, expectedMsg);
assertThat(getChangeSortedComments(id.get())).hasSize(3);
}
Aggregations