use of com.google.gerrit.server.permissions.LabelPermission 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.server.permissions.LabelPermission 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.server.permissions.LabelPermission in project gerrit by GerritCodeReview.
the class LabelsJson method setAllApprovals.
private void setAllApprovals(AccountLoader accountLoader, ChangeData cd, Map<String, LabelWithStatus> labels, boolean detailed) throws PermissionBackendException {
checkState(!cd.change().isMerged(), "should not call setAllApprovals on %s change", ChangeUtil.status(cd.change()));
// 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.accountId());
}
Table<Account.Id, String, PatchSetApproval> current = HashBasedTable.create(allUsers.size(), cd.getLabelTypes().getLabelTypes().size());
for (PatchSetApproval psa : cd.currentApprovals()) {
current.put(psa.accountId(), psa.label(), psa);
}
LabelTypes labelTypes = cd.getLabelTypes();
for (Account.Id accountId : allUsers) {
Map<String, VotingRangeInfo> pvr = null;
PermissionBackend.ForChange perm = null;
if (detailed) {
perm = permissionBackend.absentUser(accountId).change(cd);
pvr = getPermittedVotingRanges(permittedLabels(accountId, cd));
}
for (Map.Entry<String, LabelWithStatus> e : labels.entrySet()) {
Optional<LabelType> lt = labelTypes.byLabel(e.getKey());
if (!lt.isPresent()) {
// author didn't intend for the label to show up in the table.
continue;
}
Integer value;
VotingRangeInfo permittedVotingRange = pvr == null ? null : pvr.getOrDefault(lt.get().getName(), null);
String tag = null;
Instant date = null;
PatchSetApproval psa = current.get(accountId, lt.get().getName());
if (psa != null) {
value = Integer.valueOf(psa.value());
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 != null && perm.test(new LabelPermission(lt.get())) ? 0 : null;
}
tag = psa.tag().orElse(null);
date = psa.granted();
if (psa.postSubmit()) {
logger.atWarning().log("unexpected post-submit approval on open change: %s", 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 != null && perm.test(new LabelPermission(lt.get())) ? 0 : null;
}
addApproval(e.getValue().label(), approvalInfo(accountLoader, accountId, value, permittedVotingRange, tag, date));
}
}
}
use of com.google.gerrit.server.permissions.LabelPermission 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;
}
Aggregations