Line data Source code
1 : // Copyright (C) 2019 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.project; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.exceptions.InvalidNameException; 19 : import com.google.gerrit.extensions.common.ChangeInfo; 20 : import com.google.gerrit.extensions.common.ChangeInput; 21 : import com.google.gerrit.extensions.restapi.AuthException; 22 : import com.google.gerrit.extensions.restapi.BadRequestException; 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.server.CurrentUser; 27 : import com.google.gerrit.server.permissions.PermissionBackendException; 28 : import com.google.gerrit.server.project.InvalidChangeOperationException; 29 : import com.google.gerrit.server.project.ProjectResource; 30 : import com.google.gerrit.server.update.BatchUpdate; 31 : import com.google.gerrit.server.update.UpdateException; 32 : import com.google.inject.Inject; 33 : import com.google.inject.Provider; 34 : import com.google.inject.Singleton; 35 : import java.io.IOException; 36 : import org.eclipse.jgit.errors.ConfigInvalidException; 37 : 38 : @Singleton 39 : public class CreateChange implements RestModifyView<ProjectResource, ChangeInput> { 40 : private final com.google.gerrit.server.restapi.change.CreateChange changeCreateChange; 41 : private final Provider<CurrentUser> user; 42 : private final BatchUpdate.Factory updateFactory; 43 : 44 : @Inject 45 : public CreateChange( 46 : Provider<CurrentUser> user, 47 : BatchUpdate.Factory updateFactory, 48 144 : com.google.gerrit.server.restapi.change.CreateChange changeCreateChange) { 49 144 : this.updateFactory = updateFactory; 50 144 : this.changeCreateChange = changeCreateChange; 51 144 : this.user = user; 52 144 : } 53 : 54 : @Override 55 : public Response<ChangeInfo> apply(ProjectResource rsrc, ChangeInput input) 56 : throws PermissionBackendException, IOException, ConfigInvalidException, 57 : InvalidChangeOperationException, InvalidNameException, UpdateException, RestApiException { 58 2 : if (!user.get().isIdentifiedUser()) { 59 0 : throw new AuthException("Authentication required"); 60 : } 61 : 62 2 : if (!Strings.isNullOrEmpty(input.project) && !rsrc.getName().equals(input.project)) { 63 0 : throw new BadRequestException("project must match URL"); 64 : } 65 : 66 2 : input.project = rsrc.getName(); 67 1 : return changeCreateChange.execute(updateFactory, input, rsrc); 68 : } 69 : }