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;
}
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();
}
}
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);
}
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);
}
}
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();
}
Aggregations