Search in sources :

Example 6 with Comment

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

the class CommentsUtil method newHumanComment.

public HumanComment newHumanComment(ChangeNotes changeNotes, CurrentUser currentUser, Instant when, String path, PatchSet.Id psId, short side, String message, @Nullable Boolean unresolved, @Nullable String parentUuid) {
    if (unresolved == null) {
        if (parentUuid == null) {
            // Default to false if comment is not descended from another.
            unresolved = false;
        } else {
            // Inherit unresolved value from inReplyTo comment if not specified.
            Comment.Key key = new Comment.Key(parentUuid, path, psId.get());
            Optional<HumanComment> parent = getPublishedHumanComment(changeNotes, key);
            // If the comment was not found, it is descended from a robot comment, or the UUID is
            // invalid. Either way, we use the default.
            unresolved = parent.map(p -> p.unresolved).orElse(false);
        }
    }
    HumanComment c = new HumanComment(new Comment.Key(ChangeUtil.messageUuid(), path, psId.get()), currentUser.getAccountId(), when, side, message, serverId, unresolved);
    c.parentUuid = parentUuid;
    currentUser.updateRealAccountId(c::setRealAuthor);
    return c;
}
Also used : Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) RobotComment(com.google.gerrit.entities.RobotComment) HumanComment(com.google.gerrit.entities.HumanComment)

Example 7 with Comment

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

the class CommentContextLoader method getContext.

/**
 * Load the comment context for multiple contextInputs at once. This method will open the
 * repository and read the source files for all necessary contextInputs' file paths.
 *
 * @param contextInputs a list of contextInputs.
 * @return a Map where all entries consist of the input contextInputs and the values are their
 *     corresponding {@link CommentContext}.
 */
public Map<ContextInput, CommentContext> getContext(Collection<ContextInput> contextInputs) throws IOException {
    ImmutableMap.Builder<ContextInput, CommentContext> result = ImmutableMap.builderWithExpectedSize(Iterables.size(contextInputs));
    // Group contextInputs by commit ID so that each commit is parsed only once
    Map<ObjectId, List<ContextInput>> commentsByCommitId = contextInputs.stream().collect(groupingBy(ContextInput::commitId));
    try (Repository repo = repoManager.openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        for (ObjectId commitId : commentsByCommitId.keySet()) {
            RevCommit commit;
            try {
                commit = rw.parseCommit(commitId);
            } catch (IncorrectObjectTypeException | MissingObjectException e) {
                logger.atWarning().log("Commit %s is missing or has an incorrect object type", commitId);
                commentsByCommitId.get(commitId).forEach(contextInput -> result.put(contextInput, CommentContext.empty()));
                continue;
            }
            for (ContextInput contextInput : commentsByCommitId.get(commitId)) {
                Optional<Range> range = getStartAndEndLines(contextInput);
                if (!range.isPresent()) {
                    result.put(contextInput, CommentContext.empty());
                    continue;
                }
                String filePath = contextInput.filePath();
                switch(filePath) {
                    case COMMIT_MSG:
                        result.put(contextInput, getContextForCommitMessage(rw.getObjectReader(), commit, range.get(), contextInput.contextPadding()));
                        break;
                    case MERGE_LIST:
                        result.put(contextInput, getContextForMergeList(rw.getObjectReader(), commit, range.get(), contextInput.contextPadding()));
                        break;
                    default:
                        result.put(contextInput, getContextForFilePath(repo, rw, commit, filePath, range.get(), contextInput.contextPadding()));
                }
            }
        }
        return result.build();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) ComparisonType(com.google.gerrit.server.patch.ComparisonType) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ProjectCache(com.google.gerrit.server.project.ProjectCache) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) SrcContentResolver(com.google.gerrit.server.patch.SrcContentResolver) Inject(com.google.inject.Inject) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) MERGE_LIST(com.google.gerrit.entities.Patch.MERGE_LIST) Comment(com.google.gerrit.entities.Comment) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) MimeType(eu.medsea.mimeutil.MimeType) Assisted(com.google.inject.assistedinject.Assisted) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PatchScript(com.google.gerrit.common.data.PatchScript) ContextLineInfo(com.google.gerrit.extensions.common.ContextLineInfo) FileContentUtil(com.google.gerrit.server.change.FileContentUtil) Map(java.util.Map) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) ImmutableMap(com.google.common.collect.ImmutableMap) Text(com.google.gerrit.server.patch.Text) Collection(java.util.Collection) FileTypeRegistry(com.google.gerrit.server.mime.FileTypeRegistry) ProjectState(com.google.gerrit.server.project.ProjectState) IOException(java.io.IOException) CommentContext(com.google.gerrit.entities.CommentContext) COMMIT_MSG(com.google.gerrit.entities.Patch.COMMIT_MSG) ObjectId(org.eclipse.jgit.lib.ObjectId) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) AutoValue(com.google.auto.value.AutoValue) Project(com.google.gerrit.entities.Project) Optional(java.util.Optional) MimeUtil2(eu.medsea.mimeutil.MimeUtil2) ObjectReader(org.eclipse.jgit.lib.ObjectReader) FluentLogger(com.google.common.flogger.FluentLogger) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) CommentContext(com.google.gerrit.entities.CommentContext) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ImmutableMap(com.google.common.collect.ImmutableMap) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Repository(org.eclipse.jgit.lib.Repository) List(java.util.List) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 8 with Comment

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

the class CommentSender method getGroupedInlineComments.

/**
 * Returns a list of FileCommentGroup objects representing the inline comments grouped by the
 * file.
 */
private List<CommentSender.FileCommentGroup> getGroupedInlineComments(Repository repo) {
    List<CommentSender.FileCommentGroup> groups = new ArrayList<>();
    // Loop over the comments and collect them into groups based on the file
    // location of the comment.
    FileCommentGroup currentGroup = null;
    for (Comment c : inlineComments) {
        // If it's a new group:
        if (currentGroup == null || !c.key.filename.equals(currentGroup.filename) || c.key.patchSetId != currentGroup.patchSetId) {
            currentGroup = new FileCommentGroup();
            currentGroup.filename = c.key.filename;
            currentGroup.patchSetId = c.key.patchSetId;
            // Get the modified files:
            Map<String, FileDiffOutput> modifiedFiles = null;
            try {
                modifiedFiles = listModifiedFiles(c.key.patchSetId);
            } catch (DiffNotAvailableException e) {
                logger.atSevere().withCause(e).log("Failed to get modified files");
            }
            groups.add(currentGroup);
            if (modifiedFiles != null && !modifiedFiles.isEmpty()) {
                try {
                    currentGroup.fileData = new PatchFile(repo, modifiedFiles, c.key.filename);
                } catch (IOException e) {
                    logger.atWarning().withCause(e).log("Cannot load %s from %s in %s", c.key.filename, modifiedFiles.values().iterator().next().newCommitId().name(), projectState.getName());
                    currentGroup.fileData = null;
                }
            }
        }
        if (currentGroup.filename.equals(PATCHSET_LEVEL) || currentGroup.fileData != null) {
            currentGroup.comments.add(c);
        }
    }
    groups.sort(Comparator.comparing(g -> g.filename, FilenameComparator.INSTANCE));
    return groups;
}
Also used : Patch(com.google.gerrit.entities.Patch) EmailException(com.google.gerrit.exceptions.EmailException) Protocol(com.google.gerrit.server.mail.receive.Protocol) Inject(com.google.inject.Inject) ZonedDateTime(java.time.ZonedDateTime) Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) CommentsUtil(com.google.gerrit.server.CommentsUtil) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Assisted(com.google.inject.assistedinject.Assisted) HashSet(java.util.HashSet) PATCHSET_LEVEL(com.google.gerrit.entities.Patch.PATCHSET_LEVEL) Strings(com.google.common.base.Strings) Config(org.eclipse.jgit.lib.Config) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Map(java.util.Map) RobotComment(com.google.gerrit.entities.RobotComment) NoSuchEntityException(com.google.gerrit.exceptions.NoSuchEntityException) Change(com.google.gerrit.entities.Change) FieldName(org.apache.james.mime4j.dom.field.FieldName) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) StorageException(com.google.gerrit.exceptions.StorageException) Account(com.google.gerrit.entities.Account) MailHeader(com.google.gerrit.mail.MailHeader) IOException(java.io.IOException) ZoneId(java.time.ZoneId) NotifyHandling(com.google.gerrit.extensions.api.changes.NotifyHandling) Collectors.toList(java.util.stream.Collectors.toList) FilenameComparator(com.google.gerrit.common.data.FilenameComparator) List(java.util.List) NotifyType(com.google.gerrit.entities.NotifyConfig.NotifyType) Project(com.google.gerrit.entities.Project) MailProcessingUtil(com.google.gerrit.mail.MailProcessingUtil) LabelVote(com.google.gerrit.server.util.LabelVote) Optional(java.util.Optional) PatchFile(com.google.gerrit.server.patch.PatchFile) Comparator(java.util.Comparator) Collections(java.util.Collections) FluentLogger(com.google.common.flogger.FluentLogger) Repository(org.eclipse.jgit.lib.Repository) Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) RobotComment(com.google.gerrit.entities.RobotComment) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) PatchFile(com.google.gerrit.server.patch.PatchFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput)

Example 9 with Comment

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

the class ChangeNotes method getCommentKeys.

public ImmutableSet<Comment.Key> getCommentKeys() {
    if (commentKeys == null) {
        ImmutableSet.Builder<Comment.Key> b = ImmutableSet.builder();
        for (Comment c : getHumanComments().values()) {
            b.add(new Comment.Key(c.key));
        }
        commentKeys = b.build();
    }
    return commentKeys;
}
Also used : Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) RobotComment(com.google.gerrit.entities.RobotComment) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BranchNameKey(com.google.gerrit.entities.BranchNameKey)

Example 10 with Comment

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

the class CommentThreadTest method threadCanBeUnresolved.

@Test
public void threadCanBeUnresolved() {
    HumanComment root = unresolved(createComment("root"));
    CommentThread<Comment> commentThread = CommentThread.builder().addComment(root).build();
    assertThat(commentThread.unresolved()).isTrue();
}
Also used : Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) HumanComment(com.google.gerrit.entities.HumanComment) Test(org.junit.Test)

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