Search in sources :

Example 31 with HumanComment

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;
}
Also used : ObjectReader(org.eclipse.jgit.lib.ObjectReader) NoteMap(org.eclipse.jgit.notes.NoteMap) HumanComment(com.google.gerrit.entities.HumanComment) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 32 with HumanComment

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);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) HumanComment(com.google.gerrit.entities.HumanComment) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) NoteMap(org.eclipse.jgit.notes.NoteMap)

Example 33 with HumanComment

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;
}
Also used : Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) RobotComment(com.google.gerrit.entities.RobotComment) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RobotComment(com.google.gerrit.entities.RobotComment) HashMap(java.util.HashMap) Map(java.util.Map) HumanComment(com.google.gerrit.entities.HumanComment)

Example 34 with HumanComment

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;
}
Also used : Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) RobotComment(com.google.gerrit.entities.RobotComment) ZoneId(java.time.ZoneId) HumanComment(com.google.gerrit.entities.HumanComment) HashSet(java.util.HashSet)

Example 35 with HumanComment

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());
}
Also used : CommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.CommentInput) RobotCommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.RobotCommentInput) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) ChangeData(com.google.gerrit.server.query.change.ChangeData) HumanComment(com.google.gerrit.entities.HumanComment) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit)

Aggregations

HumanComment (com.google.gerrit.entities.HumanComment)87 Test (org.junit.Test)53 Change (com.google.gerrit.entities.Change)39 PatchSet (com.google.gerrit.entities.PatchSet)39 ObjectId (org.eclipse.jgit.lib.ObjectId)39 CommentRange (com.google.gerrit.entities.CommentRange)25 Instant (java.time.Instant)20 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)14 Comment (com.google.gerrit.entities.Comment)11 Project (com.google.gerrit.entities.Project)11 Map (java.util.Map)11 ArrayList (java.util.ArrayList)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 HashMap (java.util.HashMap)7 NoteMap (org.eclipse.jgit.notes.NoteMap)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 TraceTimer (com.google.gerrit.server.logging.TraceContext.TraceTimer)6 FluentLogger (com.google.common.flogger.FluentLogger)5 Account (com.google.gerrit.entities.Account)5 BranchNameKey (com.google.gerrit.entities.BranchNameKey)5