LCOV - code coverage report
Current view: top level - server/restapi/change - ListRevisionReviewers.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 10 17 58.8 %
Date: 2022-11-19 15:00:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2017 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.Account;
      18             : import com.google.gerrit.entities.Address;
      19             : import com.google.gerrit.extensions.api.changes.ReviewerInfo;
      20             : import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
      21             : import com.google.gerrit.extensions.restapi.Response;
      22             : import com.google.gerrit.extensions.restapi.RestReadView;
      23             : import com.google.gerrit.server.approval.ApprovalsUtil;
      24             : import com.google.gerrit.server.change.ReviewerJson;
      25             : import com.google.gerrit.server.change.ReviewerResource;
      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.Singleton;
      30             : import java.util.LinkedHashMap;
      31             : import java.util.List;
      32             : import java.util.Map;
      33             : 
      34             : @Singleton
      35             : class ListRevisionReviewers implements RestReadView<RevisionResource> {
      36             :   private final ApprovalsUtil approvalsUtil;
      37             :   private final ReviewerJson json;
      38             :   private final ReviewerResource.Factory resourceFactory;
      39             : 
      40             :   @Inject
      41             :   ListRevisionReviewers(
      42         145 :       ApprovalsUtil approvalsUtil, ReviewerResource.Factory resourceFactory, ReviewerJson json) {
      43         145 :     this.approvalsUtil = approvalsUtil;
      44         145 :     this.resourceFactory = resourceFactory;
      45         145 :     this.json = json;
      46         145 :   }
      47             : 
      48             :   @Override
      49             :   public Response<List<ReviewerInfo>> apply(RevisionResource rsrc)
      50             :       throws MethodNotAllowedException, PermissionBackendException {
      51           1 :     if (!rsrc.isCurrent()) {
      52           0 :       throw new MethodNotAllowedException("Cannot list reviewers on non-current patch set");
      53             :     }
      54             : 
      55           1 :     Map<String, ReviewerResource> reviewers = new LinkedHashMap<>();
      56           1 :     for (Account.Id accountId : approvalsUtil.getReviewers(rsrc.getNotes()).all()) {
      57           0 :       if (!reviewers.containsKey(accountId.toString())) {
      58           0 :         reviewers.put(accountId.toString(), resourceFactory.create(rsrc, accountId));
      59             :       }
      60           0 :     }
      61           1 :     for (Address address : rsrc.getNotes().getReviewersByEmail().all()) {
      62           0 :       if (!reviewers.containsKey(address.toString())) {
      63           0 :         reviewers.put(address.toString(), new ReviewerResource(rsrc, address));
      64             :       }
      65           0 :     }
      66           1 :     return Response.ok(json.format(reviewers.values()));
      67             :   }
      68             : }

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