LCOV - code coverage report
Current view: top level - server/restapi/change - SetWorkInProgress.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 20 22 90.9 %
Date: 2022-11-19 15:00:39 Functions: 3 3 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.restapi.change;
      16             : 
      17             : import static com.google.common.base.MoreObjects.firstNonNull;
      18             : import static com.google.gerrit.extensions.conditions.BooleanCondition.and;
      19             : 
      20             : import com.google.gerrit.entities.Change;
      21             : import com.google.gerrit.extensions.api.changes.NotifyHandling;
      22             : import com.google.gerrit.extensions.restapi.ResourceConflictException;
      23             : import com.google.gerrit.extensions.restapi.Response;
      24             : import com.google.gerrit.extensions.restapi.RestApiException;
      25             : import com.google.gerrit.extensions.restapi.RestModifyView;
      26             : import com.google.gerrit.extensions.webui.UiAction;
      27             : import com.google.gerrit.server.ChangeUtil;
      28             : import com.google.gerrit.server.change.ChangeResource;
      29             : import com.google.gerrit.server.change.NotifyResolver;
      30             : import com.google.gerrit.server.change.WorkInProgressOp;
      31             : import com.google.gerrit.server.change.WorkInProgressOp.Input;
      32             : import com.google.gerrit.server.permissions.ChangePermission;
      33             : import com.google.gerrit.server.permissions.PermissionBackendException;
      34             : import com.google.gerrit.server.update.BatchUpdate;
      35             : import com.google.gerrit.server.update.UpdateException;
      36             : import com.google.gerrit.server.util.time.TimeUtil;
      37             : import com.google.inject.Inject;
      38             : import com.google.inject.Singleton;
      39             : 
      40             : @Singleton
      41             : public class SetWorkInProgress
      42             :     implements RestModifyView<ChangeResource, Input>, UiAction<ChangeResource> {
      43             :   private final BatchUpdate.Factory updateFactory;
      44             :   private final WorkInProgressOp.Factory opFactory;
      45             : 
      46             :   @Inject
      47         145 :   SetWorkInProgress(BatchUpdate.Factory updateFactory, WorkInProgressOp.Factory opFactory) {
      48         145 :     this.updateFactory = updateFactory;
      49         145 :     this.opFactory = opFactory;
      50         145 :   }
      51             : 
      52             :   @Override
      53             :   public Response<String> apply(ChangeResource rsrc, Input input)
      54             :       throws RestApiException, UpdateException, PermissionBackendException {
      55          11 :     rsrc.permissions().check(ChangePermission.TOGGLE_WORK_IN_PROGRESS_STATE);
      56             : 
      57          11 :     Change change = rsrc.getChange();
      58          11 :     if (!change.isNew()) {
      59           0 :       throw new ResourceConflictException("change is " + ChangeUtil.status(change));
      60             :     }
      61             : 
      62          11 :     if (change.isWorkInProgress()) {
      63           0 :       throw new ResourceConflictException("change is already work in progress");
      64             :     }
      65             : 
      66          11 :     try (BatchUpdate bu = updateFactory.create(rsrc.getProject(), rsrc.getUser(), TimeUtil.now())) {
      67          11 :       bu.setNotify(NotifyResolver.Result.create(firstNonNull(input.notify, NotifyHandling.NONE)));
      68          11 :       bu.addOp(rsrc.getChange().getId(), opFactory.create(true, input));
      69          11 :       bu.execute();
      70          11 :       return Response.ok();
      71             :     }
      72             :   }
      73             : 
      74             :   @Override
      75             :   public Description getDescription(ChangeResource rsrc) {
      76          57 :     return new Description()
      77          57 :         .setLabel("WIP")
      78          57 :         .setTitle("Set Work In Progress")
      79          57 :         .setVisible(
      80          57 :             and(
      81          57 :                 rsrc.getChange().isNew() && !rsrc.getChange().isWorkInProgress(),
      82          57 :                 rsrc.permissions().testCond(ChangePermission.TOGGLE_WORK_IN_PROGRESS_STATE)));
      83             :   }
      84             : }

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