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.collect.ImmutableList; 18 : import com.google.common.primitives.Shorts; 19 : import com.google.gerrit.entities.LabelFunction; 20 : import com.google.gerrit.entities.LabelType; 21 : import com.google.gerrit.entities.LabelValue; 22 : import com.google.gerrit.entities.PermissionRule; 23 : import com.google.gerrit.entities.RefNames; 24 : import com.google.gerrit.exceptions.InvalidNameException; 25 : import com.google.gerrit.extensions.restapi.BadRequestException; 26 : import com.google.gerrit.server.project.RefPattern; 27 : import java.util.ArrayList; 28 : import java.util.HashSet; 29 : import java.util.List; 30 : import java.util.Map; 31 : import java.util.Optional; 32 : import java.util.Set; 33 : 34 : public class LabelDefinitionInputParser { 35 : public static LabelFunction parseFunction(String functionString) throws BadRequestException { 36 5 : Optional<LabelFunction> function = LabelFunction.parse(functionString.trim()); 37 5 : return function.orElseThrow( 38 2 : () -> new BadRequestException("unknown function: " + functionString)); 39 : } 40 : 41 : public static List<LabelValue> parseValues(Map<String, String> values) 42 : throws BadRequestException { 43 5 : List<LabelValue> valueList = new ArrayList<>(); 44 5 : Set<Short> allValues = new HashSet<>(); 45 5 : for (Map.Entry<String, String> e : values.entrySet()) { 46 : short value; 47 : try { 48 5 : value = Shorts.checkedCast(PermissionRule.parseInt(e.getKey().trim())); 49 2 : } catch (NumberFormatException ex) { 50 2 : throw new BadRequestException("invalid value: " + e.getKey(), ex); 51 5 : } 52 5 : if (!allValues.add(value)) { 53 2 : throw new BadRequestException("duplicate value: " + value); 54 : } 55 5 : String valueDescription = e.getValue().trim(); 56 5 : if (valueDescription.isEmpty()) { 57 2 : throw new BadRequestException("description for value '" + e.getKey() + "' cannot be empty"); 58 : } 59 5 : valueList.add(LabelValue.create(value, valueDescription)); 60 5 : } 61 5 : return valueList; 62 : } 63 : 64 : public static short parseDefaultValue(LabelType.Builder labelType, short defaultValue) 65 : throws BadRequestException { 66 2 : if (!labelType.getValues().stream().anyMatch(v -> v.getValue() == defaultValue)) { 67 2 : throw new BadRequestException("invalid default value: " + defaultValue); 68 : } 69 2 : return defaultValue; 70 : } 71 : 72 : public static ImmutableList<String> parseBranches(List<String> branches) 73 : throws BadRequestException { 74 2 : ImmutableList.Builder<String> validBranches = ImmutableList.builder(); 75 2 : for (String branch : branches) { 76 2 : String newBranch = branch.trim(); 77 2 : if (newBranch.isEmpty()) { 78 2 : continue; 79 : } 80 2 : if (!RefPattern.isRE(newBranch) && !newBranch.startsWith(RefNames.REFS)) { 81 2 : newBranch = RefNames.REFS_HEADS + newBranch; 82 : } 83 : try { 84 2 : RefPattern.validate(newBranch); 85 2 : } catch (InvalidNameException e) { 86 2 : throw new BadRequestException("invalid branch: " + branch, e); 87 2 : } 88 2 : validBranches.add(newBranch); 89 2 : } 90 2 : return validBranches.build(); 91 : } 92 : 93 : private LabelDefinitionInputParser() {} 94 : }