Search in sources :

Example 1 with Comment

use of com.google.gerrit.entities.Comment in project gerrit by GerritCodeReview.

the class CommentTimestampAdapterTest method newAdapterRoundTripOfWholeComment.

@Test
public void newAdapterRoundTripOfWholeComment() {
    Comment c = new HumanComment(new Comment.Key("uuid", "filename", 1), Account.id(100), NON_DST_TS.toInstant(), (short) 0, "message", "serverId", false);
    c.lineNbr = 1;
    c.setCommitId(ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));
    String json = gson.toJson(c);
    assertThat(json).contains("\"writtenOn\": \"" + NON_DST_STR_TRUNC + "\",");
    Comment result = gson.fromJson(json, HumanComment.class);
    // Round-trip lossily truncates ms, but that's ok.
    assertThat(result.writtenOn).isEqualTo(NON_DST_TS_TRUNC);
    result.writtenOn = NON_DST_TS;
    assertThat(result).isEqualTo(c);
}
Also used : Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) HumanComment(com.google.gerrit.entities.HumanComment) Test(org.junit.Test)

Example 2 with Comment

use of com.google.gerrit.entities.Comment in project gerrit by GerritCodeReview.

the class RevisionNoteBuilder method buildNoteJson.

private void buildNoteJson(ChangeNoteJson noteUtil, OutputStream out) throws IOException {
    ListMultimap<Integer, Comment> comments = buildCommentMap();
    if (submitRequirementResults == null && comments.isEmpty() && pushCert == null) {
        return;
    }
    RevisionNoteData data = new RevisionNoteData();
    data.comments = COMMENT_ORDER.sortedCopy(comments.values());
    data.pushCert = pushCert;
    data.submitRequirementResults = submitRequirementResults == null ? null : submitRequirementResults.stream().sorted(SUBMIT_REQUIREMENT_RESULT_COMPARATOR).collect(Collectors.toList());
    try (OutputStreamWriter osw = new OutputStreamWriter(out, UTF_8)) {
        noteUtil.getGson().toJson(data, osw);
    }
}
Also used : Comment(com.google.gerrit.entities.Comment) OutputStreamWriter(java.io.OutputStreamWriter)

Example 3 with Comment

use of com.google.gerrit.entities.Comment 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 4 with Comment

use of com.google.gerrit.entities.Comment 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 5 with Comment

use of com.google.gerrit.entities.Comment in project gerrit by GerritCodeReview.

the class CommentCumulativeSizeValidator method validateComments.

@Override
public ImmutableList<CommentValidationFailure> validateComments(CommentValidationContext ctx, ImmutableList<CommentForValidation> comments) {
    ChangeNotes notes = notesFactory.createChecked(Project.nameKey(ctx.getProject()), Change.id(ctx.getChangeId()));
    int existingCumulativeSize = Stream.concat(notes.getHumanComments().values().stream(), notes.getRobotComments().values().stream()).mapToInt(Comment::getApproximateSize).sum() + notes.getChangeMessages().stream().mapToInt(cm -> cm.getMessage().length()).sum();
    int newCumulativeSize = comments.stream().mapToInt(CommentForValidation::getApproximateSize).sum();
    ImmutableList.Builder<CommentValidationFailure> failures = ImmutableList.builder();
    if (!comments.isEmpty() && !isEnoughSpace(notes, newCumulativeSize, maxCumulativeSize)) {
        // This warning really applies to the set of all comments, but we need to pick one to attach
        // the message to.
        CommentForValidation commentForFailureMessage = Iterables.getLast(comments);
        failures.add(commentForFailureMessage.failValidation(String.format("Exceeding maximum cumulative size of comments and change messages:" + " %d (existing) + %d (new) > %d", existingCumulativeSize, newCumulativeSize, maxCumulativeSize)));
    }
    return failures.build();
}
Also used : Comment(com.google.gerrit.entities.Comment) CommentValidationFailure(com.google.gerrit.extensions.validators.CommentValidationFailure) ImmutableList(com.google.common.collect.ImmutableList) CommentForValidation(com.google.gerrit.extensions.validators.CommentForValidation) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes)

Aggregations

Comment (com.google.gerrit.entities.Comment)12 HumanComment (com.google.gerrit.entities.HumanComment)9 RobotComment (com.google.gerrit.entities.RobotComment)5 Test (org.junit.Test)4 Map (java.util.Map)3 FluentLogger (com.google.common.flogger.FluentLogger)2 Project (com.google.gerrit.entities.Project)2 Inject (com.google.inject.Inject)2 Assisted (com.google.inject.assistedinject.Assisted)2 IOException (java.io.IOException)2 List (java.util.List)2 Optional (java.util.Optional)2 Repository (org.eclipse.jgit.lib.Repository)2 AutoValue (com.google.auto.value.AutoValue)1 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 Iterables (com.google.common.collect.Iterables)1