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.gerrit.entities.ChangeMessage; 20 : import com.google.gerrit.extensions.common.RobotCommentInfo; 21 : import com.google.gerrit.extensions.restapi.AuthException; 22 : import com.google.gerrit.extensions.restapi.Response; 23 : import com.google.gerrit.extensions.restapi.RestReadView; 24 : import com.google.gerrit.server.ChangeMessagesUtil; 25 : import com.google.gerrit.server.CommentsUtil; 26 : import com.google.gerrit.server.change.ChangeResource; 27 : import com.google.gerrit.server.permissions.PermissionBackendException; 28 : import com.google.gerrit.server.query.change.ChangeData; 29 : import com.google.inject.Inject; 30 : import com.google.inject.Provider; 31 : import java.util.List; 32 : import java.util.Map; 33 : 34 : public class ListChangeRobotComments implements RestReadView<ChangeResource> { 35 : private final ChangeData.Factory changeDataFactory; 36 : private final Provider<CommentJson> commentJson; 37 : private final CommentsUtil commentsUtil; 38 : private final ChangeMessagesUtil changeMessagesUtil; 39 : 40 : @Inject 41 : ListChangeRobotComments( 42 : ChangeData.Factory changeDataFactory, 43 : Provider<CommentJson> commentJson, 44 : CommentsUtil commentsUtil, 45 91 : ChangeMessagesUtil changeMessagesUtil) { 46 91 : this.changeDataFactory = changeDataFactory; 47 91 : this.commentJson = commentJson; 48 91 : this.commentsUtil = commentsUtil; 49 91 : this.changeMessagesUtil = changeMessagesUtil; 50 91 : } 51 : 52 : @Override 53 : public Response<Map<String, List<RobotCommentInfo>>> apply(ChangeResource rsrc) 54 : throws AuthException, PermissionBackendException { 55 4 : ChangeData cd = changeDataFactory.create(rsrc.getNotes()); 56 4 : Map<String, List<RobotCommentInfo>> robotCommentsMap = 57 : commentJson 58 4 : .get() 59 4 : .setFillAccounts(true) 60 4 : .setFillPatchSet(true) 61 4 : .newRobotCommentFormatter() 62 4 : .format(commentsUtil.robotCommentsByChange(cd.notes())); 63 4 : List<RobotCommentInfo> commentInfos = 64 4 : robotCommentsMap.values().stream().flatMap(List::stream).collect(toList()); 65 4 : List<ChangeMessage> changeMessages = changeMessagesUtil.byChange(rsrc.getNotes()); 66 4 : CommentsUtil.linkCommentsToChangeMessages(commentInfos, changeMessages, false); 67 4 : return Response.ok(robotCommentsMap); 68 : } 69 : }