Search in sources :

Example 11 with LabelValue

use of com.google.gerrit.entities.LabelValue in project gerrit by GerritCodeReview.

the class ProjectJson method format.

public ProjectInfo format(ProjectState projectState) {
    ProjectInfo info = format(projectState.getProject());
    info.labels = new HashMap<>();
    for (LabelType t : projectState.getLabelTypes().getLabelTypes()) {
        LabelTypeInfo labelInfo = new LabelTypeInfo();
        labelInfo.values = t.getValues().stream().collect(toMap(LabelValue::formatValue, LabelValue::getText, (v1, v2) -> {
            logger.atSevere().log("Duplicate values for project: %s, label: %s found: '%s':'%s'", projectState.getName(), t.getName(), v1, v2);
            return v1;
        }));
        labelInfo.defaultValue = t.getDefaultValue();
        info.labels.put(t.getName(), labelInfo);
    }
    return info;
}
Also used : LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) ProjectInfo(com.google.gerrit.extensions.common.ProjectInfo) LabelTypeInfo(com.google.gerrit.extensions.common.LabelTypeInfo)

Example 12 with LabelValue

use of com.google.gerrit.entities.LabelValue in project gerrit by GerritCodeReview.

the class MergedSender method format.

private String format(String type, Table<Account.Id, String, PatchSetApproval> approvals) {
    StringBuilder txt = new StringBuilder();
    if (approvals.isEmpty()) {
        return "";
    }
    txt.append(type).append(":\n");
    for (Account.Id id : approvals.rowKeySet()) {
        txt.append("  ");
        txt.append(getNameFor(id));
        txt.append(": ");
        boolean first = true;
        for (LabelType lt : labelTypes.getLabelTypes()) {
            PatchSetApproval ca = approvals.get(id, lt.getName());
            if (ca == null) {
                continue;
            }
            if (first) {
                first = false;
            } else {
                txt.append("; ");
            }
            LabelValue v = lt.getValue(ca);
            if (v != null) {
                txt.append(v.getText());
            } else {
                txt.append(lt.getName());
                txt.append('=');
                txt.append(LabelValue.formatValue(ca.value()));
            }
        }
        txt.append('\n');
    }
    txt.append('\n');
    return txt.toString();
}
Also used : Account(com.google.gerrit.entities.Account) LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 13 with LabelValue

use of com.google.gerrit.entities.LabelValue in project gerrit by GerritCodeReview.

the class CreateLabel method createLabel.

/**
 * Creates a new label.
 *
 * @param config the project config
 * @param label the name of the new label
 * @param input the input that describes the new label
 * @return the created label type
 * @throws BadRequestException if there was invalid data in the input
 * @throws ResourceConflictException if the label cannot be created due to a conflict
 */
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;
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) IdString(com.google.gerrit.extensions.restapi.IdString) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 14 with LabelValue

use of com.google.gerrit.entities.LabelValue in project gerrit by GerritCodeReview.

the class LabelValueSerializerTest method roundTrip.

@Test
public void roundTrip() {
    LabelValue autoValue = LabelValue.create((short) 123, "Approved!");
    assertThat(deserialize(serialize(autoValue))).isEqualTo(autoValue);
}
Also used : LabelValue(com.google.gerrit.entities.LabelValue) Test(org.junit.Test)

Aggregations

LabelValue (com.google.gerrit.entities.LabelValue)14 LabelType (com.google.gerrit.entities.LabelType)8 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)3 LabelFunction (com.google.gerrit.entities.LabelFunction)2 PatchSetApproval (com.google.gerrit.entities.PatchSetApproval)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 Map (java.util.Map)2 Test (org.junit.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Truth8.assertThat (com.google.common.truth.Truth8.assertThat)1 Account (com.google.gerrit.entities.Account)1 GroupReference (com.google.gerrit.entities.GroupReference)1 LabelTypes (com.google.gerrit.entities.LabelTypes)1 StorageException (com.google.gerrit.exceptions.StorageException)1 LabelDefinitionInfo (com.google.gerrit.extensions.common.LabelDefinitionInfo)1 LabelTypeInfo (com.google.gerrit.extensions.common.LabelTypeInfo)1 ProjectInfo (com.google.gerrit.extensions.common.ProjectInfo)1 IdString (com.google.gerrit.extensions.restapi.IdString)1