Search in sources :

Example 36 with HumanComment

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

the class CommentPorter method portSamePatchset.

private ImmutableList<HumanComment> portSamePatchset(Project.NameKey project, Change change, PatchSet originalPatchset, PatchSet targetPatchset, ImmutableList<HumanComment> comments) {
    try (TraceTimer ignored = TraceContext.newTimer("Porting comments same patchset", Metadata.builder().projectName(project.get()).changeId(change.getChangeId()).patchSetId(originalPatchset.number()).build())) {
        Map<Short, List<HumanComment>> commentsPerSide = comments.stream().collect(groupingBy(comment -> comment.side));
        ImmutableList.Builder<HumanComment> portedComments = ImmutableList.builder();
        for (Map.Entry<Short, List<HumanComment>> sideAndComments : commentsPerSide.entrySet()) {
            portedComments.addAll(portSamePatchsetAndSide(project, change, originalPatchset, targetPatchset, sideAndComments.getValue(), sideAndComments.getKey()));
        }
        return portedComments.build();
    }
}
Also used : Patch(com.google.gerrit.entities.Patch) Inject(com.google.inject.Inject) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Mapping(com.google.gerrit.server.patch.GitPositionTransformer.Mapping) HumanComment(com.google.gerrit.entities.HumanComment) CommentsUtil(com.google.gerrit.server.CommentsUtil) Position(com.google.gerrit.server.patch.GitPositionTransformer.Position) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) Function(java.util.function.Function) BestPositionOnConflict(com.google.gerrit.server.patch.GitPositionTransformer.BestPositionOnConflict) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) ImmutableList(com.google.common.collect.ImmutableList) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Description(com.google.gerrit.metrics.Description) Map(java.util.Map) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) Metadata(com.google.gerrit.server.logging.Metadata) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TraceContext(com.google.gerrit.server.logging.TraceContext) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DiffMappings(com.google.gerrit.server.patch.DiffMappings) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ObjectId(org.eclipse.jgit.lib.ObjectId) DiffOptions(com.google.gerrit.server.patch.DiffOptions) TaggedEdit(com.google.gerrit.server.patch.filediff.TaggedEdit) List(java.util.List) FileEdits(com.google.gerrit.server.patch.filediff.FileEdits) Stream(java.util.stream.Stream) GitPositionTransformer(com.google.gerrit.server.patch.GitPositionTransformer) DiffOperations(com.google.gerrit.server.patch.DiffOperations) FileMapping(com.google.gerrit.server.patch.GitPositionTransformer.FileMapping) Project(com.google.gerrit.entities.Project) PositionedEntity(com.google.gerrit.server.patch.GitPositionTransformer.PositionedEntity) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Counter0(com.google.gerrit.metrics.Counter0) MetricMaker(com.google.gerrit.metrics.MetricMaker) FluentLogger(com.google.common.flogger.FluentLogger) Range(com.google.gerrit.entities.Comment.Range) Singleton(com.google.inject.Singleton) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) HumanComment(com.google.gerrit.entities.HumanComment) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 37 with HumanComment

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

the class CommentPorter method portSamePatchsetAndSide.

private ImmutableList<HumanComment> portSamePatchsetAndSide(Project.NameKey project, Change change, PatchSet originalPatchset, PatchSet targetPatchset, List<HumanComment> comments, short side) {
    try (TraceTimer ignored = TraceContext.newTimer("Porting comments same patchset and side", Metadata.builder().projectName(project.get()).changeId(change.getChangeId()).patchSetId(originalPatchset.number()).commentSide(side).build())) {
        ImmutableSet<Mapping> mappings;
        try {
            mappings = loadMappings(project, change, originalPatchset, targetPatchset, side);
        } catch (Exception e) {
            logger.atWarning().withCause(e).log("Could not determine some necessary diff mappings for porting comments on change %s" + " from patchset %s to patchset %s. Mapping %d affected comments to the fallback" + " destination.", change.getChangeId(), originalPatchset.id().getId(), targetPatchset.id().getId(), comments.size());
            mappings = getFallbackMappings(comments);
        }
        ImmutableList<PositionedEntity<HumanComment>> positionedComments = comments.stream().map(this::toPositionedEntity).collect(toImmutableList());
        ImmutableMap<PositionedEntity<HumanComment>, HumanComment> origToPortedMap = positionTransformer.transform(positionedComments, mappings).stream().collect(ImmutableMap.toImmutableMap(Function.identity(), PositionedEntity::getEntityAtUpdatedPosition));
        collectMetrics(origToPortedMap);
        return ImmutableList.copyOf(origToPortedMap.values());
    }
}
Also used : PositionedEntity(com.google.gerrit.server.patch.GitPositionTransformer.PositionedEntity) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) Mapping(com.google.gerrit.server.patch.GitPositionTransformer.Mapping) FileMapping(com.google.gerrit.server.patch.GitPositionTransformer.FileMapping) HumanComment(com.google.gerrit.entities.HumanComment) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException)

Example 38 with HumanComment

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

the class CommentPorter method port.

private ImmutableList<HumanComment> port(ChangeNotes notes, PatchSet targetPatchset, List<HumanComment> comments) {
    Map<Integer, ImmutableList<HumanComment>> commentsPerPatchset = comments.stream().collect(groupingBy(comment -> comment.key.patchSetId, toImmutableList()));
    ImmutableList.Builder<HumanComment> portedComments = ImmutableList.builderWithExpectedSize(comments.size());
    for (Integer originalPatchsetId : commentsPerPatchset.keySet()) {
        ImmutableList<HumanComment> patchsetComments = commentsPerPatchset.get(originalPatchsetId);
        PatchSet originalPatchset = notes.getPatchSets().get(PatchSet.id(notes.getChangeId(), originalPatchsetId));
        if (originalPatchset != null) {
            portedComments.addAll(portSamePatchset(notes.getProjectName(), notes.getChange(), originalPatchset, targetPatchset, patchsetComments));
        } else {
            logger.atWarning().log("Some comments which should be ported refer to the non-existent patchset %s of" + " change %d. Omitting %d affected comments.", originalPatchsetId, notes.getChangeId().get(), patchsetComments.size());
        }
    }
    return portedComments.build();
}
Also used : Patch(com.google.gerrit.entities.Patch) Inject(com.google.inject.Inject) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Mapping(com.google.gerrit.server.patch.GitPositionTransformer.Mapping) HumanComment(com.google.gerrit.entities.HumanComment) CommentsUtil(com.google.gerrit.server.CommentsUtil) Position(com.google.gerrit.server.patch.GitPositionTransformer.Position) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) Function(java.util.function.Function) BestPositionOnConflict(com.google.gerrit.server.patch.GitPositionTransformer.BestPositionOnConflict) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) ImmutableList(com.google.common.collect.ImmutableList) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Description(com.google.gerrit.metrics.Description) Map(java.util.Map) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) Metadata(com.google.gerrit.server.logging.Metadata) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TraceContext(com.google.gerrit.server.logging.TraceContext) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DiffMappings(com.google.gerrit.server.patch.DiffMappings) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ObjectId(org.eclipse.jgit.lib.ObjectId) DiffOptions(com.google.gerrit.server.patch.DiffOptions) TaggedEdit(com.google.gerrit.server.patch.filediff.TaggedEdit) List(java.util.List) FileEdits(com.google.gerrit.server.patch.filediff.FileEdits) Stream(java.util.stream.Stream) GitPositionTransformer(com.google.gerrit.server.patch.GitPositionTransformer) DiffOperations(com.google.gerrit.server.patch.DiffOperations) FileMapping(com.google.gerrit.server.patch.GitPositionTransformer.FileMapping) Project(com.google.gerrit.entities.Project) PositionedEntity(com.google.gerrit.server.patch.GitPositionTransformer.PositionedEntity) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Counter0(com.google.gerrit.metrics.Counter0) MetricMaker(com.google.gerrit.metrics.MetricMaker) FluentLogger(com.google.common.flogger.FluentLogger) Range(com.google.gerrit.entities.Comment.Range) Singleton(com.google.inject.Singleton) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) PatchSet(com.google.gerrit.entities.PatchSet) HumanComment(com.google.gerrit.entities.HumanComment)

Example 39 with HumanComment

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

the class CommentPorter method createCommentAtNewPosition.

private static HumanComment createCommentAtNewPosition(HumanComment originalComment, Position newPosition) {
    HumanComment portedComment = new HumanComment(originalComment);
    portedComment.key.filename = newPosition.filePath().orElse(Patch.PATCHSET_LEVEL);
    if (portedComment.range != null && newPosition.lineRange().isPresent()) {
        // Comment was a range comment and also stayed one.
        portedComment.range = toRange(newPosition.lineRange().get(), portedComment.range.startChar, portedComment.range.endChar);
        portedComment.lineNbr = portedComment.range.endLine;
    } else {
        portedComment.range = null;
        // No line -> use 0 = file comment or any other comment type without an explicit line.
        portedComment.lineNbr = newPosition.lineRange().map(range -> range.start() + 1).orElse(0);
    }
    if (Patch.PATCHSET_LEVEL.equals(portedComment.key.filename)) {
        // Correct the side of the comment to Side.REVISION (= 1) if the comment was changed to
        // patchset level.
        portedComment.side = 1;
    }
    return portedComment;
}
Also used : HumanComment(com.google.gerrit.entities.HumanComment)

Example 40 with HumanComment

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

the class DeleteComment method apply.

@Override
public Response<CommentInfo> apply(HumanCommentResource rsrc, DeleteCommentInput input) throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException, UpdateException {
    CurrentUser user = userProvider.get();
    permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
    if (input == null) {
        input = new DeleteCommentInput();
    }
    String newMessage = getCommentNewMessage(user.asIdentifiedUser().getName(), input.reason);
    DeleteCommentOp deleteCommentOp = new DeleteCommentOp(rsrc, newMessage);
    try (BatchUpdate batchUpdate = updateFactory.create(rsrc.getRevisionResource().getProject(), user, TimeUtil.now())) {
        batchUpdate.addOp(rsrc.getRevisionResource().getChange().getId(), deleteCommentOp).execute();
    }
    ChangeNotes updatedNotes = notesFactory.createChecked(rsrc.getRevisionResource().getProject(), rsrc.getRevisionResource().getChangeResource().getId());
    List<HumanComment> changeComments = commentsUtil.publishedHumanCommentsByChange(updatedNotes);
    Optional<HumanComment> updatedComment = changeComments.stream().filter(c -> c.key.equals(rsrc.getComment().key)).findFirst();
    if (!updatedComment.isPresent()) {
        // This should not happen as this endpoint should not remove the whole comment.
        throw new ResourceNotFoundException("comment not found: " + rsrc.getComment().key);
    }
    return Response.ok(commentJson.get().newHumanCommentFormatter().format(updatedComment.get()));
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) CommentInfo(com.google.gerrit.extensions.common.CommentInfo) Inject(com.google.inject.Inject) HumanComment(com.google.gerrit.entities.HumanComment) CommentsUtil(com.google.gerrit.server.CommentsUtil) Response(com.google.gerrit.extensions.restapi.Response) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) UpdateException(com.google.gerrit.server.update.UpdateException) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) Strings(com.google.common.base.Strings) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) PatchSet(com.google.gerrit.entities.PatchSet) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeContext(com.google.gerrit.server.update.ChangeContext) GlobalPermission(com.google.gerrit.server.permissions.GlobalPermission) CurrentUser(com.google.gerrit.server.CurrentUser) DeleteCommentInput(com.google.gerrit.extensions.api.changes.DeleteCommentInput) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) Provider(com.google.inject.Provider) List(java.util.List) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Optional(java.util.Optional) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) HumanCommentResource(com.google.gerrit.server.change.HumanCommentResource) Singleton(com.google.inject.Singleton) CurrentUser(com.google.gerrit.server.CurrentUser) DeleteCommentInput(com.google.gerrit.extensions.api.changes.DeleteCommentInput) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) HumanComment(com.google.gerrit.entities.HumanComment) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

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