use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class ReviewerJson method format.
public ReviewerInfo format(ReviewerInfo out, Account.Id reviewerAccountId, ChangeData cd, Iterable<PatchSetApproval> approvals) throws PermissionBackendException {
LabelTypes labelTypes = cd.getLabelTypes();
out.approvals = new TreeMap<>(labelTypes.nameComparator());
for (PatchSetApproval ca : approvals) {
Optional<LabelType> at = labelTypes.byLabel(ca.labelId());
at.ifPresent(lt -> out.approvals.put(lt.getName(), formatValue(ca.value())));
}
// Add dummy approvals for all permitted labels for the user even if they
// do not exist in the DB.
PatchSet ps = cd.currentPatchSet();
if (ps != null) {
PermissionBackend.ForChange perm = permissionBackend.absentUser(reviewerAccountId).change(cd);
for (SubmitRecord rec : cd.submitRecords(SubmitRuleOptions.defaults())) {
if (rec.labels == null) {
continue;
}
for (SubmitRecord.Label label : rec.labels) {
String name = label.label;
Optional<LabelType> type = labelTypes.byLabel(name);
if (out.approvals.containsKey(name) || !type.isPresent()) {
continue;
}
if (perm.test(new LabelPermission(type.get()))) {
out.approvals.put(name, formatValue((short) 0));
}
}
}
}
if (out.approvals.isEmpty()) {
out.approvals = null;
}
return out;
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class LabelNormalizer method normalize.
/**
* Returns copies of approvals normalized to the defined ranges for the label type. Approvals for
* unknown labels are not included in the output
*
* @param notes change notes containing the given approvals.
* @param approvals list of approvals.
*/
public Result normalize(ChangeNotes notes, Collection<PatchSetApproval> approvals) {
List<PatchSetApproval> unchanged = Lists.newArrayListWithCapacity(approvals.size());
List<PatchSetApproval> updated = Lists.newArrayListWithCapacity(approvals.size());
List<PatchSetApproval> deleted = Lists.newArrayListWithCapacity(approvals.size());
LabelTypes labelTypes = projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName())).getLabelTypes(notes);
for (PatchSetApproval psa : approvals) {
Change.Id changeId = psa.key().patchSetId().changeId();
checkArgument(changeId.equals(notes.getChangeId()), "Approval %s does not match change %s", psa.key(), notes.getChange().getKey());
if (psa.isLegacySubmit()) {
unchanged.add(psa);
continue;
}
Optional<LabelType> label = labelTypes.byLabel(psa.labelId());
if (!label.isPresent()) {
deleted.add(psa);
continue;
}
PatchSetApproval copy = applyTypeFloor(label.get(), psa);
if (copy.value() != psa.value()) {
updated.add(copy);
} else {
unchanged.add(psa);
}
}
return Result.create(unchanged, updated, deleted);
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class ChangeField method getLabels.
private static Iterable<String> getLabels(ChangeData cd) {
Set<String> allApprovals = new HashSet<>();
Set<String> distinctApprovals = new HashSet<>();
Table<String, Short, Integer> voteCounts = HashBasedTable.create();
for (PatchSetApproval a : cd.currentApprovals()) {
if (a.value() != 0 && !a.isLegacySubmit()) {
increment(voteCounts, a.label(), a.value());
Optional<LabelType> labelType = cd.getLabelTypes().byLabel(a.labelId());
allApprovals.add(formatLabel(a.label(), a.value(), a.accountId()));
allApprovals.addAll(getMagicLabelFormats(a.label(), a.value(), labelType, a.accountId()));
allApprovals.addAll(getLabelOwnerFormats(a, cd, labelType));
allApprovals.addAll(getLabelNonUploaderFormats(a, cd, labelType));
distinctApprovals.add(formatLabel(a.label(), a.value()));
distinctApprovals.addAll(getMagicLabelFormats(a.label(), a.value(), labelType, /* accountId= */
null));
}
}
allApprovals.addAll(distinctApprovals);
allApprovals.addAll(getCountLabelFormats(voteCounts, cd));
return allApprovals;
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class ProjectConfig method updateLabelType.
/**
* Allows a mutation of an existing {@link LabelType}.
*/
public void updateLabelType(String name, Consumer<LabelType.Builder> update) {
LabelType labelType = labelSections.get(name);
checkState(labelType != null, "labelType must not be null");
LabelType.Builder builder = labelSections.get(name).toBuilder();
update.accept(builder);
upsertLabelType(builder.build());
}
use of com.google.gerrit.entities.LabelType 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;
}
Aggregations