Search in sources :

Example 1 with LabelValue

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

the class LabelDefinitionInputParser method parseValues.

public static List<LabelValue> parseValues(Map<String, String> values) throws BadRequestException {
    List<LabelValue> valueList = new ArrayList<>();
    Set<Short> allValues = new HashSet<>();
    for (Map.Entry<String, String> e : values.entrySet()) {
        short value;
        try {
            value = Shorts.checkedCast(PermissionRule.parseInt(e.getKey().trim()));
        } catch (NumberFormatException ex) {
            throw new BadRequestException("invalid value: " + e.getKey(), ex);
        }
        if (!allValues.add(value)) {
            throw new BadRequestException("duplicate value: " + value);
        }
        String valueDescription = e.getValue().trim();
        if (valueDescription.isEmpty()) {
            throw new BadRequestException("description for value '" + e.getKey() + "' cannot be empty");
        }
        valueList.add(LabelValue.create(value, valueDescription));
    }
    return valueList;
}
Also used : LabelValue(com.google.gerrit.entities.LabelValue) ArrayList(java.util.ArrayList) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with LabelValue

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

the class SchemaCreatorImplTest method assertValueRange.

private void assertValueRange(LabelType label, Integer... range) {
    List<Integer> rangeList = Arrays.asList(range);
    assertThat(rangeList).isNotEmpty();
    assertThat(rangeList).isInStrictOrder();
    assertThat(label.getValues().stream().map(v -> (int) v.getValue())).containsExactlyElementsIn(rangeList).inOrder();
    assertThat(label.getMax().getValue()).isEqualTo(Collections.max(rangeList));
    assertThat(label.getMin().getValue()).isEqualTo(Collections.min(rangeList));
    for (LabelValue v : label.getValues()) {
        assertThat(v.getText()).isNotNull();
        assertThat(v.getText()).isNotEmpty();
    }
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) GroupNameNotes(com.google.gerrit.server.group.db.GroupNameNotes) InMemoryModule(com.google.gerrit.testing.InMemoryModule) Arrays(java.util.Arrays) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Inject(com.google.inject.Inject) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) ArrayList(java.util.ArrayList) GroupReference(com.google.gerrit.entities.GroupReference) LabelTypes(com.google.gerrit.entities.LabelTypes) List(java.util.List) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) ImmutableList(com.google.common.collect.ImmutableList) LabelValue(com.google.gerrit.entities.LabelValue) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) LabelType(com.google.gerrit.entities.LabelType) LabelFunction(com.google.gerrit.entities.LabelFunction) Truth8.assertThat(com.google.common.truth.Truth8.assertThat) ServiceUserClassifier(com.google.gerrit.server.account.ServiceUserClassifier) Collections(java.util.Collections) Repository(org.eclipse.jgit.lib.Repository) Before(org.junit.Before) LabelValue(com.google.gerrit.entities.LabelValue)

Example 3 with LabelValue

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

the class ReviewCommand method parseCommandLine.

@Override
protected void parseCommandLine(DynamicOptions pluginOptions) throws UnloggedFailure {
    optionMap = new LinkedHashMap<>();
    customLabels = new HashMap<>();
    ProjectState allProjectsState;
    try {
        allProjectsState = projectCache.getAllProjects();
    } catch (Exception e) {
        throw die("missing " + allProjects.get(), e);
    }
    for (LabelType type : allProjectsState.getLabelTypes().getLabelTypes()) {
        StringBuilder usage = new StringBuilder("score for ").append(type.getName()).append("\n");
        for (LabelValue v : type.getValues()) {
            usage.append(v.format()).append("\n");
        }
        optionMap.put(newApproveOption(type, usage.toString()), new LabelSetter(type));
    }
    super.parseCommandLine(pluginOptions);
}
Also used : LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) ProjectState(com.google.gerrit.server.project.ProjectState) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) CmdLineException(org.kohsuke.args4j.CmdLineException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) JsonSyntaxException(com.google.gson.JsonSyntaxException) StorageException(com.google.gerrit.exceptions.StorageException) IOException(java.io.IOException)

Example 4 with LabelValue

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

the class ProjectConfig method saveLabelSections.

private void saveLabelSections(Config rc) {
    List<String> existing = new ArrayList<>(rc.getSubsections(LABEL));
    if (!new ArrayList<>(labelSections.keySet()).equals(existing)) {
        // Order of sections changed, remove and rewrite them all.
        unsetSection(rc, LABEL);
    }
    Set<String> toUnset = new HashSet<>(existing);
    for (Map.Entry<String, LabelType> e : labelSections.entrySet()) {
        String name = e.getKey();
        LabelType label = e.getValue();
        toUnset.remove(name);
        if (label.getDescription().isPresent() && !label.getDescription().get().isEmpty()) {
            rc.setString(LABEL, name, KEY_LABEL_DESCRIPTION, label.getDescription().get());
        } else {
            rc.unset(LABEL, name, KEY_LABEL_DESCRIPTION);
        }
        rc.setString(LABEL, name, KEY_FUNCTION, label.getFunction().getFunctionName());
        rc.setInt(LABEL, name, KEY_DEFAULT_VALUE, label.getDefaultValue());
        setBooleanConfigKey(rc, LABEL, name, KEY_ALLOW_POST_SUBMIT, label.isAllowPostSubmit(), LabelType.DEF_ALLOW_POST_SUBMIT);
        setBooleanConfigKey(rc, LABEL, name, KEY_IGNORE_SELF_APPROVAL, label.isIgnoreSelfApproval(), LabelType.DEF_IGNORE_SELF_APPROVAL);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ANY_SCORE, label.isCopyAnyScore(), LabelType.DEF_COPY_ANY_SCORE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_MIN_SCORE, label.isCopyMinScore(), LabelType.DEF_COPY_MIN_SCORE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_MAX_SCORE, label.isCopyMaxScore(), LabelType.DEF_COPY_MAX_SCORE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_ON_TRIVIAL_REBASE, label.isCopyAllScoresOnTrivialRebase(), LabelType.DEF_COPY_ALL_SCORES_ON_TRIVIAL_REBASE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_IF_NO_CODE_CHANGE, label.isCopyAllScoresIfNoCodeChange(), LabelType.DEF_COPY_ALL_SCORES_IF_NO_CODE_CHANGE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_IF_NO_CHANGE, label.isCopyAllScoresIfNoChange(), LabelType.DEF_COPY_ALL_SCORES_IF_NO_CHANGE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_IF_LIST_OF_FILES_DID_NOT_CHANGE, label.isCopyAllScoresIfListOfFilesDidNotChange(), LabelType.DEF_COPY_ALL_SCORES_IF_LIST_OF_FILES_DID_NOT_CHANGE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_ON_MERGE_FIRST_PARENT_UPDATE, label.isCopyAllScoresOnMergeFirstParentUpdate(), LabelType.DEF_COPY_ALL_SCORES_ON_MERGE_FIRST_PARENT_UPDATE);
        rc.setStringList(LABEL, name, KEY_COPY_VALUE, label.getCopyValues().stream().map(LabelValue::formatValue).collect(toList()));
        setBooleanConfigKey(rc, LABEL, name, KEY_CAN_OVERRIDE, label.isCanOverride(), LabelType.DEF_CAN_OVERRIDE);
        List<String> values = new ArrayList<>(label.getValues().size());
        for (LabelValue value : label.getValues()) {
            values.add(value.format().trim());
        }
        rc.setStringList(LABEL, name, KEY_VALUE, values);
        if (label.getCopyCondition().isPresent()) {
            rc.setString(LABEL, name, KEY_COPY_CONDITION, label.getCopyCondition().get());
        } else {
            rc.unset(LABEL, name, KEY_COPY_CONDITION);
        }
        List<String> refPatterns = label.getRefPatterns();
        if (refPatterns != null && !refPatterns.isEmpty()) {
            rc.setStringList(LABEL, name, KEY_BRANCH, refPatterns);
        } else {
            rc.unset(LABEL, name, KEY_BRANCH);
        }
    }
    for (String name : toUnset) {
        rc.unsetSection(LABEL, name);
    }
}
Also used : LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet)

Example 5 with LabelValue

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

the class LabelsJson method permittedLabels.

/**
 * Returns A map of all label names and the values that the provided user has permission to vote
 * on.
 *
 * @param filterApprovalsBy a Gerrit user ID.
 * @param cd {@link ChangeData} corresponding to a specific gerrit change.
 * @return A Map where the key contain a label name, and the value is a list of the permissible
 *     vote values that the user can vote on.
 */
Map<String, Collection<String>> permittedLabels(Account.Id filterApprovalsBy, ChangeData cd) throws PermissionBackendException {
    SetMultimap<String, String> permitted = LinkedHashMultimap.create();
    boolean isMerged = cd.change().isMerged();
    Map<String, Short> currentUserVotes = currentLabels(filterApprovalsBy, cd);
    for (LabelType labelType : cd.getLabelTypes().getLabelTypes()) {
        if (isMerged && !labelType.isAllowPostSubmit()) {
            continue;
        }
        Set<LabelPermission.WithValue> can = permissionBackend.absentUser(filterApprovalsBy).change(cd).test(labelType);
        for (LabelValue v : labelType.getValues()) {
            boolean ok = can.contains(new LabelPermission.WithValue(labelType, v));
            if (isMerged) {
                // Votes cannot be decreased if the change is merged. Only accept the label value if it's
                // greater or equal than the user's latest vote.
                short prev = currentUserVotes.getOrDefault(labelType.getName(), (short) 0);
                ok &= v.getValue() >= prev;
            }
            if (ok) {
                permitted.put(labelType.getName(), v.formatValue());
            }
        }
    }
    clearOnlyZerosEntries(permitted);
    return permitted.asMap();
}
Also used : LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) LabelPermission(com.google.gerrit.server.permissions.LabelPermission)

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