Line data Source code
1 : // Copyright (C) 2016 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.RobotComment; 22 : import com.google.gerrit.extensions.common.RobotCommentInfo; 23 : import com.google.gerrit.extensions.restapi.Response; 24 : import com.google.gerrit.extensions.restapi.RestReadView; 25 : import com.google.gerrit.server.ChangeMessagesUtil; 26 : import com.google.gerrit.server.CommentsUtil; 27 : import com.google.gerrit.server.change.RevisionResource; 28 : import com.google.gerrit.server.permissions.PermissionBackendException; 29 : import com.google.gerrit.server.restapi.change.CommentJson.RobotCommentFormatter; 30 : import com.google.inject.Inject; 31 : import com.google.inject.Provider; 32 : import com.google.inject.Singleton; 33 : import java.util.List; 34 : import java.util.Map; 35 : 36 : @Singleton 37 : public class ListRobotComments implements RestReadView<RevisionResource> { 38 : protected final Provider<CommentJson> commentJson; 39 : protected final CommentsUtil commentsUtil; 40 : protected final ChangeMessagesUtil changeMessagesUtil; 41 : 42 : @Inject 43 : ListRobotComments( 44 : Provider<CommentJson> commentJson, 45 : CommentsUtil commentsUtil, 46 145 : ChangeMessagesUtil changeMessagesUtil) { 47 145 : this.commentJson = commentJson; 48 145 : this.commentsUtil = commentsUtil; 49 145 : this.changeMessagesUtil = changeMessagesUtil; 50 145 : } 51 : 52 : @Override 53 : public Response<Map<String, List<RobotCommentInfo>>> apply(RevisionResource rsrc) 54 : throws PermissionBackendException { 55 2 : return Response.ok(getAsMap(listComments(rsrc), rsrc)); 56 : } 57 : 58 : public ImmutableList<RobotCommentInfo> getComments(RevisionResource rsrc) 59 : throws PermissionBackendException { 60 4 : return getAsList(listComments(rsrc), rsrc); 61 : } 62 : 63 : private ImmutableList<RobotCommentInfo> getAsList( 64 : Iterable<RobotComment> comments, RevisionResource rsrc) throws PermissionBackendException { 65 4 : ImmutableList<RobotCommentInfo> commentInfos = getCommentFormatter().formatAsList(comments); 66 4 : List<ChangeMessage> changeMessages = changeMessagesUtil.byChange(rsrc.getNotes()); 67 4 : CommentsUtil.linkCommentsToChangeMessages(commentInfos, changeMessages, false); 68 4 : return commentInfos; 69 : } 70 : 71 : private Map<String, List<RobotCommentInfo>> getAsMap( 72 : Iterable<RobotComment> comments, RevisionResource rsrc) throws PermissionBackendException { 73 2 : Map<String, List<RobotCommentInfo>> commentInfosMap = getCommentFormatter().format(comments); 74 2 : List<RobotCommentInfo> commentInfos = 75 2 : commentInfosMap.values().stream().flatMap(List::stream).collect(toList()); 76 2 : List<ChangeMessage> changeMessages = changeMessagesUtil.byChange(rsrc.getNotes()); 77 2 : CommentsUtil.linkCommentsToChangeMessages(commentInfos, changeMessages, false); 78 2 : return commentInfosMap; 79 : } 80 : 81 : private Iterable<RobotComment> listComments(RevisionResource rsrc) { 82 4 : return commentsUtil.robotCommentsByPatchSet(rsrc.getNotes(), rsrc.getPatchSet().id()); 83 : } 84 : 85 : private RobotCommentFormatter getCommentFormatter() { 86 4 : return commentJson.get().setFillAccounts(true).newRobotCommentFormatter(); 87 : } 88 : }