LCOV - code coverage report
Current view: top level - server/restapi/project - LabelsCollection.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 18 18 100.0 %
Date: 2022-11-19 15:00:39 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2019 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.project;
      16             : 
      17             : import com.google.gerrit.entities.LabelType;
      18             : import com.google.gerrit.extensions.registration.DynamicMap;
      19             : import com.google.gerrit.extensions.restapi.AuthException;
      20             : import com.google.gerrit.extensions.restapi.ChildCollection;
      21             : import com.google.gerrit.extensions.restapi.IdString;
      22             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      23             : import com.google.gerrit.extensions.restapi.RestApiException;
      24             : import com.google.gerrit.extensions.restapi.RestView;
      25             : import com.google.gerrit.server.CurrentUser;
      26             : import com.google.gerrit.server.permissions.PermissionBackend;
      27             : import com.google.gerrit.server.permissions.PermissionBackendException;
      28             : import com.google.gerrit.server.permissions.ProjectPermission;
      29             : import com.google.gerrit.server.project.LabelResource;
      30             : import com.google.gerrit.server.project.ProjectResource;
      31             : import com.google.inject.Inject;
      32             : import com.google.inject.Provider;
      33             : import com.google.inject.Singleton;
      34             : 
      35             : @Singleton
      36             : public class LabelsCollection implements ChildCollection<ProjectResource, LabelResource> {
      37             :   private final Provider<ListLabels> list;
      38             :   private final DynamicMap<RestView<LabelResource>> views;
      39             :   private final Provider<CurrentUser> user;
      40             :   private final PermissionBackend permissionBackend;
      41             : 
      42             :   @Inject
      43             :   LabelsCollection(
      44             :       Provider<ListLabels> list,
      45             :       DynamicMap<RestView<LabelResource>> views,
      46             :       Provider<CurrentUser> user,
      47         144 :       PermissionBackend permissionBackend) {
      48         144 :     this.list = list;
      49         144 :     this.views = views;
      50         144 :     this.user = user;
      51         144 :     this.permissionBackend = permissionBackend;
      52         144 :   }
      53             : 
      54             :   @Override
      55             :   public RestView<ProjectResource> list() throws RestApiException {
      56           1 :     return list.get();
      57             :   }
      58             : 
      59             :   @Override
      60             :   public LabelResource parse(ProjectResource parent, IdString id)
      61             :       throws AuthException, ResourceNotFoundException, PermissionBackendException {
      62           8 :     if (!user.get().isIdentifiedUser()) {
      63           3 :       throw new AuthException("Authentication required");
      64             :     }
      65             : 
      66           8 :     permissionBackend
      67           8 :         .currentUser()
      68           8 :         .project(parent.getNameKey())
      69           8 :         .check(ProjectPermission.READ_CONFIG);
      70           8 :     LabelType labelType = parent.getProjectState().getConfig().getLabelSections().get(id.get());
      71           8 :     if (labelType == null) {
      72           5 :       throw new ResourceNotFoundException(id);
      73             :     }
      74           8 :     return new LabelResource(parent, labelType);
      75             :   }
      76             : 
      77             :   @Override
      78             :   public DynamicMap<RestView<LabelResource>> views() {
      79           1 :     return views;
      80             :   }
      81             : }

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