LCOV - code coverage report
Current view: top level - server/restapi/project - GetDashboard.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 30 41 73.2 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2012 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 com.google.gerrit.entities.RefNames.REFS_DASHBOARDS;
      18             : import static com.google.gerrit.server.restapi.project.DashboardsCollection.isDefaultDashboard;
      19             : 
      20             : import com.google.common.base.Splitter;
      21             : import com.google.common.base.Strings;
      22             : import com.google.common.collect.Lists;
      23             : import com.google.gerrit.extensions.api.projects.DashboardInfo;
      24             : import com.google.gerrit.extensions.restapi.BadRequestException;
      25             : import com.google.gerrit.extensions.restapi.IdString;
      26             : import com.google.gerrit.extensions.restapi.ResourceConflictException;
      27             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      28             : import com.google.gerrit.extensions.restapi.Response;
      29             : import com.google.gerrit.extensions.restapi.RestApiException;
      30             : import com.google.gerrit.extensions.restapi.RestReadView;
      31             : import com.google.gerrit.extensions.restapi.Url;
      32             : import com.google.gerrit.server.CurrentUser;
      33             : import com.google.gerrit.server.permissions.PermissionBackendException;
      34             : import com.google.gerrit.server.project.DashboardResource;
      35             : import com.google.gerrit.server.project.ProjectResource;
      36             : import com.google.gerrit.server.project.ProjectState;
      37             : import com.google.inject.Inject;
      38             : import java.io.IOException;
      39             : import java.util.List;
      40             : import org.eclipse.jgit.errors.ConfigInvalidException;
      41             : import org.kohsuke.args4j.Option;
      42             : 
      43             : public class GetDashboard implements RestReadView<DashboardResource> {
      44             :   private final DashboardsCollection dashboards;
      45             : 
      46             :   @Option(name = "--inherited", usage = "include inherited dashboards")
      47             :   private boolean inherited;
      48             : 
      49             :   @Inject
      50           2 :   GetDashboard(DashboardsCollection dashboards) {
      51           2 :     this.dashboards = dashboards;
      52           2 :   }
      53             : 
      54             :   public GetDashboard setInherited(boolean inherited) {
      55           1 :     this.inherited = inherited;
      56           1 :     return this;
      57             :   }
      58             : 
      59             :   @Override
      60             :   public Response<DashboardInfo> apply(DashboardResource rsrc)
      61             :       throws RestApiException, IOException, PermissionBackendException {
      62           2 :     if (inherited && !rsrc.isProjectDefault()) {
      63           1 :       throw new BadRequestException("inherited flag can only be used with default");
      64             :     }
      65             : 
      66           2 :     if (rsrc.isProjectDefault()) {
      67             :       // The default is not resolved to a definition yet.
      68             :       try {
      69           2 :         rsrc = defaultOf(rsrc.getProjectState(), rsrc.getUser());
      70           0 :       } catch (ConfigInvalidException e) {
      71           0 :         throw new ResourceConflictException(e.getMessage());
      72           2 :       }
      73             :     }
      74             : 
      75           2 :     return Response.ok(
      76           2 :         DashboardsCollection.parse(
      77           2 :             rsrc.getProjectState().getProject(),
      78           2 :             rsrc.getRefName().substring(REFS_DASHBOARDS.length()),
      79           2 :             rsrc.getPathName(),
      80           2 :             rsrc.getConfig(),
      81           2 :             rsrc.getProjectState().getName(),
      82             :             true));
      83             :   }
      84             : 
      85             :   private DashboardResource defaultOf(ProjectState projectState, CurrentUser user)
      86             :       throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
      87           2 :     String id = projectState.getProject().getLocalDefaultDashboard();
      88           2 :     if (Strings.isNullOrEmpty(id)) {
      89           1 :       id = projectState.getProject().getDefaultDashboard();
      90             :     }
      91           2 :     if (isDefaultDashboard(id)) {
      92           0 :       throw new ResourceNotFoundException();
      93           2 :     } else if (!Strings.isNullOrEmpty(id)) {
      94           2 :       return parse(projectState, user, id);
      95           1 :     } else if (!inherited) {
      96           1 :       throw new ResourceNotFoundException();
      97             :     }
      98             : 
      99           0 :     for (ProjectState ps : projectState.tree()) {
     100           0 :       id = ps.getProject().getDefaultDashboard();
     101           0 :       if (isDefaultDashboard(id)) {
     102           0 :         throw new ResourceNotFoundException();
     103           0 :       } else if (!Strings.isNullOrEmpty(id)) {
     104           0 :         return parse(projectState, user, id);
     105             :       }
     106           0 :     }
     107           0 :     throw new ResourceNotFoundException();
     108             :   }
     109             : 
     110             :   private DashboardResource parse(ProjectState projectState, CurrentUser user, String id)
     111             :       throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
     112           2 :     List<String> p = Lists.newArrayList(Splitter.on(':').limit(2).split(id));
     113           2 :     String ref = Url.encode(p.get(0));
     114           2 :     String path = Url.encode(p.get(1));
     115           2 :     return dashboards.parse(
     116           2 :         new ProjectResource(projectState, user), IdString.fromUrl(ref + ':' + path));
     117             :   }
     118             : }

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