LCOV - code coverage report
Current view: top level - server/restapi/project - ListChildProjects.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 16 18 88.9 %
Date: 2022-11-19 15:00:39 Functions: 5 5 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.project;
      16             : 
      17             : import static java.util.stream.Collectors.toList;
      18             : 
      19             : import com.google.gerrit.entities.Project;
      20             : import com.google.gerrit.extensions.common.ProjectInfo;
      21             : import com.google.gerrit.extensions.restapi.BadRequestException;
      22             : import com.google.gerrit.extensions.restapi.ResourceConflictException;
      23             : import com.google.gerrit.extensions.restapi.Response;
      24             : import com.google.gerrit.extensions.restapi.RestApiException;
      25             : import com.google.gerrit.extensions.restapi.RestReadView;
      26             : import com.google.gerrit.server.permissions.PermissionBackendException;
      27             : import com.google.gerrit.server.project.ChildProjects;
      28             : import com.google.gerrit.server.project.ProjectResource;
      29             : import com.google.inject.Inject;
      30             : import com.google.inject.Provider;
      31             : import java.util.List;
      32             : import org.kohsuke.args4j.Option;
      33             : 
      34             : public class ListChildProjects implements RestReadView<ProjectResource> {
      35             : 
      36             :   @Option(name = "--recursive", usage = "to list child projects recursively")
      37             :   private boolean recursive;
      38             : 
      39             :   @Option(name = "--limit", usage = "maximum number of parents projects to list")
      40             :   private int limit;
      41             : 
      42             :   private final ChildProjects childProjects;
      43             :   private final Provider<QueryProjects> queryProvider;
      44             : 
      45             :   @Inject
      46           4 :   ListChildProjects(ChildProjects childProjects, Provider<QueryProjects> queryProvider) {
      47           4 :     this.childProjects = childProjects;
      48           4 :     this.queryProvider = queryProvider;
      49           4 :   }
      50             : 
      51             :   public ListChildProjects withRecursive(boolean recursive) {
      52           2 :     this.recursive = recursive;
      53           2 :     return this;
      54             :   }
      55             : 
      56             :   public ListChildProjects withLimit(int limit) {
      57           1 :     this.limit = limit;
      58           1 :     return this;
      59             :   }
      60             : 
      61             :   @Override
      62             :   public Response<List<ProjectInfo>> apply(ProjectResource rsrc)
      63             :       throws PermissionBackendException, RestApiException {
      64           3 :     if (limit < 0) {
      65           0 :       throw new BadRequestException("limit must be a positive number");
      66             :     }
      67           3 :     if (recursive && limit != 0) {
      68           0 :       throw new ResourceConflictException("recursive and limit options are mutually exclusive");
      69             :     }
      70           3 :     rsrc.getProjectState().checkStatePermitsRead();
      71           3 :     if (recursive) {
      72           2 :       return Response.ok(childProjects.list(rsrc.getNameKey()));
      73             :     }
      74             : 
      75           2 :     return Response.ok(directChildProjects(rsrc.getNameKey()));
      76             :   }
      77             : 
      78             :   private List<ProjectInfo> directChildProjects(Project.NameKey parent) throws RestApiException {
      79           2 :     return queryProvider.get().withQuery("parent:" + parent.get()).withLimit(limit).apply().stream()
      80           2 :         .collect(toList());
      81             :   }
      82             : }

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