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

          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 com.google.gerrit.entities.AccountGroup;
      18             : import com.google.gerrit.entities.GroupDescription;
      19             : import com.google.gerrit.exceptions.NoSuchGroupException;
      20             : import com.google.gerrit.extensions.common.GroupOptionsInfo;
      21             : import com.google.gerrit.extensions.restapi.AuthException;
      22             : import com.google.gerrit.extensions.restapi.BadRequestException;
      23             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      24             : import com.google.gerrit.extensions.restapi.Response;
      25             : import com.google.gerrit.extensions.restapi.RestModifyView;
      26             : import com.google.gerrit.server.UserInitiated;
      27             : import com.google.gerrit.server.group.GroupResource;
      28             : import com.google.gerrit.server.group.db.GroupDelta;
      29             : import com.google.gerrit.server.group.db.GroupsUpdate;
      30             : import com.google.inject.Inject;
      31             : import com.google.inject.Provider;
      32             : import com.google.inject.Singleton;
      33             : import java.io.IOException;
      34             : import org.eclipse.jgit.errors.ConfigInvalidException;
      35             : 
      36             : @Singleton
      37             : public class PutOptions implements RestModifyView<GroupResource, GroupOptionsInfo> {
      38             :   private final Provider<GroupsUpdate> groupsUpdateProvider;
      39             : 
      40             :   @Inject
      41         146 :   PutOptions(@UserInitiated Provider<GroupsUpdate> groupsUpdateProvider) {
      42         146 :     this.groupsUpdateProvider = groupsUpdateProvider;
      43         146 :   }
      44             : 
      45             :   @Override
      46             :   public Response<GroupOptionsInfo> apply(GroupResource resource, GroupOptionsInfo input)
      47             :       throws NotInternalGroupException, AuthException, BadRequestException,
      48             :           ResourceNotFoundException, IOException, ConfigInvalidException {
      49           2 :     GroupDescription.Internal internalGroup =
      50           2 :         resource.asInternalGroup().orElseThrow(NotInternalGroupException::new);
      51           2 :     if (!resource.getControl().isOwner()) {
      52           0 :       throw new AuthException("Not group owner");
      53             :     }
      54             : 
      55           2 :     if (input == null) {
      56           0 :       throw new BadRequestException("options are required");
      57             :     }
      58           2 :     if (input.visibleToAll == null) {
      59           1 :       input.visibleToAll = false;
      60             :     }
      61             : 
      62           2 :     if (internalGroup.isVisibleToAll() != input.visibleToAll) {
      63           1 :       AccountGroup.UUID groupUuid = internalGroup.getGroupUUID();
      64           1 :       GroupDelta groupDelta = GroupDelta.builder().setVisibleToAll(input.visibleToAll).build();
      65             :       try {
      66           1 :         groupsUpdateProvider.get().updateGroup(groupUuid, groupDelta);
      67           0 :       } catch (NoSuchGroupException e) {
      68           0 :         throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e);
      69           1 :       }
      70             :     }
      71             : 
      72           2 :     GroupOptionsInfo options = new GroupOptionsInfo();
      73           2 :     if (input.visibleToAll) {
      74           1 :       options.visibleToAll = true;
      75             :     }
      76           2 :     return Response.ok(options);
      77             :   }
      78             : }

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