public LabelType createLabel()

in java/com/google/gerrit/server/restapi/project/CreateLabel.java [126:243]


  public LabelType createLabel(ProjectConfig config, String label, LabelDefinitionInput input)
      throws BadRequestException, ResourceConflictException {
    if (config.getLabelSections().containsKey(label)) {
      throw new ResourceConflictException(String.format("label %s already exists", label));
    }

    for (String labelName : config.getLabelSections().keySet()) {
      if (labelName.equalsIgnoreCase(label)) {
        throw new ResourceConflictException(
            String.format("label %s conflicts with existing label %s", label, labelName));
      }
    }

    if (input.values == null || input.values.isEmpty()) {
      throw new BadRequestException("values are required");
    }

    try {
      LabelType.checkName(label);
    } catch (IllegalArgumentException e) {
      throw new BadRequestException("invalid name: " + label, e);
    }

    List<LabelValue> values = LabelDefinitionInputParser.parseValues(input.values);
    LabelType.Builder labelType = LabelType.builder(LabelType.checkName(label), values);

    if (input.description != null) {
      String description = Strings.emptyToNull(input.description.trim());
      labelType.setDescription(Optional.ofNullable(description));
    }

    if (input.function != null && !input.function.trim().isEmpty()) {
      labelType.setFunction(LabelDefinitionInputParser.parseFunction(input.function));
    } else {
      labelType.setFunction(LabelFunction.MAX_WITH_BLOCK);
    }

    if (input.defaultValue != null) {
      labelType.setDefaultValue(
          LabelDefinitionInputParser.parseDefaultValue(labelType, input.defaultValue));
    }

    if (input.branches != null) {
      labelType.setRefPatterns(LabelDefinitionInputParser.parseBranches(input.branches));
    }

    if (input.canOverride != null) {
      labelType.setCanOverride(input.canOverride);
    }

    if (input.copyAnyScore != null) {
      labelType.setCopyAnyScore(input.copyAnyScore);
    }

    if (input.copyCondition != null) {
      try {
        approvalQueryBuilder.parse(input.copyCondition);
      } catch (QueryParseException e) {
        throw new BadRequestException(
            "unable to parse copy condition. got: " + input.copyCondition + ". " + e.getMessage(),
            e);
      }
      if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
        throw new BadRequestException("can't set and unset copyCondition in the same request");
      }
      labelType.setCopyCondition(Strings.emptyToNull(input.copyCondition));
    }
    if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
      labelType.setCopyCondition(null);
    }

    if (input.copyMinScore != null) {
      labelType.setCopyMinScore(input.copyMinScore);
    }

    if (input.copyMaxScore != null) {
      labelType.setCopyMaxScore(input.copyMaxScore);
    }

    if (input.copyAllScoresIfListOfFilesDidNotChange != null) {
      labelType.setCopyAllScoresIfListOfFilesDidNotChange(
          input.copyAllScoresIfListOfFilesDidNotChange);
    }

    if (input.copyAllScoresIfNoChange != null) {
      labelType.setCopyAllScoresIfNoChange(input.copyAllScoresIfNoChange);
    }

    if (input.copyAllScoresIfNoCodeChange != null) {
      labelType.setCopyAllScoresIfNoCodeChange(input.copyAllScoresIfNoCodeChange);
    }

    if (input.copyAllScoresOnTrivialRebase != null) {
      labelType.setCopyAllScoresOnTrivialRebase(input.copyAllScoresOnTrivialRebase);
    }

    if (input.copyAllScoresOnMergeFirstParentUpdate != null) {
      labelType.setCopyAllScoresOnMergeFirstParentUpdate(
          input.copyAllScoresOnMergeFirstParentUpdate);
    }

    if (input.copyValues != null) {
      labelType.setCopyValues(input.copyValues);
    }

    if (input.allowPostSubmit != null) {
      labelType.setAllowPostSubmit(input.allowPostSubmit);
    }

    if (input.ignoreSelfApproval != null) {
      labelType.setIgnoreSelfApproval(input.ignoreSelfApproval);
    }

    LabelType lt = labelType.build();
    config.upsertLabelType(lt);

    return lt;
  }