LCOV - code coverage report
Current view: top level - server/restapi/change - ListChangeDrafts.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 25 26 96.2 %
Date: 2022-11-19 15:00:39 Functions: 7 7 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2015 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 com.google.gerrit.entities.HumanComment;
      18             : import com.google.gerrit.extensions.common.CommentInfo;
      19             : import com.google.gerrit.extensions.common.ContextLineInfo;
      20             : import com.google.gerrit.extensions.restapi.AuthException;
      21             : import com.google.gerrit.extensions.restapi.Response;
      22             : import com.google.gerrit.extensions.restapi.RestReadView;
      23             : import com.google.gerrit.server.CommentsUtil;
      24             : import com.google.gerrit.server.change.ChangeResource;
      25             : import com.google.gerrit.server.permissions.PermissionBackendException;
      26             : import com.google.gerrit.server.query.change.ChangeData;
      27             : import com.google.gerrit.server.restapi.change.CommentJson.HumanCommentFormatter;
      28             : import com.google.inject.Inject;
      29             : import com.google.inject.Provider;
      30             : import java.util.List;
      31             : import java.util.Map;
      32             : import org.kohsuke.args4j.Option;
      33             : 
      34             : public class ListChangeDrafts implements RestReadView<ChangeResource> {
      35             :   private final ChangeData.Factory changeDataFactory;
      36             :   private final Provider<CommentJson> commentJson;
      37             :   private final CommentsUtil commentsUtil;
      38             : 
      39             :   private boolean includeContext;
      40             :   private int contextPadding;
      41             : 
      42             :   /**
      43             :    * Optional parameter. If set, the contextLines field of the {@link ContextLineInfo} of the
      44             :    * response will contain the lines of the source file where the comment was written.
      45             :    *
      46             :    * @param context If true, comment context will be attached to the response
      47             :    */
      48             :   @Option(name = "--enable-context")
      49             :   public void setContext(boolean context) {
      50           7 :     this.includeContext = context;
      51           7 :   }
      52             : 
      53             :   /**
      54             :    * Optional parameter. Works only if {@link #includeContext} is set to true. If {@link
      55             :    * #contextPadding} is set, the context lines in the response will be padded with {@link
      56             :    * #contextPadding} extra lines before and after the comment range.
      57             :    */
      58             :   @Option(name = "--context-padding")
      59             :   public void setContextPadding(int contextPadding) {
      60           7 :     this.contextPadding = contextPadding;
      61           7 :   }
      62             : 
      63             :   @Inject
      64             :   ListChangeDrafts(
      65             :       ChangeData.Factory changeDataFactory,
      66             :       Provider<CommentJson> commentJson,
      67          57 :       CommentsUtil commentsUtil) {
      68          57 :     this.changeDataFactory = changeDataFactory;
      69          57 :     this.commentJson = commentJson;
      70          57 :     this.commentsUtil = commentsUtil;
      71          57 :   }
      72             : 
      73             :   private Iterable<HumanComment> listComments(ChangeResource rsrc) {
      74           8 :     ChangeData cd = changeDataFactory.create(rsrc.getNotes());
      75           8 :     return commentsUtil.draftByChangeAuthor(cd.notes(), rsrc.getUser().getAccountId());
      76             :   }
      77             : 
      78             :   @Override
      79             :   public Response<Map<String, List<CommentInfo>>> apply(ChangeResource rsrc)
      80             :       throws AuthException, PermissionBackendException {
      81           7 :     if (!rsrc.getUser().isIdentifiedUser()) {
      82           0 :       throw new AuthException("Authentication required");
      83             :     }
      84           7 :     return Response.ok(getCommentFormatter(rsrc).format(listComments(rsrc)));
      85             :   }
      86             : 
      87             :   public List<CommentInfo> getComments(ChangeResource rsrc)
      88             :       throws AuthException, PermissionBackendException {
      89           2 :     if (!rsrc.getUser().isIdentifiedUser()) {
      90           1 :       throw new AuthException("Authentication required");
      91             :     }
      92           2 :     return getCommentFormatter(rsrc).formatAsList(listComments(rsrc));
      93             :   }
      94             : 
      95             :   private HumanCommentFormatter getCommentFormatter(ChangeResource rsrc) {
      96           8 :     return commentJson
      97           8 :         .get()
      98           8 :         .setFillAccounts(false)
      99           8 :         .setFillPatchSet(true)
     100           8 :         .setFillCommentContext(includeContext)
     101           8 :         .setContextPadding(contextPadding)
     102           8 :         .setProjectKey(rsrc.getProject())
     103           8 :         .setChangeId(rsrc.getId())
     104           8 :         .newHumanCommentFormatter();
     105             :   }
     106             : }

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