Search in sources :

Example 1 with HumanComment

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

the class CommentsUtil method byPatchSet.

public List<HumanComment> byPatchSet(ChangeNotes notes, PatchSet.Id psId) {
    List<HumanComment> comments = new ArrayList<>();
    comments.addAll(publishedByPatchSet(notes, psId));
    for (Ref ref : getDraftRefs(notes.getChangeId())) {
        Account.Id account = Account.Id.fromRefSuffix(ref.getName());
        if (account != null) {
            comments.addAll(draftByPatchSetAuthor(psId, account, notes));
        }
    }
    return sort(comments);
}
Also used : Account(com.google.gerrit.entities.Account) Ref(org.eclipse.jgit.lib.Ref) ArrayList(java.util.ArrayList) HumanComment(com.google.gerrit.entities.HumanComment)

Example 2 with HumanComment

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

the class PublishCommentUtil method publish.

public void publish(ChangeContext ctx, ChangeUpdate changeUpdate, Collection<HumanComment> draftComments, @Nullable String tag) {
    ChangeNotes notes = ctx.getNotes();
    checkArgument(notes != null);
    if (draftComments.isEmpty()) {
        return;
    }
    Map<PatchSet.Id, PatchSet> patchSets = psUtil.getAsMap(notes, draftComments.stream().map(d -> psId(notes, d)).collect(toSet()));
    Set<HumanComment> commentsToPublish = new HashSet<>();
    for (HumanComment draftComment : draftComments) {
        PatchSet.Id psIdOfDraftComment = psId(notes, draftComment);
        PatchSet ps = patchSets.get(psIdOfDraftComment);
        if (ps == null) {
            // This can happen if changes with the same numeric ID exist:
            // - change 12345 has 3 patch sets in repo X
            // - another change 12345 has 7 patch sets in repo Y
            // - the user saves a draft comment on patch set 6 of the change in repo Y
            // - this draft comment gets stored in:
            // AllUsers -> refs/draft-comments/45/12345/<account-id>
            // - when posting a review with draft handling PUBLISH_ALL_REVISIONS on the change in
            // repo X, the draft comments are loaded from
            // AllUsers -> refs/draft-comments/45/12345/<account-id>, including the draft
            // comment that was saved for patch set 6 of the change in repo Y
            // - patch set 6 does not exist for the change in repo x, hence we get null for the patch
            // set here
            // Instead of failing hard (and returning an Internal Server Error) to the caller,
            // just ignore that comment.
            // Gerrit ensures that numeric change IDs are unique, but you can get duplicates if
            // change refs of one repo are copied/pushed to another repo on the same host (this
            // should never be done, but we know it happens).
            logger.atWarning().log("Ignoring draft comment %s on non existing patch set %s (repo = %s)", draftComment, psIdOfDraftComment, notes.getProjectName());
            continue;
        }
        draftComment.writtenOn = Timestamp.from(ctx.getWhen());
        draftComment.tag = tag;
        // Draft may have been created by a different real user; copy the current real user. (Only
        // applies to X-Gerrit-RunAs, since modifying drafts via on_behalf_of is not allowed.)
        ctx.getUser().updateRealAccountId(draftComment::setRealAuthor);
        commentsUtil.setCommentCommitId(draftComment, notes.getChange(), ps);
        commentsToPublish.add(draftComment);
    }
    commentsUtil.putHumanComments(changeUpdate, HumanComment.Status.PUBLISHED, commentsToPublish);
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) HumanComment(com.google.gerrit.entities.HumanComment) HashSet(java.util.HashSet)

Example 3 with HumanComment

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

the class CommentsIT method assertMetaBranchCommitsAfterRewriting.

/**
 * All the commits, which contain the target comment before, should still contain the comment with
 * the updated message. All the other metas of the commits should be exactly the same.
 */
private void assertMetaBranchCommitsAfterRewriting(List<RevCommit> beforeDelete, Change.Id changeId, String targetCommentUuid, String expectedMessage) throws Exception {
    List<RevCommit> afterDelete = getChangeMetaCommitsInReverseOrder(changeId);
    assertThat(afterDelete).hasSize(beforeDelete.size());
    try (Repository repo = repoManager.openRepository(project);
        ObjectReader reader = repo.newObjectReader()) {
        for (int i = 0; i < beforeDelete.size(); i++) {
            RevCommit commitBefore = beforeDelete.get(i);
            RevCommit commitAfter = afterDelete.get(i);
            Map<String, HumanComment> commentMapBefore = DeleteCommentRewriter.getPublishedComments(noteUtil, reader, NoteMap.read(reader, commitBefore));
            Map<String, HumanComment> commentMapAfter = DeleteCommentRewriter.getPublishedComments(noteUtil, reader, NoteMap.read(reader, commitAfter));
            if (commentMapBefore.containsKey(targetCommentUuid)) {
                assertThat(commentMapAfter).containsKey(targetCommentUuid);
                HumanComment comment = commentMapAfter.get(targetCommentUuid);
                assertThat(comment.message).isEqualTo(expectedMessage);
                comment.message = commentMapBefore.get(targetCommentUuid).message;
                commentMapAfter.put(targetCommentUuid, comment);
                assertThat(commentMapAfter).isEqualTo(commentMapBefore);
            } else {
                assertThat(commentMapAfter).doesNotContainKey(targetCommentUuid);
            }
            // Other metas should be exactly the same.
            assertThat(commitAfter.getFullMessage()).isEqualTo(commitBefore.getFullMessage());
            assertThat(commitAfter.getCommitterIdent()).isEqualTo(commitBefore.getCommitterIdent());
            assertThat(commitAfter.getAuthorIdent()).isEqualTo(commitBefore.getAuthorIdent());
            assertThat(commitAfter.getEncoding()).isEqualTo(commitBefore.getEncoding());
            assertThat(commitAfter.getEncodingName()).isEqualTo(commitBefore.getEncodingName());
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectReader(org.eclipse.jgit.lib.ObjectReader) IdString(com.google.gerrit.extensions.restapi.IdString) HumanComment(com.google.gerrit.entities.HumanComment) TestHumanComment(com.google.gerrit.acceptance.testsuite.change.TestHumanComment) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with HumanComment

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

the class AbstractChangeNotesTest method newComment.

protected HumanComment newComment(PatchSet.Id psId, String filename, String UUID, CommentRange range, int line, IdentifiedUser commenter, String parentUUID, Instant t, String message, short side, ObjectId commitId, boolean unresolved) {
    HumanComment c = new HumanComment(new Comment.Key(UUID, filename, psId.get()), commenter.getAccountId(), t, side, message, serverId, unresolved);
    c.lineNbr = line;
    c.parentUuid = parentUUID;
    c.setCommitId(commitId);
    c.setRange(range);
    return c;
}
Also used : Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) HumanComment(com.google.gerrit.entities.HumanComment)

Example 5 with HumanComment

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

the class ChangeNotesTest method patchLineCommentMultipleOnePatchsetMultipleFiles.

@Test
public void patchLineCommentMultipleOnePatchsetMultipleFiles() throws Exception {
    Change c = newChange();
    String uuid = "uuid";
    ObjectId commitId = ObjectId.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
    CommentRange range = new CommentRange(1, 1, 2, 1);
    PatchSet.Id psId = c.currentPatchSetId();
    String filename1 = "filename1";
    String filename2 = "filename2";
    short side = (short) 1;
    ChangeUpdate update = newUpdate(c, otherUser);
    Instant now = TimeUtil.now();
    HumanComment comment1 = newComment(psId, filename1, uuid, range, range.getEndLine(), otherUser, null, now, "comment 1", side, commitId, false);
    update.setPatchSetId(psId);
    update.putComment(HumanComment.Status.PUBLISHED, comment1);
    update.commit();
    update = newUpdate(c, otherUser);
    HumanComment comment2 = newComment(psId, filename2, uuid, range, range.getEndLine(), otherUser, null, now, "comment 2", side, commitId, false);
    update.setPatchSetId(psId);
    update.putComment(HumanComment.Status.PUBLISHED, comment2);
    update.commit();
    assertThat(newNotes(c).getHumanComments()).containsExactlyEntriesIn(ImmutableListMultimap.of(commitId, comment1, commitId, comment2)).inOrder();
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) Instant(java.time.Instant) CommentRange(com.google.gerrit.entities.CommentRange) PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) HumanComment(com.google.gerrit.entities.HumanComment) Test(org.junit.Test)

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