Search in sources :

Example 1 with HumanCommentResource

use of com.google.gerrit.server.change.HumanCommentResource 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)

Example 2 with HumanCommentResource

use of com.google.gerrit.server.change.HumanCommentResource in project gerrit by GerritCodeReview.

the class Comments method parse.

@Override
public HumanCommentResource parse(RevisionResource rev, IdString id) throws ResourceNotFoundException {
    String uuid = id.get();
    ChangeNotes notes = rev.getNotes();
    for (HumanComment c : commentsUtil.publishedByPatchSet(notes, rev.getPatchSet().id())) {
        if (uuid.equals(c.key.uuid)) {
            return new HumanCommentResource(rev, c);
        }
    }
    throw new ResourceNotFoundException(id);
}
Also used : HumanCommentResource(com.google.gerrit.server.change.HumanCommentResource) IdString(com.google.gerrit.extensions.restapi.IdString) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) HumanComment(com.google.gerrit.entities.HumanComment)

Aggregations

HumanComment (com.google.gerrit.entities.HumanComment)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 HumanCommentResource (com.google.gerrit.server.change.HumanCommentResource)2 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)2 Strings (com.google.common.base.Strings)1 PatchSet (com.google.gerrit.entities.PatchSet)1 DeleteCommentInput (com.google.gerrit.extensions.api.changes.DeleteCommentInput)1 CommentInfo (com.google.gerrit.extensions.common.CommentInfo)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 Response (com.google.gerrit.extensions.restapi.Response)1 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)1 RestModifyView (com.google.gerrit.extensions.restapi.RestModifyView)1 CommentsUtil (com.google.gerrit.server.CommentsUtil)1 CurrentUser (com.google.gerrit.server.CurrentUser)1 GlobalPermission (com.google.gerrit.server.permissions.GlobalPermission)1 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)1 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)1 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)1 BatchUpdateOp (com.google.gerrit.server.update.BatchUpdateOp)1