Line data Source code
1 : // Copyright (C) 2015 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 static org.eclipse.jgit.lib.Constants.R_HEADS; 18 : 19 : import com.google.common.collect.ImmutableSet; 20 : import com.google.gerrit.entities.RefNames; 21 : import com.google.gerrit.extensions.api.projects.DeleteBranchesInput; 22 : import com.google.gerrit.extensions.restapi.BadRequestException; 23 : import com.google.gerrit.extensions.restapi.MethodNotAllowedException; 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.permissions.PermissionBackendException; 28 : import com.google.gerrit.server.project.ProjectResource; 29 : import com.google.inject.Inject; 30 : import com.google.inject.Singleton; 31 : import java.io.IOException; 32 : 33 : @Singleton 34 : public class DeleteBranches implements RestModifyView<ProjectResource, DeleteBranchesInput> { 35 : private final DeleteRef deleteRef; 36 : 37 : @Inject 38 146 : DeleteBranches(DeleteRef deleteRef) { 39 146 : this.deleteRef = deleteRef; 40 146 : } 41 : 42 : @Override 43 : public Response<?> apply(ProjectResource project, DeleteBranchesInput input) 44 : throws IOException, RestApiException, PermissionBackendException { 45 2 : if (input == null || input.branches == null || input.branches.isEmpty()) { 46 2 : throw new BadRequestException("branches must be specified"); 47 : } 48 : 49 1 : if (input.branches.contains(RefNames.HEAD)) { 50 1 : throw new MethodNotAllowedException("not allowed to delete HEAD"); 51 1 : } else if (input.branches.stream().anyMatch(RefNames::isConfigRef)) { 52 : // Never allow to delete the meta config branch. 53 1 : throw new MethodNotAllowedException("not allowed to delete branch " + RefNames.REFS_CONFIG); 54 : } 55 : 56 1 : deleteRef.deleteMultipleRefs( 57 1 : project.getProjectState(), ImmutableSet.copyOf(input.branches), R_HEADS); 58 1 : return Response.none(); 59 : } 60 : }