LCOV - code coverage report
Current view: top level - server/api/changes - FileApiImpl.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 33 43 76.7 %
Date: 2022-11-19 15:00:39 Functions: 13 13 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2014 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.changes;
      16             : 
      17             : import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
      18             : 
      19             : import com.google.gerrit.extensions.api.changes.FileApi;
      20             : import com.google.gerrit.extensions.common.BlameInfo;
      21             : import com.google.gerrit.extensions.common.DiffInfo;
      22             : import com.google.gerrit.extensions.common.Input;
      23             : import com.google.gerrit.extensions.restapi.BinaryResult;
      24             : import com.google.gerrit.extensions.restapi.RestApiException;
      25             : import com.google.gerrit.server.change.FileResource;
      26             : import com.google.gerrit.server.restapi.change.GetBlame;
      27             : import com.google.gerrit.server.restapi.change.GetContent;
      28             : import com.google.gerrit.server.restapi.change.GetDiff;
      29             : import com.google.gerrit.server.restapi.change.Reviewed;
      30             : import com.google.inject.Inject;
      31             : import com.google.inject.Provider;
      32             : import com.google.inject.assistedinject.Assisted;
      33             : import java.util.List;
      34             : 
      35             : class FileApiImpl implements FileApi {
      36             :   interface Factory {
      37             :     FileApiImpl create(FileResource r);
      38             :   }
      39             : 
      40             :   private final GetContent getContent;
      41             :   private final Provider<GetBlame> getBlame;
      42             :   private final GetDiff getDiff;
      43             :   private final Reviewed.PutReviewed putReviewed;
      44             :   private final Reviewed.DeleteReviewed deleteReviewed;
      45             :   private final FileResource file;
      46             : 
      47             :   @Inject
      48             :   FileApiImpl(
      49             :       GetContent getContent,
      50             :       Provider<GetBlame> getBlame,
      51             :       GetDiff getDiff,
      52             :       Reviewed.PutReviewed putReviewed,
      53             :       Reviewed.DeleteReviewed deleteReviewed,
      54          14 :       @Assisted FileResource file) {
      55          14 :     this.getContent = getContent;
      56          14 :     this.getBlame = getBlame;
      57          14 :     this.getDiff = getDiff;
      58          14 :     this.putReviewed = putReviewed;
      59          14 :     this.deleteReviewed = deleteReviewed;
      60          14 :     this.file = file;
      61          14 :   }
      62             : 
      63             :   @Override
      64             :   public BinaryResult content() throws RestApiException {
      65             :     try {
      66          11 :       return getContent.apply(file).value();
      67           1 :     } catch (Exception e) {
      68           1 :       throw asRestApiException("Cannot retrieve file content", e);
      69             :     }
      70             :   }
      71             : 
      72             :   @Override
      73             :   public DiffInfo diff() throws RestApiException {
      74             :     try {
      75           5 :       return getDiff.apply(file).value();
      76           0 :     } catch (Exception e) {
      77           0 :       throw asRestApiException("Cannot retrieve diff", e);
      78             :     }
      79             :   }
      80             : 
      81             :   @Override
      82             :   public DiffInfo diff(String base) throws RestApiException {
      83             :     try {
      84           2 :       return getDiff.setBase(base).apply(file).value();
      85           0 :     } catch (Exception e) {
      86           0 :       throw asRestApiException("Cannot retrieve diff", e);
      87             :     }
      88             :   }
      89             : 
      90             :   @Override
      91             :   public DiffInfo diff(int parent) throws RestApiException {
      92             :     try {
      93           1 :       return getDiff.setParent(parent).apply(file).value();
      94           0 :     } catch (Exception e) {
      95           0 :       throw asRestApiException("Cannot retrieve diff", e);
      96             :     }
      97             :   }
      98             : 
      99             :   @Override
     100             :   public DiffRequest diffRequest() {
     101           3 :     return new DiffRequest() {
     102             :       @Override
     103             :       public DiffInfo get() throws RestApiException {
     104           3 :         return FileApiImpl.this.get(this);
     105             :       }
     106             :     };
     107             :   }
     108             : 
     109             :   @Override
     110             :   public void setReviewed(boolean reviewed) throws RestApiException {
     111             :     try {
     112           1 :       if (reviewed) {
     113           1 :         putReviewed.apply(file, new Input());
     114             :       } else {
     115           1 :         deleteReviewed.apply(file, new Input());
     116             :       }
     117           0 :     } catch (Exception e) {
     118           0 :       throw asRestApiException(String.format("Cannot set %sreviewed", reviewed ? "" : "un"), e);
     119           1 :     }
     120           1 :   }
     121             : 
     122             :   private DiffInfo get(DiffRequest r) throws RestApiException {
     123           3 :     if (r.getBase() != null) {
     124           3 :       getDiff.setBase(r.getBase());
     125             :     }
     126           3 :     if (r.getIntraline() != null) {
     127           2 :       getDiff.setIntraline(r.getIntraline());
     128             :     }
     129           3 :     if (r.getWhitespace() != null) {
     130           2 :       getDiff.setWhitespace(r.getWhitespace());
     131             :     }
     132           3 :     r.getParent().ifPresent(getDiff::setParent);
     133             :     try {
     134           3 :       return getDiff.apply(file).value();
     135           2 :     } catch (Exception e) {
     136           2 :       throw asRestApiException("Cannot retrieve diff", e);
     137             :     }
     138             :   }
     139             : 
     140             :   @Override
     141             :   public BlameRequest blameRequest() throws RestApiException {
     142           2 :     return new BlameRequest() {
     143             :       @Override
     144             :       public List<BlameInfo> get() throws RestApiException {
     145             :         try {
     146           2 :           return getBlame.get().setBase(isForBase()).apply(file).value();
     147           0 :         } catch (Exception e) {
     148           0 :           throw asRestApiException("Cannot retrieve blame", e);
     149             :         }
     150             :       }
     151             :     };
     152             :   }
     153             : }

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