LCOV - code coverage report
Current view: top level - server/restapi/change - ListChangeComments.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 33 33 100.0 %
Date: 2022-11-19 15:00:39 Functions: 9 9 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 static java.util.stream.Collectors.toList;
      18             : 
      19             : import com.google.common.collect.ImmutableList;
      20             : import com.google.gerrit.entities.ChangeMessage;
      21             : import com.google.gerrit.entities.HumanComment;
      22             : import com.google.gerrit.extensions.common.CommentInfo;
      23             : import com.google.gerrit.extensions.common.ContextLineInfo;
      24             : import com.google.gerrit.extensions.restapi.AuthException;
      25             : import com.google.gerrit.extensions.restapi.Response;
      26             : import com.google.gerrit.extensions.restapi.RestReadView;
      27             : import com.google.gerrit.server.ChangeMessagesUtil;
      28             : import com.google.gerrit.server.CommentsUtil;
      29             : import com.google.gerrit.server.change.ChangeResource;
      30             : import com.google.gerrit.server.permissions.PermissionBackendException;
      31             : import com.google.gerrit.server.query.change.ChangeData;
      32             : import com.google.inject.Inject;
      33             : import com.google.inject.Provider;
      34             : import java.util.List;
      35             : import java.util.Map;
      36             : import org.kohsuke.args4j.Option;
      37             : 
      38             : public class ListChangeComments implements RestReadView<ChangeResource> {
      39             :   private final ChangeMessagesUtil changeMessagesUtil;
      40             :   private final ChangeData.Factory changeDataFactory;
      41             :   private final Provider<CommentJson> commentJson;
      42             :   private final CommentsUtil commentsUtil;
      43             : 
      44             :   private boolean includeContext;
      45             :   private int contextPadding;
      46             : 
      47             :   /**
      48             :    * Optional parameter. If set, the contextLines field of the {@link ContextLineInfo} of the
      49             :    * response will contain the lines of the source file where the comment was written.
      50             :    *
      51             :    * @param context If true, comment context will be attached to the response
      52             :    */
      53             :   @Option(name = "--enable-context")
      54             :   public void setContext(boolean context) {
      55          11 :     this.includeContext = context;
      56          11 :   }
      57             : 
      58             :   /**
      59             :    * Optional parameter. Works only if {@link #includeContext} is set to true. If {@link
      60             :    * #contextPadding} is set, the context lines in the response will be padded with {@link
      61             :    * #contextPadding} extra lines before and after the comment range.
      62             :    */
      63             :   @Option(name = "--context-padding")
      64             :   public void setContextPadding(int contextPadding) {
      65          11 :     this.contextPadding = contextPadding;
      66          11 :   }
      67             : 
      68             :   @Inject
      69             :   ListChangeComments(
      70             :       ChangeData.Factory changeDataFactory,
      71             :       Provider<CommentJson> commentJson,
      72             :       CommentsUtil commentsUtil,
      73          58 :       ChangeMessagesUtil changeMessagesUtil) {
      74          58 :     this.changeDataFactory = changeDataFactory;
      75          58 :     this.commentJson = commentJson;
      76          58 :     this.commentsUtil = commentsUtil;
      77          58 :     this.changeMessagesUtil = changeMessagesUtil;
      78          58 :   }
      79             : 
      80             :   @Override
      81             :   public Response<Map<String, List<CommentInfo>>> apply(ChangeResource rsrc)
      82             :       throws AuthException, PermissionBackendException {
      83          11 :     return Response.ok(getAsMap(listComments(rsrc), rsrc));
      84             :   }
      85             : 
      86             :   public List<CommentInfo> getComments(ChangeResource rsrc) throws PermissionBackendException {
      87           2 :     return getAsList(listComments(rsrc), rsrc);
      88             :   }
      89             : 
      90             :   private Iterable<HumanComment> listComments(ChangeResource rsrc) {
      91          12 :     ChangeData cd = changeDataFactory.create(rsrc.getNotes());
      92          12 :     return commentsUtil.publishedHumanCommentsByChange(cd.notes());
      93             :   }
      94             : 
      95             :   private ImmutableList<CommentInfo> getAsList(Iterable<HumanComment> comments, ChangeResource rsrc)
      96             :       throws PermissionBackendException {
      97           2 :     ImmutableList<CommentInfo> commentInfos = getCommentFormatter(rsrc).formatAsList(comments);
      98           2 :     List<ChangeMessage> changeMessages = changeMessagesUtil.byChange(rsrc.getNotes());
      99           2 :     CommentsUtil.linkCommentsToChangeMessages(commentInfos, changeMessages, true);
     100           2 :     return commentInfos;
     101             :   }
     102             : 
     103             :   private Map<String, List<CommentInfo>> getAsMap(
     104             :       Iterable<HumanComment> comments, ChangeResource rsrc) throws PermissionBackendException {
     105          11 :     Map<String, List<CommentInfo>> commentInfosMap = getCommentFormatter(rsrc).format(comments);
     106          11 :     List<CommentInfo> commentInfos =
     107          11 :         commentInfosMap.values().stream().flatMap(List::stream).collect(toList());
     108          11 :     List<ChangeMessage> changeMessages = changeMessagesUtil.byChange(rsrc.getNotes());
     109          11 :     CommentsUtil.linkCommentsToChangeMessages(commentInfos, changeMessages, true);
     110          11 :     return commentInfosMap;
     111             :   }
     112             : 
     113             :   private CommentJson.HumanCommentFormatter getCommentFormatter(ChangeResource rsrc) {
     114          12 :     return commentJson
     115          12 :         .get()
     116          12 :         .setFillAccounts(true)
     117          12 :         .setFillPatchSet(true)
     118          12 :         .setFillCommentContext(includeContext)
     119          12 :         .setContextPadding(contextPadding)
     120          12 :         .setProjectKey(rsrc.getProject())
     121          12 :         .setChangeId(rsrc.getId())
     122          12 :         .newHumanCommentFormatter();
     123             :   }
     124             : }

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