LCOV - code coverage report
Current view: top level - server/api/changes - ChangeEditApiImpl.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 78 96 81.2 %
Date: 2022-11-19 15:00:39 Functions: 19 19 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.changes;
      16             : 
      17             : import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
      18             : 
      19             : import com.google.gerrit.extensions.api.changes.ChangeEditApi;
      20             : import com.google.gerrit.extensions.api.changes.FileContentInput;
      21             : import com.google.gerrit.extensions.api.changes.PublishChangeEditInput;
      22             : import com.google.gerrit.extensions.client.ChangeEditDetailOption;
      23             : import com.google.gerrit.extensions.common.EditInfo;
      24             : import com.google.gerrit.extensions.common.Input;
      25             : import com.google.gerrit.extensions.restapi.AuthException;
      26             : import com.google.gerrit.extensions.restapi.BinaryResult;
      27             : import com.google.gerrit.extensions.restapi.IdString;
      28             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      29             : import com.google.gerrit.extensions.restapi.Response;
      30             : import com.google.gerrit.extensions.restapi.RestApiException;
      31             : import com.google.gerrit.server.change.ChangeEditResource;
      32             : import com.google.gerrit.server.change.ChangeResource;
      33             : import com.google.gerrit.server.restapi.change.ChangeEdits;
      34             : import com.google.gerrit.server.restapi.change.DeleteChangeEdit;
      35             : import com.google.gerrit.server.restapi.change.PublishChangeEdit;
      36             : import com.google.gerrit.server.restapi.change.RebaseChangeEdit;
      37             : import com.google.inject.Inject;
      38             : import com.google.inject.Provider;
      39             : import com.google.inject.assistedinject.Assisted;
      40             : import java.io.IOException;
      41             : import java.util.Optional;
      42             : 
      43             : public class ChangeEditApiImpl implements ChangeEditApi {
      44             :   interface Factory {
      45             :     ChangeEditApiImpl create(ChangeResource changeResource);
      46             :   }
      47             : 
      48             :   private final Provider<ChangeEdits.Detail> editDetailProvider;
      49             :   private final ChangeEdits.Post changeEditsPost;
      50             :   private final DeleteChangeEdit deleteChangeEdit;
      51             :   private final RebaseChangeEdit rebaseChangeEdit;
      52             :   private final PublishChangeEdit publishChangeEdit;
      53             :   private final Provider<ChangeEdits.Get> changeEditsGetProvider;
      54             :   private final ChangeEdits.Put changeEditsPut;
      55             :   private final ChangeEdits.DeleteContent changeEditDeleteContent;
      56             :   private final Provider<ChangeEdits.GetMessage> getChangeEditCommitMessageProvider;
      57             :   private final ChangeEdits.EditMessage modifyChangeEditCommitMessage;
      58             :   private final ChangeEdits changeEdits;
      59             :   private final ChangeResource changeResource;
      60             : 
      61             :   @Inject
      62             :   public ChangeEditApiImpl(
      63             :       Provider<ChangeEdits.Detail> editDetailProvider,
      64             :       ChangeEdits.Post changeEditsPost,
      65             :       DeleteChangeEdit deleteChangeEdit,
      66             :       RebaseChangeEdit rebaseChangeEdit,
      67             :       PublishChangeEdit publishChangeEdit,
      68             :       Provider<ChangeEdits.Get> changeEditsGetProvider,
      69             :       ChangeEdits.Put changeEditsPut,
      70             :       ChangeEdits.DeleteContent changeEditDeleteContent,
      71             :       Provider<ChangeEdits.GetMessage> getChangeEditCommitMessageProvider,
      72             :       ChangeEdits.EditMessage modifyChangeEditCommitMessage,
      73             :       ChangeEdits changeEdits,
      74          27 :       @Assisted ChangeResource changeResource) {
      75          27 :     this.editDetailProvider = editDetailProvider;
      76          27 :     this.changeEditsPost = changeEditsPost;
      77          27 :     this.deleteChangeEdit = deleteChangeEdit;
      78          27 :     this.rebaseChangeEdit = rebaseChangeEdit;
      79          27 :     this.publishChangeEdit = publishChangeEdit;
      80          27 :     this.changeEditsGetProvider = changeEditsGetProvider;
      81          27 :     this.changeEditsPut = changeEditsPut;
      82          27 :     this.changeEditDeleteContent = changeEditDeleteContent;
      83          27 :     this.getChangeEditCommitMessageProvider = getChangeEditCommitMessageProvider;
      84          27 :     this.modifyChangeEditCommitMessage = modifyChangeEditCommitMessage;
      85          27 :     this.changeEdits = changeEdits;
      86          27 :     this.changeResource = changeResource;
      87          27 :   }
      88             : 
      89             :   @Override
      90             :   public ChangeEditDetailRequest detail() throws RestApiException {
      91             :     try {
      92           1 :       return new ChangeEditDetailRequest() {
      93             :         @Override
      94             :         public Optional<EditInfo> get() throws RestApiException {
      95           1 :           return ChangeEditApiImpl.this.get(this);
      96             :         }
      97             :       };
      98           0 :     } catch (Exception e) {
      99           0 :       throw asRestApiException("Cannot retrieve change edit", e);
     100             :     }
     101             :   }
     102             : 
     103             :   private Optional<EditInfo> get(ChangeEditDetailRequest r) throws RestApiException {
     104             :     try {
     105           1 :       ChangeEdits.Detail editDetail = editDetailProvider.get();
     106           1 :       editDetail.setBase(r.getBase());
     107           1 :       editDetail.setList(r.options().contains(ChangeEditDetailOption.LIST_FILES));
     108           1 :       editDetail.setDownloadCommands(
     109           1 :           r.options().contains(ChangeEditDetailOption.DOWNLOAD_COMMANDS));
     110           1 :       Response<EditInfo> edit = editDetail.apply(changeResource);
     111           1 :       return edit.isNone() ? Optional.empty() : Optional.of(edit.value());
     112           0 :     } catch (Exception e) {
     113           0 :       throw asRestApiException("Cannot retrieve change edit", e);
     114             :     }
     115             :   }
     116             : 
     117             :   @Override
     118             :   public Optional<EditInfo> get() throws RestApiException {
     119             :     try {
     120           8 :       Response<EditInfo> edit = editDetailProvider.get().apply(changeResource);
     121           8 :       return edit.isNone() ? Optional.empty() : Optional.of(edit.value());
     122           0 :     } catch (Exception e) {
     123           0 :       throw asRestApiException("Cannot retrieve change edit", e);
     124             :     }
     125             :   }
     126             : 
     127             :   @Override
     128             :   public void create() throws RestApiException {
     129             :     try {
     130          18 :       changeEditsPost.apply(changeResource, null);
     131           1 :     } catch (Exception e) {
     132           1 :       throw asRestApiException("Cannot create change edit", e);
     133          18 :     }
     134          18 :   }
     135             : 
     136             :   @Override
     137             :   public void delete() throws RestApiException {
     138             :     try {
     139           1 :       deleteChangeEdit.apply(changeResource, new Input());
     140           0 :     } catch (Exception e) {
     141           0 :       throw asRestApiException("Cannot delete change edit", e);
     142           1 :     }
     143           1 :   }
     144             : 
     145             :   @Override
     146             :   public void rebase() throws RestApiException {
     147             :     try {
     148           1 :       rebaseChangeEdit.apply(changeResource, null);
     149           0 :     } catch (Exception e) {
     150           0 :       throw asRestApiException("Cannot rebase change edit", e);
     151           1 :     }
     152           1 :   }
     153             : 
     154             :   @Override
     155             :   public void publish() throws RestApiException {
     156          16 :     publish(null);
     157          16 :   }
     158             : 
     159             :   @Override
     160             :   public void publish(PublishChangeEditInput publishChangeEditInput) throws RestApiException {
     161             :     try {
     162          18 :       publishChangeEdit.apply(changeResource, publishChangeEditInput);
     163           0 :     } catch (Exception e) {
     164           0 :       throw asRestApiException("Cannot publish change edit", e);
     165          18 :     }
     166          18 :   }
     167             : 
     168             :   @Override
     169             :   public Optional<BinaryResult> getFile(String filePath) throws RestApiException {
     170             :     try {
     171           3 :       ChangeEditResource changeEditResource = getChangeEditResource(filePath);
     172           3 :       Response<BinaryResult> fileResponse = changeEditsGetProvider.get().apply(changeEditResource);
     173           3 :       return fileResponse.isNone() ? Optional.empty() : Optional.of(fileResponse.value());
     174           0 :     } catch (Exception e) {
     175           0 :       throw asRestApiException("Cannot retrieve file of change edit", e);
     176             :     }
     177             :   }
     178             : 
     179             :   @Override
     180             :   public void renameFile(String oldFilePath, String newFilePath) throws RestApiException {
     181             :     try {
     182           3 :       ChangeEdits.Post.Input renameInput = new ChangeEdits.Post.Input();
     183           3 :       renameInput.oldPath = oldFilePath;
     184           3 :       renameInput.newPath = newFilePath;
     185           3 :       changeEditsPost.apply(changeResource, renameInput);
     186           1 :     } catch (Exception e) {
     187           1 :       throw asRestApiException("Cannot rename file of change edit", e);
     188           3 :     }
     189           3 :   }
     190             : 
     191             :   @Override
     192             :   public void restoreFile(String filePath) throws RestApiException {
     193             :     try {
     194           1 :       ChangeEdits.Post.Input restoreInput = new ChangeEdits.Post.Input();
     195           1 :       restoreInput.restorePath = filePath;
     196           1 :       changeEditsPost.apply(changeResource, restoreInput);
     197           0 :     } catch (Exception e) {
     198           0 :       throw asRestApiException("Cannot restore file of change edit", e);
     199           1 :     }
     200           1 :   }
     201             : 
     202             :   @Override
     203             :   public void modifyFile(String filePath, FileContentInput input) throws RestApiException {
     204             :     try {
     205          17 :       changeEditsPut.apply(changeResource, filePath, input);
     206           2 :     } catch (Exception e) {
     207           2 :       throw asRestApiException("Cannot modify file of change edit", e);
     208          17 :     }
     209          17 :   }
     210             : 
     211             :   @Override
     212             :   public void deleteFile(String filePath) throws RestApiException {
     213             :     try {
     214           5 :       changeEditDeleteContent.apply(changeResource, filePath);
     215           1 :     } catch (Exception e) {
     216           1 :       throw asRestApiException("Cannot delete file of change edit", e);
     217           5 :     }
     218           5 :   }
     219             : 
     220             :   @Override
     221             :   public String getCommitMessage() throws RestApiException {
     222             :     try {
     223           3 :       try (BinaryResult binaryResult =
     224           3 :           getChangeEditCommitMessageProvider.get().apply(changeResource).value()) {
     225           3 :         return binaryResult.asString();
     226             :       }
     227           0 :     } catch (Exception e) {
     228           0 :       throw asRestApiException("Cannot get commit message of change edit", e);
     229             :     }
     230             :   }
     231             : 
     232             :   @Override
     233             :   public void modifyCommitMessage(String newCommitMessage) throws RestApiException {
     234           6 :     ChangeEdits.EditMessage.Input input = new ChangeEdits.EditMessage.Input();
     235           6 :     input.message = newCommitMessage;
     236             :     try {
     237           6 :       modifyChangeEditCommitMessage.apply(changeResource, input);
     238           1 :     } catch (Exception e) {
     239           1 :       throw asRestApiException("Cannot modify commit message of change edit", e);
     240           6 :     }
     241           6 :   }
     242             : 
     243             :   private ChangeEditResource getChangeEditResource(String filePath)
     244             :       throws ResourceNotFoundException, AuthException, IOException {
     245           3 :     return changeEdits.parse(changeResource, IdString.fromDecoded(filePath));
     246             :   }
     247             : }

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