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.git.CommitUtil; 33 : import com.google.gerrit.server.permissions.ChangePermission; 34 : import com.google.gerrit.server.permissions.PermissionBackendException; 35 : import com.google.gerrit.server.update.BatchUpdate; 36 : import com.google.gerrit.server.update.UpdateException; 37 : import com.google.gerrit.server.util.time.TimeUtil; 38 : import com.google.inject.Inject; 39 : import com.google.inject.Singleton; 40 : 41 : @Singleton 42 : public class SetReadyForReview 43 : implements RestModifyView<ChangeResource, Input>, UiAction<ChangeResource> { 44 : private final BatchUpdate.Factory updateFactory; 45 : private final WorkInProgressOp.Factory opFactory; 46 : private final CommitUtil commitUtil; 47 : 48 : @Inject 49 : SetReadyForReview( 50 : BatchUpdate.Factory updateFactory, 51 : WorkInProgressOp.Factory opFactory, 52 145 : CommitUtil commitUtil) { 53 145 : this.updateFactory = updateFactory; 54 145 : this.opFactory = opFactory; 55 145 : this.commitUtil = commitUtil; 56 145 : } 57 : 58 : @Override 59 : public Response<String> apply(ChangeResource rsrc, Input input) 60 : throws RestApiException, UpdateException, PermissionBackendException { 61 8 : rsrc.permissions().check(ChangePermission.TOGGLE_WORK_IN_PROGRESS_STATE); 62 : 63 8 : Change change = rsrc.getChange(); 64 8 : if (!change.isNew()) { 65 0 : throw new ResourceConflictException("change is " + ChangeUtil.status(change)); 66 : } 67 : 68 8 : if (!change.isWorkInProgress()) { 69 0 : throw new ResourceConflictException("change is not work in progress"); 70 : } 71 : 72 8 : try (BatchUpdate bu = updateFactory.create(rsrc.getProject(), rsrc.getUser(), TimeUtil.now())) { 73 8 : bu.setNotify(NotifyResolver.Result.create(firstNonNull(input.notify, NotifyHandling.ALL))); 74 8 : bu.addOp(rsrc.getChange().getId(), opFactory.create(false, input)); 75 8 : if (change.getRevertOf() != null) { 76 1 : commitUtil.addChangeRevertedNotificationOps( 77 1 : bu, change.getRevertOf(), change.getId(), change.getKey().get()); 78 : } 79 8 : bu.execute(); 80 8 : return Response.ok(); 81 : } 82 : } 83 : 84 : @Override 85 : public Description getDescription(ChangeResource rsrc) { 86 57 : return new Description() 87 57 : .setLabel("Mark as Active") 88 57 : .setTitle("Switch change state from WIP to Active (ready for review)") 89 57 : .setVisible( 90 57 : and( 91 57 : rsrc.getChange().isNew() && rsrc.getChange().isWorkInProgress(), 92 57 : rsrc.permissions().testCond(ChangePermission.TOGGLE_WORK_IN_PROGRESS_STATE))); 93 : } 94 : }