LCOV - code coverage report
Current view: top level - server/restapi/change - DeleteChange.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 21 21 100.0 %
Date: 2022-11-19 15:00:39 Functions: 4 4 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.restapi.change;
      16             : 
      17             : import static com.google.gerrit.extensions.conditions.BooleanCondition.and;
      18             : 
      19             : import com.google.gerrit.entities.Change;
      20             : import com.google.gerrit.extensions.common.Input;
      21             : import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
      22             : import com.google.gerrit.extensions.restapi.Response;
      23             : import com.google.gerrit.extensions.restapi.RestApiException;
      24             : import com.google.gerrit.extensions.restapi.RestModifyView;
      25             : import com.google.gerrit.extensions.webui.UiAction;
      26             : import com.google.gerrit.server.change.ChangeResource;
      27             : import com.google.gerrit.server.change.DeleteChangeOp;
      28             : import com.google.gerrit.server.permissions.ChangePermission;
      29             : import com.google.gerrit.server.permissions.PermissionBackend;
      30             : import com.google.gerrit.server.permissions.PermissionBackendException;
      31             : import com.google.gerrit.server.update.BatchUpdate;
      32             : import com.google.gerrit.server.update.UpdateException;
      33             : import com.google.gerrit.server.util.time.TimeUtil;
      34             : import com.google.inject.Inject;
      35             : import com.google.inject.Singleton;
      36             : 
      37             : @Singleton
      38             : public class DeleteChange
      39             :     implements RestModifyView<ChangeResource, Input>, UiAction<ChangeResource> {
      40             :   private final BatchUpdate.Factory updateFactory;
      41             :   private final DeleteChangeOp.Factory opFactory;
      42             : 
      43             :   @Inject
      44         145 :   public DeleteChange(BatchUpdate.Factory updateFactory, DeleteChangeOp.Factory opFactory) {
      45         145 :     this.updateFactory = updateFactory;
      46         145 :     this.opFactory = opFactory;
      47         145 :   }
      48             : 
      49             :   @Override
      50             :   public Response<Object> apply(ChangeResource rsrc, Input input)
      51             :       throws RestApiException, UpdateException, PermissionBackendException {
      52          11 :     if (!isChangeDeletable(rsrc)) {
      53           1 :       throw new MethodNotAllowedException("delete not permitted");
      54             :     }
      55          11 :     rsrc.permissions().check(ChangePermission.DELETE);
      56             : 
      57          11 :     try (BatchUpdate bu = updateFactory.create(rsrc.getProject(), rsrc.getUser(), TimeUtil.now())) {
      58          11 :       Change.Id id = rsrc.getChange().getId();
      59          11 :       bu.addOp(id, opFactory.create(id));
      60          11 :       bu.execute();
      61             :     }
      62          11 :     return Response.none();
      63             :   }
      64             : 
      65             :   @Override
      66             :   public UiAction.Description getDescription(ChangeResource rsrc) {
      67          57 :     PermissionBackend.ForChange perm = rsrc.permissions();
      68          57 :     return new UiAction.Description()
      69          57 :         .setLabel("Delete")
      70          57 :         .setTitle("Delete change " + rsrc.getId())
      71          57 :         .setVisible(and(isChangeDeletable(rsrc), perm.testCond(ChangePermission.DELETE)));
      72             :   }
      73             : 
      74             :   private static boolean isChangeDeletable(ChangeResource rsrc) {
      75          58 :     Change change = rsrc.getChange();
      76          58 :     if (change.isMerged()) {
      77             :       // Merged changes should never be deleted.
      78          25 :       return false;
      79             :     }
      80             :     // New or abandoned changes can be deleted with the right permissions.
      81          54 :     return true;
      82             :   }
      83             : }

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