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.project; 16 : 17 : import com.google.common.collect.ImmutableListMultimap; 18 : import com.google.gerrit.entities.Project; 19 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 20 : import com.google.gerrit.server.IdentifiedUser; 21 : import com.google.gerrit.server.git.validators.RefOperationValidators; 22 : import com.google.gerrit.server.validators.ValidationException; 23 : import com.google.inject.Inject; 24 : import com.google.inject.assistedinject.Assisted; 25 : import org.eclipse.jgit.lib.RefUpdate; 26 : import org.eclipse.jgit.transport.ReceiveCommand; 27 : 28 : public class RefValidationHelper { 29 : public interface Factory { 30 : RefValidationHelper create(ReceiveCommand.Type operationType); 31 : } 32 : 33 : private final RefOperationValidators.Factory refValidatorsFactory; 34 : private final ReceiveCommand.Type operationType; 35 : 36 : @Inject 37 : RefValidationHelper( 38 : RefOperationValidators.Factory refValidatorsFactory, 39 146 : @Assisted ReceiveCommand.Type operationType) { 40 146 : this.refValidatorsFactory = refValidatorsFactory; 41 146 : this.operationType = operationType; 42 146 : } 43 : 44 : public void validateRefOperation( 45 : String projectName, 46 : IdentifiedUser user, 47 : RefUpdate update, 48 : ImmutableListMultimap<String, String> pushOptions) 49 : throws ResourceConflictException { 50 31 : RefOperationValidators refValidators = 51 31 : refValidatorsFactory.create( 52 31 : Project.builder(Project.nameKey(projectName)).build(), 53 : user, 54 31 : RefOperationValidators.getCommand(update, operationType), 55 : pushOptions); 56 : try { 57 31 : refValidators.validateForRefOperation(); 58 3 : } catch (ValidationException e) { 59 3 : throw new ResourceConflictException(e.getMessage()); 60 31 : } 61 31 : } 62 : }