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 com.google.common.base.Strings; 18 : import com.google.gerrit.entities.BranchNameKey; 19 : import com.google.gerrit.entities.Project; 20 : import com.google.gerrit.entities.RefNames; 21 : import com.google.gerrit.extensions.api.changes.CherryPickInput; 22 : import com.google.gerrit.extensions.common.ChangeInfo; 23 : import com.google.gerrit.extensions.restapi.BadRequestException; 24 : import com.google.gerrit.extensions.restapi.Response; 25 : import com.google.gerrit.extensions.restapi.RestApiException; 26 : import com.google.gerrit.extensions.restapi.RestModifyView; 27 : import com.google.gerrit.server.CurrentUser; 28 : import com.google.gerrit.server.change.ChangeJson; 29 : import com.google.gerrit.server.permissions.PermissionBackend; 30 : import com.google.gerrit.server.permissions.PermissionBackendException; 31 : import com.google.gerrit.server.permissions.RefPermission; 32 : import com.google.gerrit.server.project.CommitResource; 33 : import com.google.gerrit.server.project.ContributorAgreementsChecker; 34 : import com.google.gerrit.server.project.InvalidChangeOperationException; 35 : import com.google.gerrit.server.project.NoSuchProjectException; 36 : import com.google.gerrit.server.update.UpdateException; 37 : import com.google.inject.Inject; 38 : import com.google.inject.Provider; 39 : import com.google.inject.Singleton; 40 : import java.io.IOException; 41 : import org.eclipse.jgit.errors.ConfigInvalidException; 42 : 43 : @Singleton 44 : public class CherryPickCommit implements RestModifyView<CommitResource, CherryPickInput> { 45 : private final PermissionBackend permissionBackend; 46 : private final Provider<CurrentUser> user; 47 : private final CherryPickChange cherryPickChange; 48 : private final ChangeJson.Factory json; 49 : private final ContributorAgreementsChecker contributorAgreements; 50 : 51 : @Inject 52 : CherryPickCommit( 53 : PermissionBackend permissionBackend, 54 : Provider<CurrentUser> user, 55 : CherryPickChange cherryPickChange, 56 : ChangeJson.Factory json, 57 138 : ContributorAgreementsChecker contributorAgreements) { 58 138 : this.permissionBackend = permissionBackend; 59 138 : this.user = user; 60 138 : this.cherryPickChange = cherryPickChange; 61 138 : this.json = json; 62 138 : this.contributorAgreements = contributorAgreements; 63 138 : } 64 : 65 : @Override 66 : public Response<ChangeInfo> apply(CommitResource rsrc, CherryPickInput input) 67 : throws IOException, UpdateException, RestApiException, PermissionBackendException, 68 : ConfigInvalidException, NoSuchProjectException { 69 3 : String destination = Strings.nullToEmpty(input.destination).trim(); 70 3 : input.parent = input.parent == null ? 1 : input.parent; 71 3 : Project.NameKey projectName = rsrc.getProjectState().getNameKey(); 72 : 73 3 : if (destination.isEmpty()) { 74 1 : throw new BadRequestException("destination must be non-empty"); 75 : } 76 : 77 2 : String refName = RefNames.fullName(destination); 78 2 : contributorAgreements.check(projectName, user.get()); 79 2 : permissionBackend 80 2 : .currentUser() 81 2 : .project(projectName) 82 2 : .ref(refName) 83 2 : .check(RefPermission.CREATE_CHANGE); 84 2 : rsrc.getProjectState().checkStatePermitsWrite(); 85 : 86 : try { 87 2 : CherryPickChange.Result cherryPickResult = 88 2 : cherryPickChange.cherryPick( 89 : null, 90 : projectName, 91 2 : rsrc.getCommit(), 92 : input, 93 2 : BranchNameKey.create(rsrc.getProjectState().getNameKey(), refName)); 94 2 : ChangeInfo changeInfo = json.noOptions().format(projectName, cherryPickResult.changeId()); 95 2 : changeInfo.containsGitConflicts = 96 2 : !cherryPickResult.filesWithGitConflicts().isEmpty() ? true : null; 97 2 : return Response.ok(changeInfo); 98 1 : } catch (InvalidChangeOperationException e) { 99 1 : throw new BadRequestException(e.getMessage()); 100 : } 101 : } 102 : }