use of com.google.gerrit.common.data.LabelTypes in project gerrit by GerritCodeReview.
the class ReviewerJson method format.
public ReviewerInfo format(ReviewerInfo out, PermissionBackend.ForChange perm, ChangeData cd, Iterable<PatchSetApproval> approvals) throws OrmException, PermissionBackendException {
LabelTypes labelTypes = cd.getLabelTypes();
// Don't use Maps.newTreeMap(Comparator) due to OpenJDK bug 100167.
out.approvals = new TreeMap<>(labelTypes.nameComparator());
for (PatchSetApproval ca : approvals) {
for (PermissionRange pr : cd.changeControl().getLabelRanges()) {
if (!pr.isEmpty()) {
LabelType at = labelTypes.byLabel(ca.getLabelId());
if (at != null) {
out.approvals.put(at.getName(), formatValue(ca.getValue()));
}
}
}
}
// 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) {
for (SubmitRecord rec : new SubmitRuleEvaluator(cd).setFastEvalLabels(true).setAllowDraft(true).evaluate()) {
if (rec.labels == null) {
continue;
}
for (SubmitRecord.Label label : rec.labels) {
String name = label.label;
LabelType type = labelTypes.byLabel(name);
if (!out.approvals.containsKey(name) && type != null && perm.test(new LabelPermission(type))) {
out.approvals.put(name, formatValue((short) 0));
}
}
}
}
if (out.approvals.isEmpty()) {
out.approvals = null;
}
return out;
}
use of com.google.gerrit.common.data.LabelTypes in project gerrit by GerritCodeReview.
the class SchemaCreatorTest method getLabelTypes.
private LabelTypes getLabelTypes() throws Exception {
db.create();
ProjectConfig c = new ProjectConfig(allProjects);
try (Repository repo = repoManager.openRepository(allProjects)) {
c.load(repo);
return new LabelTypes(ImmutableList.copyOf(c.getLabelSections().values()));
}
}
use of com.google.gerrit.common.data.LabelTypes in project gerrit by GerritCodeReview.
the class LabelNormalizer method normalize.
/**
* @param ctl change control containing the given approvals.
* @param approvals list of approvals.
* @return copies of approvals normalized to the defined ranges for the label type and permissions
* for the user. Approvals for unknown labels are not included in the output, nor are
* approvals where the user has no permissions for that label.
*/
public Result normalize(ChangeControl ctl, 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 = ctl.getLabelTypes();
for (PatchSetApproval psa : approvals) {
Change.Id changeId = psa.getKey().getParentKey().getParentKey();
checkArgument(changeId.equals(ctl.getId()), "Approval %s does not match change %s", psa.getKey(), ctl.getChange().getKey());
if (psa.isLegacySubmit()) {
unchanged.add(psa);
continue;
}
LabelType label = labelTypes.byLabel(psa.getLabelId());
if (label == null) {
deleted.add(psa);
continue;
}
PatchSetApproval copy = copy(psa);
applyTypeFloor(label, copy);
if (!applyRightFloor(ctl, label, copy)) {
deleted.add(psa);
} else if (copy.getValue() != psa.getValue()) {
updated.add(copy);
} else {
unchanged.add(psa);
}
}
return Result.create(unchanged, updated, deleted);
}
use of com.google.gerrit.common.data.LabelTypes in project gerrit by GerritCodeReview.
the class ChangeJson method setAllApprovals.
private void setAllApprovals(PermissionBackend.ForChange basePerm, ChangeData cd, Map<String, LabelWithStatus> labels) throws OrmException, PermissionBackendException {
Change.Status status = cd.change().getStatus();
checkState(status.isOpen(), "should not call setAllApprovals on %s change", status);
// Include a user in the output for this label if either:
// - They are an explicit reviewer.
// - They ever voted on this change.
Set<Account.Id> allUsers = new HashSet<>();
allUsers.addAll(cd.reviewers().byState(ReviewerStateInternal.REVIEWER));
for (PatchSetApproval psa : cd.approvals().values()) {
allUsers.add(psa.getAccountId());
}
Table<Account.Id, String, PatchSetApproval> current = HashBasedTable.create(allUsers.size(), cd.getLabelTypes().getLabelTypes().size());
for (PatchSetApproval psa : cd.currentApprovals()) {
current.put(psa.getAccountId(), psa.getLabel(), psa);
}
LabelTypes labelTypes = cd.getLabelTypes();
for (Account.Id accountId : allUsers) {
PermissionBackend.ForChange perm = basePerm.user(userFactory.create(accountId));
Map<String, VotingRangeInfo> pvr = getPermittedVotingRanges(permittedLabels(perm, cd));
for (Map.Entry<String, LabelWithStatus> e : labels.entrySet()) {
LabelType lt = labelTypes.byLabel(e.getKey());
if (lt == null) {
// author didn't intend for the label to show up in the table.
continue;
}
Integer value;
VotingRangeInfo permittedVotingRange = pvr.getOrDefault(lt.getName(), null);
String tag = null;
Timestamp date = null;
PatchSetApproval psa = current.get(accountId, lt.getName());
if (psa != null) {
value = Integer.valueOf(psa.getValue());
if (value == 0) {
// This may be a dummy approval that was inserted when the reviewer
// was added. Explicitly check whether the user can vote on this
// label.
value = perm.test(new LabelPermission(lt)) ? 0 : null;
}
tag = psa.getTag();
date = psa.getGranted();
if (psa.isPostSubmit()) {
log.warn("unexpected post-submit approval on open change: {}", psa);
}
} else {
// Either the user cannot vote on this label, or they were added as a
// reviewer but have not responded yet. Explicitly check whether the
// user can vote on this label.
value = perm.test(new LabelPermission(lt)) ? 0 : null;
}
addApproval(e.getValue().label(), approvalInfo(accountId, value, permittedVotingRange, tag, date));
}
}
}
use of com.google.gerrit.common.data.LabelTypes in project gerrit by GerritCodeReview.
the class ChangeControl method getLabelTypes.
/** All available label types for this change. */
public LabelTypes getLabelTypes() {
String destBranch = getChange().getDest().get();
List<LabelType> all = getProjectControl().getLabelTypes().getLabelTypes();
List<LabelType> r = Lists.newArrayListWithCapacity(all.size());
for (LabelType l : all) {
List<String> refs = l.getRefPatterns();
if (refs == null) {
r.add(l);
} else {
for (String refPattern : refs) {
if (RefConfigSection.isValid(refPattern) && match(destBranch, refPattern)) {
r.add(l);
break;
}
}
}
}
return new LabelTypes(r);
}
Aggregations