LCOV - code coverage report
Current view: top level - server/api/groups - GroupsImpl.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 61 70 87.1 %
Date: 2022-11-19 15:00:39 Functions: 13 13 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2015 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.api.groups;
      16             : 
      17             : import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
      18             : import static java.util.Objects.requireNonNull;
      19             : 
      20             : import com.google.gerrit.extensions.api.groups.GroupApi;
      21             : import com.google.gerrit.extensions.api.groups.GroupInput;
      22             : import com.google.gerrit.extensions.api.groups.Groups;
      23             : import com.google.gerrit.extensions.client.ListGroupsOption;
      24             : import com.google.gerrit.extensions.common.GroupInfo;
      25             : import com.google.gerrit.extensions.restapi.BadRequestException;
      26             : import com.google.gerrit.extensions.restapi.IdString;
      27             : import com.google.gerrit.extensions.restapi.RestApiException;
      28             : import com.google.gerrit.extensions.restapi.TopLevelResource;
      29             : import com.google.gerrit.server.account.AccountResolver;
      30             : import com.google.gerrit.server.group.GroupResolver;
      31             : import com.google.gerrit.server.permissions.GlobalPermission;
      32             : import com.google.gerrit.server.permissions.PermissionBackend;
      33             : import com.google.gerrit.server.project.ProjectResource;
      34             : import com.google.gerrit.server.restapi.group.CreateGroup;
      35             : import com.google.gerrit.server.restapi.group.GroupsCollection;
      36             : import com.google.gerrit.server.restapi.group.ListGroups;
      37             : import com.google.gerrit.server.restapi.group.QueryGroups;
      38             : import com.google.gerrit.server.restapi.project.ProjectsCollection;
      39             : import com.google.inject.Inject;
      40             : import com.google.inject.Provider;
      41             : import com.google.inject.Singleton;
      42             : import java.util.List;
      43             : import java.util.SortedMap;
      44             : 
      45             : @Singleton
      46             : class GroupsImpl implements Groups {
      47             :   private final AccountResolver accountResolver;
      48             :   private final GroupsCollection groups;
      49             :   private final GroupResolver groupResolver;
      50             :   private final ProjectsCollection projects;
      51             :   private final Provider<ListGroups> listGroups;
      52             :   private final Provider<QueryGroups> queryGroups;
      53             :   private final PermissionBackend permissionBackend;
      54             :   private final CreateGroup createGroup;
      55             :   private final GroupApiImpl.Factory api;
      56             : 
      57             :   @Inject
      58             :   GroupsImpl(
      59             :       AccountResolver accountResolver,
      60             :       GroupsCollection groups,
      61             :       GroupResolver groupResolver,
      62             :       ProjectsCollection projects,
      63             :       Provider<ListGroups> listGroups,
      64             :       Provider<QueryGroups> queryGroups,
      65             :       PermissionBackend permissionBackend,
      66             :       CreateGroup createGroup,
      67         149 :       GroupApiImpl.Factory api) {
      68         149 :     this.accountResolver = accountResolver;
      69         149 :     this.groups = groups;
      70         149 :     this.groupResolver = groupResolver;
      71         149 :     this.projects = projects;
      72         149 :     this.listGroups = listGroups;
      73         149 :     this.queryGroups = queryGroups;
      74         149 :     this.permissionBackend = permissionBackend;
      75         149 :     this.createGroup = createGroup;
      76         149 :     this.api = api;
      77         149 :   }
      78             : 
      79             :   @Override
      80             :   public GroupApi id(String id) throws RestApiException {
      81          29 :     return api.create(groups.parse(TopLevelResource.INSTANCE, IdString.fromDecoded(id)));
      82             :   }
      83             : 
      84             :   @Override
      85             :   public GroupApi create(String name) throws RestApiException {
      86           9 :     GroupInput in = new GroupInput();
      87           9 :     in.name = name;
      88           9 :     return create(in);
      89             :   }
      90             : 
      91             :   @Override
      92             :   public GroupApi create(GroupInput in) throws RestApiException {
      93          28 :     if (requireNonNull(in, "GroupInput").name == null) {
      94           0 :       throw new BadRequestException("GroupInput must specify name");
      95             :     }
      96             :     try {
      97          28 :       permissionBackend
      98          28 :           .currentUser()
      99          28 :           .checkAny(GlobalPermission.fromAnnotation(createGroup.getClass()));
     100          28 :       GroupInfo info =
     101          28 :           createGroup.apply(TopLevelResource.INSTANCE, IdString.fromDecoded(in.name), in).value();
     102          28 :       return id(info.id);
     103           1 :     } catch (Exception e) {
     104           1 :       throw asRestApiException("Cannot create group " + in.name, e);
     105             :     }
     106             :   }
     107             : 
     108             :   @Override
     109             :   public ListRequest list() {
     110           1 :     return new ListRequest() {
     111             :       @Override
     112             :       public SortedMap<String, GroupInfo> getAsMap() throws RestApiException {
     113           1 :         return list(this);
     114             :       }
     115             :     };
     116             :   }
     117             : 
     118             :   private SortedMap<String, GroupInfo> list(ListRequest req) throws RestApiException {
     119           1 :     TopLevelResource tlr = TopLevelResource.INSTANCE;
     120           1 :     ListGroups list = listGroups.get();
     121           1 :     list.setOptions(req.getOptions());
     122             : 
     123           1 :     for (String project : req.getProjects()) {
     124             :       try {
     125           0 :         ProjectResource rsrc = projects.parse(tlr, IdString.fromDecoded(project));
     126           0 :         list.addProject(rsrc.getProjectState());
     127           0 :       } catch (Exception e) {
     128           0 :         throw asRestApiException("Error looking up project " + project, e);
     129           0 :       }
     130           0 :     }
     131             : 
     132           1 :     for (String group : req.getGroups()) {
     133           1 :       list.addGroup(groupResolver.parse(group).getGroupUUID());
     134           1 :     }
     135             : 
     136           1 :     list.setVisibleToAll(req.getVisibleToAll());
     137             : 
     138           1 :     if (req.getOwnedBy() != null) {
     139           1 :       list.setOwnedBy(req.getOwnedBy());
     140             :     }
     141             : 
     142           1 :     if (req.getUser() != null) {
     143             :       try {
     144           1 :         list.setUser(accountResolver.resolve(req.getUser()).asUnique().account().id());
     145           0 :       } catch (Exception e) {
     146           0 :         throw asRestApiException("Error looking up user " + req.getUser(), e);
     147           1 :       }
     148             :     }
     149             : 
     150           1 :     list.setOwned(req.getOwned());
     151           1 :     list.setLimit(req.getLimit());
     152           1 :     list.setStart(req.getStart());
     153           1 :     list.setMatchSubstring(req.getSubstring());
     154           1 :     list.setMatchRegex(req.getRegex());
     155           1 :     list.setSuggest(req.getSuggest());
     156             :     try {
     157           1 :       return list.apply(tlr).value();
     158           1 :     } catch (Exception e) {
     159           1 :       throw asRestApiException("Cannot list groups", e);
     160             :     }
     161             :   }
     162             : 
     163             :   @Override
     164             :   public QueryRequest query() {
     165          12 :     return new QueryRequest() {
     166             :       @Override
     167             :       public List<GroupInfo> get() throws RestApiException {
     168          12 :         return GroupsImpl.this.query(this);
     169             :       }
     170             :     };
     171             :   }
     172             : 
     173             :   @Override
     174             :   public QueryRequest query(String query) {
     175          12 :     return query().withQuery(query);
     176             :   }
     177             : 
     178             :   private List<GroupInfo> query(QueryRequest r) throws RestApiException {
     179             :     try {
     180          12 :       QueryGroups myQueryGroups = queryGroups.get();
     181          12 :       myQueryGroups.setQuery(r.getQuery());
     182          12 :       myQueryGroups.setLimit(r.getLimit());
     183          12 :       myQueryGroups.setStart(r.getStart());
     184          12 :       for (ListGroupsOption option : r.getOptions()) {
     185           1 :         myQueryGroups.addOption(option);
     186           1 :       }
     187          12 :       return myQueryGroups.apply(TopLevelResource.INSTANCE).value();
     188           2 :     } catch (Exception e) {
     189           2 :       throw asRestApiException("Cannot query groups", e);
     190             :     }
     191             :   }
     192             : }

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