LCOV - code coverage report
Current view: top level - server/api/projects - BranchApiImpl.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 34 34 100.0 %
Date: 2022-11-19 15:00:39 Functions: 7 7 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.api.projects;
      16             : 
      17             : import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
      18             : 
      19             : import com.google.gerrit.entities.RefNames;
      20             : import com.google.gerrit.extensions.api.projects.BranchApi;
      21             : import com.google.gerrit.extensions.api.projects.BranchInfo;
      22             : import com.google.gerrit.extensions.api.projects.BranchInput;
      23             : import com.google.gerrit.extensions.api.projects.ReflogEntryInfo;
      24             : import com.google.gerrit.extensions.common.Input;
      25             : import com.google.gerrit.extensions.restapi.BinaryResult;
      26             : import com.google.gerrit.extensions.restapi.IdString;
      27             : import com.google.gerrit.extensions.restapi.RestApiException;
      28             : import com.google.gerrit.server.permissions.PermissionBackendException;
      29             : import com.google.gerrit.server.project.BranchResource;
      30             : import com.google.gerrit.server.project.FileResource;
      31             : import com.google.gerrit.server.project.ProjectResource;
      32             : import com.google.gerrit.server.restapi.project.BranchesCollection;
      33             : import com.google.gerrit.server.restapi.project.CreateBranch;
      34             : import com.google.gerrit.server.restapi.project.DeleteBranch;
      35             : import com.google.gerrit.server.restapi.project.FilesCollection;
      36             : import com.google.gerrit.server.restapi.project.GetBranch;
      37             : import com.google.gerrit.server.restapi.project.GetContent;
      38             : import com.google.gerrit.server.restapi.project.GetReflog;
      39             : import com.google.inject.Inject;
      40             : import com.google.inject.assistedinject.Assisted;
      41             : import java.io.IOException;
      42             : import java.util.List;
      43             : 
      44             : public class BranchApiImpl implements BranchApi {
      45             :   interface Factory {
      46             :     BranchApiImpl create(ProjectResource project, String ref);
      47             :   }
      48             : 
      49             :   private final BranchesCollection branches;
      50             :   private final CreateBranch createBranch;
      51             :   private final DeleteBranch deleteBranch;
      52             :   private final FilesCollection filesCollection;
      53             :   private final GetBranch getBranch;
      54             :   private final GetContent getContent;
      55             :   private final GetReflog getReflog;
      56             :   private final String ref;
      57             :   private final ProjectResource project;
      58             : 
      59             :   @Inject
      60             :   BranchApiImpl(
      61             :       BranchesCollection branches,
      62             :       CreateBranch createBranch,
      63             :       DeleteBranch deleteBranch,
      64             :       FilesCollection filesCollection,
      65             :       GetBranch getBranch,
      66             :       GetContent getContent,
      67             :       GetReflog getReflog,
      68             :       @Assisted ProjectResource project,
      69          33 :       @Assisted String ref) {
      70          33 :     this.branches = branches;
      71          33 :     this.createBranch = createBranch;
      72          33 :     this.deleteBranch = deleteBranch;
      73          33 :     this.filesCollection = filesCollection;
      74          33 :     this.getBranch = getBranch;
      75          33 :     this.getContent = getContent;
      76          33 :     this.getReflog = getReflog;
      77          33 :     this.project = project;
      78          33 :     this.ref = ref;
      79          33 :   }
      80             : 
      81             :   @Override
      82             :   public BranchApi create(BranchInput input) throws RestApiException {
      83             :     try {
      84          28 :       createBranch.apply(project, IdString.fromDecoded(ref), input);
      85          28 :       return this;
      86           3 :     } catch (Exception e) {
      87           3 :       throw asRestApiException("Cannot create branch", e);
      88             :     }
      89             :   }
      90             : 
      91             :   @Override
      92             :   public BranchInfo get() throws RestApiException {
      93             :     try {
      94           9 :       return getBranch.apply(resource()).value();
      95           4 :     } catch (Exception e) {
      96           4 :       throw asRestApiException("Cannot read branch", e);
      97             :     }
      98             :   }
      99             : 
     100             :   @Override
     101             :   public void delete() throws RestApiException {
     102             :     try {
     103           3 :       deleteBranch.apply(resource(), new Input());
     104           2 :     } catch (Exception e) {
     105           2 :       throw asRestApiException("Cannot delete branch", e);
     106           3 :     }
     107           3 :   }
     108             : 
     109             :   @Override
     110             :   public BinaryResult file(String path) throws RestApiException {
     111             :     try {
     112           4 :       FileResource resource = filesCollection.parse(resource(), IdString.fromDecoded(path));
     113           4 :       return getContent.apply(resource).value();
     114           1 :     } catch (Exception e) {
     115           1 :       throw asRestApiException("Cannot retrieve file", e);
     116             :     }
     117             :   }
     118             : 
     119             :   @Override
     120             :   public List<ReflogEntryInfo> reflog() throws RestApiException {
     121             :     try {
     122           3 :       return getReflog.apply(resource()).value();
     123           1 :     } catch (Exception e) {
     124           1 :       throw asRestApiException("Cannot retrieve reflog", e);
     125             :     }
     126             :   }
     127             : 
     128             :   private BranchResource resource()
     129             :       throws RestApiException, IOException, PermissionBackendException {
     130          16 :     String refName = ref;
     131          16 :     if (RefNames.isRefsUsersSelf(ref, project.getProjectState().isAllUsers())) {
     132           1 :       refName = RefNames.refsUsers(project.getUser().getAccountId());
     133             :     }
     134          16 :     return branches.parse(project, IdString.fromDecoded(refName));
     135             :   }
     136             : }

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