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.project; 16 : 17 : import static com.google.gerrit.entities.RefNames.isConfigRef; 18 : import static org.eclipse.jgit.lib.Constants.R_HEADS; 19 : 20 : import com.google.gerrit.entities.RefNames; 21 : import com.google.gerrit.extensions.common.Input; 22 : import com.google.gerrit.extensions.restapi.MethodNotAllowedException; 23 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 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.BranchResource; 29 : import com.google.gerrit.server.query.change.InternalChangeQuery; 30 : import com.google.inject.Inject; 31 : import com.google.inject.Provider; 32 : import com.google.inject.Singleton; 33 : import java.io.IOException; 34 : 35 : @Singleton 36 : public class DeleteBranch implements RestModifyView<BranchResource, Input> { 37 : 38 : private final Provider<InternalChangeQuery> queryProvider; 39 : private final DeleteRef deleteRef; 40 : 41 : @Inject 42 138 : DeleteBranch(Provider<InternalChangeQuery> queryProvider, DeleteRef deleteRef) { 43 138 : this.queryProvider = queryProvider; 44 138 : this.deleteRef = deleteRef; 45 138 : } 46 : 47 : @Override 48 : public Response<?> apply(BranchResource rsrc, Input input) 49 : throws RestApiException, IOException, PermissionBackendException { 50 4 : if (RefNames.HEAD.equals(rsrc.getBranchKey().branch())) { 51 1 : throw new MethodNotAllowedException("not allowed to delete HEAD"); 52 4 : } else if (isConfigRef(rsrc.getBranchKey().branch())) { 53 : // Never allow to delete the meta config branch. 54 1 : throw new MethodNotAllowedException( 55 1 : "not allowed to delete branch " + rsrc.getBranchKey().branch()); 56 : } 57 : 58 4 : if (!queryProvider.get().setLimit(1).byBranchOpen(rsrc.getBranchKey()).isEmpty()) { 59 0 : throw new ResourceConflictException("branch " + rsrc.getBranchKey() + " has open changes"); 60 : } 61 : 62 4 : deleteRef.deleteSingleRef(rsrc.getProjectState(), rsrc.getRef(), R_HEADS); 63 4 : return Response.none(); 64 : } 65 : }