use of com.google.gerrit.entities.HumanComment in project gerrit by GerritCodeReview.
the class DeleteCommentRewriter method rewriteCommitHistory.
@Override
public ObjectId rewriteCommitHistory(RevWalk revWalk, ObjectInserter inserter, ObjectId currTip) throws IOException, ConfigInvalidException {
checkArgument(!currTip.equals(ObjectId.zeroId()));
// Walk from the first commit of the branch.
revWalk.reset();
revWalk.markStart(revWalk.parseCommit(currTip));
revWalk.sort(RevSort.REVERSE);
ObjectReader reader = revWalk.getObjectReader();
// The first commit will not be rewritten.
RevCommit newTipCommit = revWalk.next();
Map<String, HumanComment> parentComments = getPublishedComments(noteUtil, reader, NoteMap.read(reader, newTipCommit));
boolean rewrite = false;
RevCommit originalCommit;
while ((originalCommit = revWalk.next()) != null) {
NoteMap noteMap = NoteMap.read(reader, originalCommit);
Map<String, HumanComment> currComments = getPublishedComments(noteUtil, reader, noteMap);
if (!rewrite && currComments.containsKey(uuid)) {
rewrite = true;
}
if (!rewrite) {
parentComments = currComments;
newTipCommit = originalCommit;
continue;
}
List<HumanComment> putInComments = getPutInComments(parentComments, currComments);
List<HumanComment> deletedComments = getDeletedComments(parentComments, currComments);
newTipCommit = revWalk.parseCommit(rewriteCommit(originalCommit, newTipCommit, inserter, reader, putInComments, deletedComments));
parentComments = currComments;
}
return newTipCommit;
}
use of com.google.gerrit.entities.HumanComment in project gerrit by GerritCodeReview.
the class DeleteCommentRewriter method rewriteCommit.
/**
* Rewrites one commit.
*
* @param originalCommit the original commit to be rewritten.
* @param parentCommit the parent of the new commit.
* @param inserter the {@code ObjectInserter} for the rewrite process.
* @param reader the {@code ObjectReader} for the rewrite process.
* @param putInComments the comments put in by this commit.
* @param deletedComments the comments deleted by this commit.
* @return the {@code objectId} of the new commit.
*/
private ObjectId rewriteCommit(RevCommit originalCommit, RevCommit parentCommit, ObjectInserter inserter, ObjectReader reader, List<HumanComment> putInComments, List<HumanComment> deletedComments) throws IOException, ConfigInvalidException {
RevisionNoteMap<ChangeRevisionNote> revNotesMap = RevisionNoteMap.parse(noteUtil.getChangeNoteJson(), reader, NoteMap.read(reader, parentCommit), HumanComment.Status.PUBLISHED);
RevisionNoteBuilder.Cache cache = new RevisionNoteBuilder.Cache(revNotesMap);
for (HumanComment c : putInComments) {
cache.get(c.getCommitId()).putComment(c);
}
for (HumanComment c : deletedComments) {
cache.get(c.getCommitId()).deleteComment(c.key);
}
Map<ObjectId, RevisionNoteBuilder> builders = cache.getBuilders();
for (Map.Entry<ObjectId, RevisionNoteBuilder> entry : builders.entrySet()) {
ObjectId objectId = entry.getKey();
byte[] data = entry.getValue().build(noteUtil.getChangeNoteJson());
if (data.length == 0) {
revNotesMap.noteMap.remove(objectId);
} else {
revNotesMap.noteMap.set(objectId, inserter.insert(OBJ_BLOB, data));
}
}
CommitBuilder cb = new CommitBuilder();
cb.setParentId(parentCommit);
cb.setTreeId(revNotesMap.noteMap.writeTree(inserter));
cb.setMessage(originalCommit.getFullMessage());
cb.setCommitter(originalCommit.getCommitterIdent());
cb.setAuthor(originalCommit.getAuthorIdent());
cb.setEncoding(originalCommit.getEncoding());
return inserter.insert(cb);
}
use of com.google.gerrit.entities.HumanComment in project gerrit by GerritCodeReview.
the class CommentSender method getCommentGroupsTemplateData.
/**
* Returns grouped inline comment data mapped to data structures that are suitable for passing
* into Soy.
*/
private List<Map<String, Object>> getCommentGroupsTemplateData(Repository repo) {
List<Map<String, Object>> commentGroups = new ArrayList<>();
for (CommentSender.FileCommentGroup group : getGroupedInlineComments(repo)) {
Map<String, Object> groupData = new HashMap<>();
groupData.put("title", group.getTitle());
groupData.put("patchSetId", group.patchSetId);
List<Map<String, Object>> commentsList = new ArrayList<>();
for (Comment comment : group.comments) {
Map<String, Object> commentData = new HashMap<>();
if (group.fileData != null) {
commentData.put("lines", getLinesOfComment(comment, group.fileData));
}
commentData.put("message", comment.message.trim());
List<CommentFormatter.Block> blocks = CommentFormatter.parse(comment.message);
commentData.put("messageBlocks", commentBlocksToSoyData(blocks));
// Set the prefix.
String prefix = getCommentLinePrefix(comment);
commentData.put("linePrefix", prefix);
commentData.put("linePrefixEmpty", Strings.padStart(": ", prefix.length(), ' '));
// Set line numbers.
int startLine;
if (comment.range == null) {
startLine = comment.lineNbr;
} else {
startLine = comment.range.startLine;
commentData.put("endLine", comment.range.endLine);
}
commentData.put("startLine", startLine);
if (comment.key.filename.equals(Patch.PATCHSET_LEVEL)) {
if (comment instanceof RobotComment) {
commentData.put("link", group.getFindingsTabLink());
} else {
commentData.put("link", group.getCommentsTabLink());
}
} else {
commentData.put("link", group.getCommentLink(comment.key.uuid));
}
// Set robot comment data.
if (comment instanceof RobotComment) {
RobotComment robotComment = (RobotComment) comment;
commentData.put("isRobotComment", true);
commentData.put("robotId", robotComment.robotId);
commentData.put("robotRunId", robotComment.robotRunId);
commentData.put("robotUrl", robotComment.url);
} else {
commentData.put("isRobotComment", false);
}
// If the comment has a quote, don't bother loading the parent message.
if (!hasQuote(blocks)) {
// Set parent comment info.
Optional<HumanComment> parent = getParent(comment);
if (parent.isPresent()) {
commentData.put("parentMessage", getShortenedCommentMessage(parent.get()));
}
}
commentsList.add(commentData);
}
groupData.put("comments", commentsList);
commentGroups.add(groupData);
}
return commentGroups;
}
use of com.google.gerrit.entities.HumanComment in project gerrit by GerritCodeReview.
the class CommentSender method getReplyAccounts.
/**
* Get the set of accounts whose comments have been replied to in this email.
*/
private HashSet<Account.Id> getReplyAccounts() {
HashSet<Account.Id> replyAccounts = new HashSet<>();
// Track visited parent UUIDs to avoid cycles.
HashSet<String> visitedUuids = new HashSet<>();
for (Comment comment : inlineComments) {
visitedUuids.add(comment.key.uuid);
// Traverse the parent relation to the top of the comment thread.
Comment current = comment;
while (current.parentUuid != null && !visitedUuids.contains(current.parentUuid)) {
Optional<HumanComment> optParent = getParent(current);
if (!optParent.isPresent()) {
// There is a parent UUID, but it cannot be loaded, break from the comment thread.
break;
}
HumanComment parent = optParent.get();
replyAccounts.add(parent.author.getId());
visitedUuids.add(current.parentUuid);
current = parent;
}
}
return replyAccounts;
}
use of com.google.gerrit.entities.HumanComment in project gerrit by GerritCodeReview.
the class ImpersonationIT method testVoteOnBehalfOfWithComment.
private void testVoteOnBehalfOfWithComment() throws Exception {
allowCodeReviewOnBehalfOf();
PushOneCommit.Result r = createChange();
ReviewInput in = new ReviewInput();
in.onBehalfOf = user.id().toString();
in.label("Code-Review", 1);
CommentInput ci = new CommentInput();
ci.path = Patch.COMMIT_MSG;
ci.side = Side.REVISION;
ci.line = 1;
ci.message = "message";
in.comments = ImmutableMap.of(ci.path, ImmutableList.of(ci));
gApi.changes().id(r.getChangeId()).current().review(in);
PatchSetApproval psa = Iterables.getOnlyElement(r.getChange().approvals().values());
assertThat(psa.patchSetId().get()).isEqualTo(1);
assertThat(psa.label()).isEqualTo("Code-Review");
assertThat(psa.accountId()).isEqualTo(user.id());
assertThat(psa.value()).isEqualTo(1);
assertThat(psa.realAccountId()).isEqualTo(admin.id());
ChangeData cd = r.getChange();
HumanComment c = Iterables.getOnlyElement(commentsUtil.publishedHumanCommentsByChange(cd.notes()));
assertThat(c.message).isEqualTo(ci.message);
assertThat(c.author.getId()).isEqualTo(user.id());
assertThat(c.getRealAuthor().getId()).isEqualTo(admin.id());
}
Aggregations