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 com.google.gerrit.entities.Change; 18 : import com.google.gerrit.extensions.api.changes.NotifyHandling; 19 : import com.google.gerrit.extensions.api.changes.ReviewerInput; 20 : import com.google.gerrit.extensions.api.changes.ReviewerResult; 21 : import com.google.gerrit.extensions.restapi.BadRequestException; 22 : import com.google.gerrit.extensions.restapi.Response; 23 : import com.google.gerrit.extensions.restapi.RestApiException; 24 : import com.google.gerrit.extensions.restapi.RestCollectionModifyView; 25 : import com.google.gerrit.server.change.ChangeResource; 26 : import com.google.gerrit.server.change.NotifyResolver; 27 : import com.google.gerrit.server.change.ReviewerModifier; 28 : import com.google.gerrit.server.change.ReviewerModifier.ReviewerModification; 29 : import com.google.gerrit.server.change.ReviewerResource; 30 : import com.google.gerrit.server.permissions.PermissionBackendException; 31 : import com.google.gerrit.server.query.change.ChangeData; 32 : import com.google.gerrit.server.update.BatchUpdate; 33 : import com.google.gerrit.server.update.UpdateException; 34 : import com.google.gerrit.server.util.time.TimeUtil; 35 : import com.google.inject.Inject; 36 : import com.google.inject.Singleton; 37 : import java.io.IOException; 38 : import org.eclipse.jgit.errors.ConfigInvalidException; 39 : 40 : @Singleton 41 : public class PostReviewers 42 : implements RestCollectionModifyView<ChangeResource, ReviewerResource, ReviewerInput> { 43 : private final BatchUpdate.Factory updateFactory; 44 : private final ChangeData.Factory changeDataFactory; 45 : private final NotifyResolver notifyResolver; 46 : private final ReviewerModifier reviewerModifier; 47 : 48 : @Inject 49 : PostReviewers( 50 : BatchUpdate.Factory updateFactory, 51 : ChangeData.Factory changeDataFactory, 52 : NotifyResolver notifyResolver, 53 145 : ReviewerModifier reviewerModifier) { 54 145 : this.updateFactory = updateFactory; 55 145 : this.changeDataFactory = changeDataFactory; 56 145 : this.notifyResolver = notifyResolver; 57 145 : this.reviewerModifier = reviewerModifier; 58 145 : } 59 : 60 : @Override 61 : public Response<ReviewerResult> apply(ChangeResource rsrc, ReviewerInput input) 62 : throws IOException, RestApiException, UpdateException, PermissionBackendException, 63 : ConfigInvalidException { 64 30 : if (input.reviewer == null) { 65 1 : throw new BadRequestException("missing reviewer field"); 66 : } 67 : 68 30 : ReviewerModification modification = 69 30 : reviewerModifier.prepare(rsrc.getNotes(), rsrc.getUser(), input, true); 70 30 : if (modification.op == null) { 71 2 : return Response.ok(modification.result); 72 : } 73 30 : try (BatchUpdate bu = updateFactory.create(rsrc.getProject(), rsrc.getUser(), TimeUtil.now())) { 74 30 : bu.setNotify(resolveNotify(rsrc, input)); 75 30 : Change.Id id = rsrc.getChange().getId(); 76 30 : bu.addOp(id, modification.op); 77 30 : bu.execute(); 78 : } 79 : 80 : // Re-read change to take into account results of the update. 81 30 : modification.gatherResults(changeDataFactory.create(rsrc.getProject(), rsrc.getId())); 82 30 : return Response.ok(modification.result); 83 : } 84 : 85 : private NotifyResolver.Result resolveNotify(ChangeResource rsrc, ReviewerInput input) 86 : throws BadRequestException, ConfigInvalidException, IOException { 87 30 : NotifyHandling notifyHandling = input.notify; 88 30 : if (notifyHandling == null) { 89 : notifyHandling = 90 30 : rsrc.getChange().isWorkInProgress() ? NotifyHandling.NONE : NotifyHandling.ALL; 91 : } 92 30 : return notifyResolver.resolve(notifyHandling, input.notifyDetails); 93 : } 94 : }