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);
}
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);
}
}
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;
}
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;
}
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();
}
Aggregations