LCOV - code coverage report
Current view: top level - server/api/projects - CommitApiImpl.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 14 20 70.0 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          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.api.projects;
      16             : 
      17             : import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
      18             : 
      19             : import com.google.gerrit.extensions.api.changes.ChangeApi;
      20             : import com.google.gerrit.extensions.api.changes.Changes;
      21             : import com.google.gerrit.extensions.api.changes.CherryPickInput;
      22             : import com.google.gerrit.extensions.api.changes.IncludedInInfo;
      23             : import com.google.gerrit.extensions.api.projects.CommitApi;
      24             : import com.google.gerrit.extensions.common.CommitInfo;
      25             : import com.google.gerrit.extensions.common.FileInfo;
      26             : import com.google.gerrit.extensions.restapi.RestApiException;
      27             : import com.google.gerrit.server.project.CommitResource;
      28             : import com.google.gerrit.server.restapi.change.CherryPickCommit;
      29             : import com.google.gerrit.server.restapi.project.CommitIncludedIn;
      30             : import com.google.gerrit.server.restapi.project.FilesInCommitCollection;
      31             : import com.google.gerrit.server.restapi.project.GetCommit;
      32             : import com.google.inject.Inject;
      33             : import com.google.inject.assistedinject.Assisted;
      34             : import java.util.Map;
      35             : 
      36             : public class CommitApiImpl implements CommitApi {
      37             :   public interface Factory {
      38             :     CommitApiImpl create(CommitResource r);
      39             :   }
      40             : 
      41             :   private final Changes changes;
      42             :   private final GetCommit getCommit;
      43             :   private final CherryPickCommit cherryPickCommit;
      44             :   private final CommitIncludedIn includedIn;
      45             :   private final CommitResource commitResource;
      46             :   private final FilesInCommitCollection.ListFiles listFiles;
      47             : 
      48             :   @Inject
      49             :   CommitApiImpl(
      50             :       Changes changes,
      51             :       GetCommit getCommit,
      52             :       CherryPickCommit cherryPickCommit,
      53             :       CommitIncludedIn includedIn,
      54             :       FilesInCommitCollection.ListFiles listFiles,
      55           3 :       @Assisted CommitResource commitResource) {
      56           3 :     this.changes = changes;
      57           3 :     this.getCommit = getCommit;
      58           3 :     this.cherryPickCommit = cherryPickCommit;
      59           3 :     this.includedIn = includedIn;
      60           3 :     this.listFiles = listFiles;
      61           3 :     this.commitResource = commitResource;
      62           3 :   }
      63             : 
      64             :   @Override
      65             :   public CommitInfo get() throws RestApiException {
      66             :     try {
      67           1 :       return getCommit.apply(commitResource).value();
      68           0 :     } catch (Exception e) {
      69           0 :       throw asRestApiException("Cannot get commit info", e);
      70             :     }
      71             :   }
      72             : 
      73             :   @Override
      74             :   public ChangeApi cherryPick(CherryPickInput input) throws RestApiException {
      75             :     try {
      76           2 :       return changes.id(cherryPickCommit.apply(commitResource, input).value()._number);
      77           1 :     } catch (Exception e) {
      78           1 :       throw asRestApiException("Cannot cherry pick", e);
      79             :     }
      80             :   }
      81             : 
      82             :   @Override
      83             :   public IncludedInInfo includedIn() throws RestApiException {
      84             :     try {
      85           1 :       return includedIn.apply(commitResource).value();
      86           0 :     } catch (Exception e) {
      87           0 :       throw asRestApiException("Could not extract IncludedIn data", e);
      88             :     }
      89             :   }
      90             : 
      91             :   @Override
      92             :   public Map<String, FileInfo> files(int parentNum) throws RestApiException {
      93             :     try {
      94           1 :       return listFiles.setParent(parentNum).apply(commitResource).value();
      95           0 :     } catch (Exception e) {
      96           0 :       throw asRestApiException("Cannot retrieve files", e);
      97             :     }
      98             :   }
      99             : }

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