Line data Source code
1 : // Copyright (C) 2020 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.common.collect.ImmutableList; 18 : import com.google.gerrit.entities.HumanComment; 19 : import com.google.gerrit.entities.PatchSet; 20 : import com.google.gerrit.extensions.common.CommentInfo; 21 : import com.google.gerrit.extensions.restapi.AuthException; 22 : import com.google.gerrit.extensions.restapi.Response; 23 : import com.google.gerrit.extensions.restapi.RestApiException; 24 : import com.google.gerrit.extensions.restapi.RestReadView; 25 : import com.google.gerrit.server.CommentsUtil; 26 : import com.google.gerrit.server.change.RevisionResource; 27 : import com.google.gerrit.server.permissions.PermissionBackendException; 28 : import com.google.inject.Inject; 29 : import com.google.inject.Provider; 30 : import com.google.inject.Singleton; 31 : import java.util.List; 32 : import java.util.Map; 33 : 34 : @Singleton 35 : public class ListPortedDrafts implements RestReadView<RevisionResource> { 36 : 37 : private final CommentsUtil commentsUtil; 38 : private final CommentPorter commentPorter; 39 : private final Provider<CommentJson> commentJson; 40 : 41 : @Inject 42 : public ListPortedDrafts( 43 145 : Provider<CommentJson> commentJson, CommentsUtil commentsUtil, CommentPorter commentPorter) { 44 145 : this.commentJson = commentJson; 45 145 : this.commentsUtil = commentsUtil; 46 145 : this.commentPorter = commentPorter; 47 145 : } 48 : 49 : @Override 50 : public Response<Map<String, List<CommentInfo>>> apply(RevisionResource revisionResource) 51 : throws PermissionBackendException, RestApiException { 52 2 : if (!revisionResource.getUser().isIdentifiedUser()) { 53 1 : throw new AuthException("requires authentication; only authenticated users can have drafts"); 54 : } 55 2 : PatchSet targetPatchset = revisionResource.getPatchSet(); 56 : 57 2 : List<HumanComment> draftComments = 58 2 : commentsUtil.draftByChangeAuthor( 59 2 : revisionResource.getNotes(), revisionResource.getAccountId()); 60 2 : ImmutableList<HumanComment> portedDraftComments = 61 2 : commentPorter.portComments( 62 2 : revisionResource.getNotes(), targetPatchset, draftComments, ImmutableList.of()); 63 2 : return Response.ok(format(portedDraftComments)); 64 : } 65 : 66 : private Map<String, List<CommentInfo>> format(List<HumanComment> comments) 67 : throws PermissionBackendException { 68 2 : return commentJson 69 2 : .get() 70 : // Always unset for draft comments as only draft comments of the requesting user are 71 : // returned. 72 2 : .setFillAccounts(false) 73 2 : .setFillPatchSet(true) 74 2 : .newHumanCommentFormatter() 75 2 : .format(comments); 76 : } 77 : }