Search in sources :

Example 1 with RobotComment

use of com.google.gerrit.reviewdb.client.RobotComment in project gerrit by GerritCodeReview.

the class RobotCommentUpdate method storeCommentsInNotes.

private CommitBuilder storeCommentsInNotes(RevWalk rw, ObjectInserter ins, ObjectId curr, CommitBuilder cb) throws ConfigInvalidException, OrmException, IOException {
    RevisionNoteMap<RobotCommentsRevisionNote> rnm = getRevisionNoteMap(rw, curr);
    Set<RevId> updatedRevs = Sets.newHashSetWithExpectedSize(rnm.revisionNotes.size());
    RevisionNoteBuilder.Cache cache = new RevisionNoteBuilder.Cache(rnm);
    for (RobotComment c : put) {
        cache.get(new RevId(c.revId)).putComment(c);
    }
    Map<RevId, RevisionNoteBuilder> builders = cache.getBuilders();
    boolean touchedAnyRevs = false;
    boolean hasComments = false;
    for (Map.Entry<RevId, RevisionNoteBuilder> e : builders.entrySet()) {
        updatedRevs.add(e.getKey());
        ObjectId id = ObjectId.fromString(e.getKey().get());
        byte[] data = e.getValue().build(noteUtil, true);
        if (!Arrays.equals(data, e.getValue().baseRaw)) {
            touchedAnyRevs = true;
        }
        if (data.length == 0) {
            rnm.noteMap.remove(id);
        } else {
            hasComments = true;
            ObjectId dataBlob = ins.insert(OBJ_BLOB, data);
            rnm.noteMap.set(id, dataBlob);
        }
    }
    // data yet.
    if (!touchedAnyRevs) {
        return NO_OP_UPDATE;
    }
    // If we touched every revision and there are no comments left, tell the
    // caller to delete the entire ref.
    boolean touchedAllRevs = updatedRevs.equals(rnm.revisionNotes.keySet());
    if (touchedAllRevs && !hasComments) {
        return null;
    }
    cb.setTreeId(rnm.noteMap.writeTree(ins));
    return cb;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevId(com.google.gerrit.reviewdb.client.RevId) RobotComment(com.google.gerrit.reviewdb.client.RobotComment) Map(java.util.Map) NoteMap(org.eclipse.jgit.notes.NoteMap)

Example 2 with RobotComment

use of com.google.gerrit.reviewdb.client.RobotComment in project gerrit by GerritCodeReview.

the class CommentSender method getCommentGroupsTemplateData.

/**
   * @return 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("link", group.getLink());
        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<>();
            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);
            // Set the comment link.
            if (comment.lineNbr == 0) {
                commentData.put("link", group.getLink());
            } else if (comment.side == 0) {
                commentData.put("link", group.getLink() + "@a" + startLine);
            } else {
                commentData.put("link", group.getLink() + '@' + startLine);
            }
            // 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<Comment> 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.reviewdb.client.Comment) RobotComment(com.google.gerrit.reviewdb.client.RobotComment) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RobotComment(com.google.gerrit.reviewdb.client.RobotComment) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with RobotComment

use of com.google.gerrit.reviewdb.client.RobotComment in project gerrit by GerritCodeReview.

the class RobotComments method parse.

@Override
public RobotCommentResource parse(RevisionResource rev, IdString id) throws ResourceNotFoundException, OrmException {
    String uuid = id.get();
    ChangeNotes notes = rev.getNotes();
    for (RobotComment c : commentsUtil.robotCommentsByPatchSet(notes, rev.getPatchSet().getId())) {
        if (uuid.equals(c.key.uuid)) {
            return new RobotCommentResource(rev, c);
        }
    }
    throw new ResourceNotFoundException(id);
}
Also used : RobotComment(com.google.gerrit.reviewdb.client.RobotComment) IdString(com.google.gerrit.extensions.restapi.IdString) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 4 with RobotComment

use of com.google.gerrit.reviewdb.client.RobotComment in project gerrit by GerritCodeReview.

the class ImpersonationIT method voteOnBehalfOfWithRobotComment.

@GerritConfig(name = "notedb.writeJson", value = "true")
@Test
public void voteOnBehalfOfWithRobotComment() throws Exception {
    assume().that(notesMigration.readChanges()).isTrue();
    allowCodeReviewOnBehalfOf();
    PushOneCommit.Result r = createChange();
    ReviewInput in = new ReviewInput();
    in.onBehalfOf = user.id.toString();
    in.label("Code-Review", 1);
    RobotCommentInput ci = new RobotCommentInput();
    ci.robotId = "my-robot";
    ci.robotRunId = "abcd1234";
    ci.path = Patch.COMMIT_MSG;
    ci.side = Side.REVISION;
    ci.line = 1;
    ci.message = "message";
    in.robotComments = ImmutableMap.of(ci.path, ImmutableList.of(ci));
    gApi.changes().id(r.getChangeId()).current().review(in);
    ChangeData cd = r.getChange();
    RobotComment c = Iterables.getOnlyElement(commentsUtil.robotCommentsByChange(cd.notes()));
    assertThat(c.message).isEqualTo(ci.message);
    assertThat(c.robotId).isEqualTo(ci.robotId);
    assertThat(c.robotRunId).isEqualTo(ci.robotRunId);
    assertThat(c.author.getId()).isEqualTo(user.id);
    assertThat(c.getRealAuthor().getId()).isEqualTo(admin.id);
}
Also used : RobotComment(com.google.gerrit.reviewdb.client.RobotComment) RobotCommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.RobotCommentInput) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) ChangeData(com.google.gerrit.server.query.change.ChangeData) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) GerritConfig(com.google.gerrit.acceptance.GerritConfig) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 5 with RobotComment

use of com.google.gerrit.reviewdb.client.RobotComment in project gerrit by GerritCodeReview.

the class Fixes method parse.

@Override
public FixResource parse(RevisionResource revisionResource, IdString id) throws ResourceNotFoundException, OrmException {
    String fixId = id.get();
    ChangeNotes changeNotes = revisionResource.getNotes();
    List<RobotComment> robotComments = commentsUtil.robotCommentsByPatchSet(changeNotes, revisionResource.getPatchSet().getId());
    for (RobotComment robotComment : robotComments) {
        for (FixSuggestion fixSuggestion : robotComment.fixSuggestions) {
            if (Objects.equals(fixId, fixSuggestion.fixId)) {
                return new FixResource(revisionResource, fixSuggestion.replacements);
            }
        }
    }
    throw new ResourceNotFoundException(id);
}
Also used : FixSuggestion(com.google.gerrit.reviewdb.client.FixSuggestion) RobotComment(com.google.gerrit.reviewdb.client.RobotComment) IdString(com.google.gerrit.extensions.restapi.IdString) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Aggregations

RobotComment (com.google.gerrit.reviewdb.client.RobotComment)8 IdString (com.google.gerrit.extensions.restapi.IdString)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 Comment (com.google.gerrit.reviewdb.client.Comment)2 RevId (com.google.gerrit.reviewdb.client.RevId)2 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)2 Map (java.util.Map)2 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)1 GerritConfig (com.google.gerrit.acceptance.GerritConfig)1 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)1 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)1 RobotCommentInput (com.google.gerrit.extensions.api.changes.ReviewInput.RobotCommentInput)1 FixSuggestion (com.google.gerrit.reviewdb.client.FixSuggestion)1 PatchLineComment (com.google.gerrit.reviewdb.client.PatchLineComment)1 ChangeData (com.google.gerrit.server.query.change.ChangeData)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1 NoteMap (org.eclipse.jgit.notes.NoteMap)1