LCOV - code coverage report
Current view: top level - server/restapi/group - ListSubgroups.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 19 23 82.6 %
Date: 2022-11-19 15:00:39 Functions: 5 6 83.3 %

          Line data    Source code
       1             : // Copyright (C) 2013 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.group;
      16             : 
      17             : import static com.google.common.base.Strings.nullToEmpty;
      18             : import static java.util.Comparator.comparing;
      19             : 
      20             : import com.google.common.flogger.FluentLogger;
      21             : import com.google.gerrit.entities.AccountGroup;
      22             : import com.google.gerrit.entities.GroupDescription;
      23             : import com.google.gerrit.exceptions.NoSuchGroupException;
      24             : import com.google.gerrit.extensions.common.GroupInfo;
      25             : import com.google.gerrit.extensions.restapi.Response;
      26             : import com.google.gerrit.extensions.restapi.RestReadView;
      27             : import com.google.gerrit.server.account.GroupControl;
      28             : import com.google.gerrit.server.group.GroupResource;
      29             : import com.google.gerrit.server.permissions.PermissionBackendException;
      30             : import com.google.inject.Inject;
      31             : import com.google.inject.Singleton;
      32             : import java.util.ArrayList;
      33             : import java.util.List;
      34             : 
      35             : @Singleton
      36             : public class ListSubgroups implements RestReadView<GroupResource> {
      37         146 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      38             : 
      39             :   private final GroupControl.Factory controlFactory;
      40             :   private final GroupJson json;
      41             : 
      42             :   @Inject
      43         146 :   ListSubgroups(GroupControl.Factory controlFactory, GroupJson json) {
      44         146 :     this.controlFactory = controlFactory;
      45         146 :     this.json = json;
      46         146 :   }
      47             : 
      48             :   @Override
      49             :   public Response<List<GroupInfo>> apply(GroupResource rsrc)
      50             :       throws NotInternalGroupException, PermissionBackendException {
      51           2 :     GroupDescription.Internal group =
      52           2 :         rsrc.asInternalGroup().orElseThrow(NotInternalGroupException::new);
      53             : 
      54           2 :     return Response.ok(getDirectSubgroups(group, rsrc.getControl()));
      55             :   }
      56             : 
      57             :   public List<GroupInfo> getDirectSubgroups(
      58             :       GroupDescription.Internal group, GroupControl groupControl)
      59             :       throws PermissionBackendException {
      60           4 :     boolean ownerOfParent = groupControl.isOwner();
      61           4 :     List<GroupInfo> included = new ArrayList<>();
      62           4 :     for (AccountGroup.UUID subgroupUuid : group.getSubgroups()) {
      63             :       try {
      64           2 :         GroupControl i = controlFactory.controlFor(subgroupUuid);
      65           2 :         if (ownerOfParent || i.isVisible()) {
      66           2 :           included.add(json.format(i.getGroup()));
      67             :         }
      68           0 :       } catch (NoSuchGroupException notFound) {
      69           0 :         logger.atWarning().log(
      70           0 :             "Group %s no longer available, subgroup of %s", subgroupUuid, group.getName());
      71           0 :         continue;
      72           2 :       }
      73           2 :     }
      74           4 :     included.sort(
      75           4 :         comparing((GroupInfo g) -> nullToEmpty(g.name)).thenComparing(g -> nullToEmpty(g.id)));
      76           4 :     return included;
      77             :   }
      78             : }

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