LCOV - code coverage report
Current view: top level - server/restapi/change - CreateDraftComment.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 47 49 95.9 %
Date: 2022-11-19 15:00:39 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2012 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.restapi.change;
      16             : 
      17             : import static com.google.gerrit.entities.Patch.PATCHSET_LEVEL;
      18             : 
      19             : import com.google.common.base.Strings;
      20             : import com.google.gerrit.entities.HumanComment;
      21             : import com.google.gerrit.entities.PatchSet;
      22             : import com.google.gerrit.extensions.api.changes.DraftInput;
      23             : import com.google.gerrit.extensions.common.CommentInfo;
      24             : import com.google.gerrit.extensions.restapi.BadRequestException;
      25             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      26             : import com.google.gerrit.extensions.restapi.Response;
      27             : import com.google.gerrit.extensions.restapi.RestApiException;
      28             : import com.google.gerrit.extensions.restapi.RestModifyView;
      29             : import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
      30             : import com.google.gerrit.extensions.restapi.Url;
      31             : import com.google.gerrit.server.CommentsUtil;
      32             : import com.google.gerrit.server.PatchSetUtil;
      33             : import com.google.gerrit.server.change.RevisionResource;
      34             : import com.google.gerrit.server.permissions.PermissionBackendException;
      35             : import com.google.gerrit.server.update.BatchUpdate;
      36             : import com.google.gerrit.server.update.BatchUpdateOp;
      37             : import com.google.gerrit.server.update.ChangeContext;
      38             : import com.google.gerrit.server.update.UpdateException;
      39             : import com.google.gerrit.server.util.time.TimeUtil;
      40             : import com.google.inject.Inject;
      41             : import com.google.inject.Provider;
      42             : import com.google.inject.Singleton;
      43             : import java.util.Collections;
      44             : 
      45             : @Singleton
      46             : public class CreateDraftComment implements RestModifyView<RevisionResource, DraftInput> {
      47             :   private final BatchUpdate.Factory updateFactory;
      48             :   private final Provider<CommentJson> commentJson;
      49             :   private final CommentsUtil commentsUtil;
      50             :   private final PatchSetUtil psUtil;
      51             : 
      52             :   @Inject
      53             :   CreateDraftComment(
      54             :       BatchUpdate.Factory updateFactory,
      55             :       Provider<CommentJson> commentJson,
      56             :       CommentsUtil commentsUtil,
      57         145 :       PatchSetUtil psUtil) {
      58         145 :     this.updateFactory = updateFactory;
      59         145 :     this.commentJson = commentJson;
      60         145 :     this.commentsUtil = commentsUtil;
      61         145 :     this.psUtil = psUtil;
      62         145 :   }
      63             : 
      64             :   @Override
      65             :   public Response<CommentInfo> apply(RevisionResource rsrc, DraftInput in)
      66             :       throws RestApiException, UpdateException, PermissionBackendException {
      67          18 :     if (Strings.isNullOrEmpty(in.path)) {
      68           1 :       throw new BadRequestException("path must be non-empty");
      69          18 :     } else if (in.message == null || in.message.trim().isEmpty()) {
      70           0 :       throw new BadRequestException("message must be non-empty");
      71          18 :     } else if (in.path.equals(PATCHSET_LEVEL)
      72             :         && (in.side != null || in.range != null || in.line != null)) {
      73           1 :       throw new BadRequestException("patchset-level comments can't have side, range, or line");
      74          18 :     } else if (in.line != null && in.line < 0) {
      75           1 :       throw new BadRequestException("line must be >= 0");
      76          18 :     } else if (in.line != null && in.range != null && in.line != in.range.endLine) {
      77           1 :       throw new BadRequestException("range endLine must be on the same line as the comment");
      78          18 :     } else if (in.inReplyTo != null
      79           3 :         && !commentsUtil.getPublishedHumanComment(rsrc.getNotes(), in.inReplyTo).isPresent()
      80           1 :         && !commentsUtil.getRobotComment(rsrc.getNotes(), in.inReplyTo).isPresent()) {
      81           1 :       throw new BadRequestException(
      82           1 :           String.format("Invalid inReplyTo, comment %s not found", in.inReplyTo));
      83             :     }
      84             : 
      85          18 :     try (BatchUpdate bu = updateFactory.create(rsrc.getProject(), rsrc.getUser(), TimeUtil.now())) {
      86          18 :       Op op = new Op(rsrc.getPatchSet().id(), in);
      87          18 :       bu.addOp(rsrc.getChange().getId(), op);
      88          18 :       bu.execute();
      89          18 :       return Response.created(
      90          18 :           commentJson.get().setFillAccounts(false).newHumanCommentFormatter().format(op.comment));
      91             :     }
      92             :   }
      93             : 
      94             :   private class Op implements BatchUpdateOp {
      95             :     private final PatchSet.Id psId;
      96             :     private final DraftInput in;
      97             : 
      98             :     private HumanComment comment;
      99             : 
     100          18 :     private Op(PatchSet.Id psId, DraftInput in) {
     101          18 :       this.psId = psId;
     102          18 :       this.in = in;
     103          18 :     }
     104             : 
     105             :     @Override
     106             :     public boolean updateChange(ChangeContext ctx)
     107             :         throws ResourceNotFoundException, UnprocessableEntityException {
     108          18 :       PatchSet ps = psUtil.get(ctx.getNotes(), psId);
     109          18 :       if (ps == null) {
     110           0 :         throw new ResourceNotFoundException("patch set not found: " + psId);
     111             :       }
     112          18 :       String parentUuid = Url.decode(in.inReplyTo);
     113             : 
     114          18 :       comment =
     115          18 :           commentsUtil.newHumanComment(
     116          18 :               ctx.getNotes(),
     117          18 :               ctx.getUser(),
     118          18 :               ctx.getWhen(),
     119             :               in.path,
     120          18 :               ps.id(),
     121          18 :               in.side(),
     122          18 :               in.message.trim(),
     123             :               in.unresolved,
     124             :               parentUuid);
     125          18 :       comment.setLineNbrAndRange(in.line, in.range);
     126          18 :       comment.tag = in.tag;
     127             : 
     128          18 :       commentsUtil.setCommentCommitId(comment, ctx.getChange(), ps);
     129             : 
     130          18 :       commentsUtil.putHumanComments(
     131          18 :           ctx.getUpdate(psId), HumanComment.Status.DRAFT, Collections.singleton(comment));
     132          18 :       return true;
     133             :     }
     134             :   }
     135             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750