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

          Line data    Source code
       1             : // Copyright (C) 2017 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.common.base.Strings;
      18             : import com.google.common.collect.Lists;
      19             : import com.google.gerrit.extensions.common.ProjectInfo;
      20             : import com.google.gerrit.extensions.restapi.BadRequestException;
      21             : import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
      22             : import com.google.gerrit.extensions.restapi.Response;
      23             : import com.google.gerrit.extensions.restapi.RestReadView;
      24             : import com.google.gerrit.extensions.restapi.TopLevelResource;
      25             : import com.google.gerrit.index.project.ProjectData;
      26             : import com.google.gerrit.index.project.ProjectIndex;
      27             : import com.google.gerrit.index.project.ProjectIndexCollection;
      28             : import com.google.gerrit.index.query.QueryParseException;
      29             : import com.google.gerrit.index.query.QueryResult;
      30             : import com.google.gerrit.server.project.ProjectJson;
      31             : import com.google.gerrit.server.query.project.ProjectQueryBuilder;
      32             : import com.google.gerrit.server.query.project.ProjectQueryProcessor;
      33             : import com.google.inject.Inject;
      34             : import com.google.inject.Provider;
      35             : import java.util.ArrayList;
      36             : import java.util.List;
      37             : import org.kohsuke.args4j.Option;
      38             : 
      39             : /** Implements the {@code GET /projects/?query=QUERY} endpoint. */
      40             : public class QueryProjects implements RestReadView<TopLevelResource> {
      41             :   private final ProjectIndexCollection indexes;
      42             :   private final ProjectQueryBuilder queryBuilder;
      43             :   private final Provider<ProjectQueryProcessor> queryProcessorProvider;
      44             :   private final ProjectJson json;
      45             : 
      46             :   private String query;
      47             :   private int limit;
      48             :   private int start;
      49             : 
      50             :   @Option(
      51             :       name = "--query",
      52             :       aliases = {"-q"},
      53             :       usage = "project query")
      54             :   public QueryProjects withQuery(String query) {
      55           6 :     this.query = query;
      56           6 :     return this;
      57             :   }
      58             : 
      59             :   @Option(
      60             :       name = "--limit",
      61             :       aliases = {"-n"},
      62             :       metaVar = "CNT",
      63             :       usage = "maximum number of projects to list")
      64             :   public QueryProjects withLimit(int limit) {
      65           6 :     this.limit = limit;
      66           6 :     return this;
      67             :   }
      68             : 
      69             :   @Option(
      70             :       name = "--start",
      71             :       aliases = {"-S"},
      72             :       metaVar = "CNT",
      73             :       usage = "number of projects to skip")
      74             :   public QueryProjects withStart(int start) {
      75           4 :     this.start = start;
      76           4 :     return this;
      77             :   }
      78             : 
      79             :   @Inject
      80             :   protected QueryProjects(
      81             :       ProjectIndexCollection indexes,
      82             :       ProjectQueryBuilder queryBuilder,
      83             :       Provider<ProjectQueryProcessor> queryProcessorProvider,
      84           6 :       ProjectJson json) {
      85           6 :     this.indexes = indexes;
      86           6 :     this.queryBuilder = queryBuilder;
      87           6 :     this.queryProcessorProvider = queryProcessorProvider;
      88           6 :     this.json = json;
      89           6 :   }
      90             : 
      91             :   @Override
      92             :   public Response<List<ProjectInfo>> apply(TopLevelResource resource)
      93             :       throws BadRequestException, MethodNotAllowedException {
      94           0 :     return Response.ok(apply());
      95             :   }
      96             : 
      97             :   public List<ProjectInfo> apply() throws BadRequestException, MethodNotAllowedException {
      98           6 :     if (Strings.isNullOrEmpty(query)) {
      99           0 :       throw new BadRequestException("missing query field");
     100             :     }
     101             : 
     102           6 :     ProjectIndex searchIndex = indexes.getSearchIndex();
     103           6 :     if (searchIndex == null) {
     104           0 :       throw new MethodNotAllowedException("no project index");
     105             :     }
     106             : 
     107           6 :     ProjectQueryProcessor queryProcessor = queryProcessorProvider.get();
     108             : 
     109           6 :     if (start < 0) {
     110           2 :       throw new BadRequestException("'start' parameter cannot be less than zero");
     111             :     }
     112             : 
     113           6 :     if (start != 0) {
     114           2 :       queryProcessor.setStart(start);
     115             :     }
     116             : 
     117           6 :     if (limit != 0) {
     118           3 :       queryProcessor.setUserProvidedLimit(limit);
     119             :     }
     120             : 
     121             :     try {
     122           6 :       QueryResult<ProjectData> result = queryProcessor.query(queryBuilder.parse(query));
     123           6 :       List<ProjectData> pds = result.entities();
     124             : 
     125           6 :       ArrayList<ProjectInfo> projectInfos = Lists.newArrayListWithCapacity(pds.size());
     126           6 :       for (ProjectData pd : pds) {
     127           5 :         projectInfos.add(json.format(pd.getProject()));
     128           5 :       }
     129           6 :       return projectInfos;
     130           2 :     } catch (QueryParseException e) {
     131           2 :       throw new BadRequestException(e.getMessage());
     132             :     }
     133             :   }
     134             : }

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