Line data Source code
1 : // Copyright (C) 2014 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 : 19 : import com.google.gerrit.extensions.api.changes.NotifyHandling; 20 : import com.google.gerrit.extensions.api.changes.PublishChangeEditInput; 21 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 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.server.change.ChangeResource; 26 : import com.google.gerrit.server.change.NotifyResolver; 27 : import com.google.gerrit.server.edit.ChangeEdit; 28 : import com.google.gerrit.server.edit.ChangeEditUtil; 29 : import com.google.gerrit.server.project.ContributorAgreementsChecker; 30 : import com.google.gerrit.server.project.NoSuchProjectException; 31 : import com.google.gerrit.server.update.BatchUpdate; 32 : import com.google.gerrit.server.update.UpdateException; 33 : import com.google.inject.Inject; 34 : import com.google.inject.Singleton; 35 : import java.io.IOException; 36 : import java.util.Optional; 37 : import org.eclipse.jgit.errors.ConfigInvalidException; 38 : 39 : @Singleton 40 : public class PublishChangeEdit implements RestModifyView<ChangeResource, PublishChangeEditInput> { 41 : private final BatchUpdate.Factory updateFactory; 42 : private final ChangeEditUtil editUtil; 43 : private final NotifyResolver notifyResolver; 44 : private final ContributorAgreementsChecker contributorAgreementsChecker; 45 : 46 : @Inject 47 : PublishChangeEdit( 48 : BatchUpdate.Factory updateFactory, 49 : ChangeEditUtil editUtil, 50 : NotifyResolver notifyResolver, 51 145 : ContributorAgreementsChecker contributorAgreementsChecker) { 52 145 : this.updateFactory = updateFactory; 53 145 : this.editUtil = editUtil; 54 145 : this.notifyResolver = notifyResolver; 55 145 : this.contributorAgreementsChecker = contributorAgreementsChecker; 56 145 : } 57 : 58 : @Override 59 : public Response<Object> apply(ChangeResource rsrc, PublishChangeEditInput in) 60 : throws IOException, RestApiException, UpdateException, ConfigInvalidException, 61 : NoSuchProjectException { 62 20 : contributorAgreementsChecker.check(rsrc.getProject(), rsrc.getUser()); 63 20 : Optional<ChangeEdit> edit = editUtil.byChange(rsrc.getNotes(), rsrc.getUser()); 64 20 : if (!edit.isPresent()) { 65 0 : throw new ResourceConflictException( 66 0 : String.format("no edit exists for change %s", rsrc.getChange().getChangeId())); 67 : } 68 20 : if (in == null) { 69 16 : in = new PublishChangeEditInput(); 70 : } 71 20 : editUtil.publish( 72 : updateFactory, 73 20 : rsrc.getNotes(), 74 20 : rsrc.getUser(), 75 20 : edit.get(), 76 20 : notifyResolver.resolve(firstNonNull(in.notify, NotifyHandling.ALL), in.notifyDetails)); 77 19 : return Response.none(); 78 : } 79 : }