use of com.google.gerrit.entities.LabelTypes in project gerrit by GerritCodeReview.
the class OutputStreamQuery method buildChangeAttribute.
private ChangeAttribute buildChangeAttribute(ChangeData d, Map<Project.NameKey, Repository> repos, Map<Project.NameKey, RevWalk> revWalks) throws IOException {
LabelTypes labelTypes = d.getLabelTypes();
ChangeAttribute c = eventFactory.asChangeAttribute(d.change(), d.notes());
eventFactory.extend(c, d.change());
if (!trackingFooters.isEmpty()) {
eventFactory.addTrackingIds(c, d.trackingFooters());
}
if (includeAllReviewers) {
eventFactory.addAllReviewers(c, d.notes());
}
if (includeSubmitRecords) {
SubmitRuleOptions options = SubmitRuleOptions.builder().recomputeOnClosedChanges(true).build();
eventFactory.addSubmitRecords(c, submitRuleEvaluatorFactory.create(options).evaluate(d));
}
if (includeCommitMessage) {
eventFactory.addCommitMessage(c, d.commitMessage());
}
RevWalk rw = null;
if (includePatchSets || includeCurrentPatchSet || includeDependencies) {
Project.NameKey p = d.change().getProject();
rw = revWalks.get(p);
// Cache and reuse repos and revwalks.
if (rw == null) {
Repository repo = repoManager.openRepository(p);
checkState(repos.put(p, repo) == null);
rw = new RevWalk(repo);
revWalks.put(p, rw);
}
}
if (includePatchSets) {
eventFactory.addPatchSets(rw, c, d.patchSets(), includeApprovals ? d.approvals().asMap() : null, includeFiles, d.change(), labelTypes);
}
if (includeCurrentPatchSet) {
PatchSet current = d.currentPatchSet();
if (current != null) {
c.currentPatchSet = eventFactory.asPatchSetAttribute(rw, d.change(), current);
eventFactory.addApprovals(c.currentPatchSet, d.currentApprovals(), labelTypes);
if (includeFiles) {
eventFactory.addPatchSetFileNames(c.currentPatchSet, d.change(), d.currentPatchSet());
}
if (includeComments) {
eventFactory.addPatchSetComments(c.currentPatchSet, d.publishedComments());
}
}
}
if (includeComments) {
eventFactory.addComments(c, d.messages());
if (includePatchSets) {
eventFactory.addPatchSets(rw, c, d.patchSets(), includeApprovals ? d.approvals().asMap() : null, includeFiles, d.change(), labelTypes);
for (PatchSetAttribute attribute : c.patchSets) {
eventFactory.addPatchSetComments(attribute, d.publishedComments());
}
}
}
if (includeDependencies) {
eventFactory.addDependencies(rw, c, d.change(), d.currentPatchSet());
}
List<PluginDefinedInfo> pluginInfos = pluginInfosByChange.get(d.getId());
if (!pluginInfos.isEmpty()) {
c.plugins = pluginInfos;
}
return c;
}
use of com.google.gerrit.entities.LabelTypes in project gerrit by GerritCodeReview.
the class LabelsJson method labelsFor.
/**
* Returns all {@link LabelInfo}s for a single change. Uses the provided {@link AccountLoader} to
* lazily populate accounts. Callers have to call {@link AccountLoader#fill()} afterwards to
* populate all accounts in the returned {@link LabelInfo}s.
*/
Map<String, LabelInfo> labelsFor(AccountLoader accountLoader, ChangeData cd, boolean standard, boolean detailed) throws PermissionBackendException {
if (!standard && !detailed) {
return null;
}
LabelTypes labelTypes = cd.getLabelTypes();
Map<String, LabelWithStatus> withStatus = cd.change().isMerged() ? labelsForSubmittedChange(accountLoader, cd, labelTypes, standard, detailed) : labelsForUnsubmittedChange(accountLoader, cd, labelTypes, standard, detailed);
return ImmutableMap.copyOf(Maps.transformValues(withStatus, LabelWithStatus::label));
}
use of com.google.gerrit.entities.LabelTypes in project gerrit by GerritCodeReview.
the class StreamEventsApiListener method approvalsAttributeSupplier.
private Supplier<ApprovalAttribute[]> approvalsAttributeSupplier(final Change change, Map<String, ApprovalInfo> newApprovals, final Map<String, ApprovalInfo> oldApprovals) {
final Map<String, Short> approvals = convertApprovalsMap(newApprovals);
return Suppliers.memoize(() -> {
Project.NameKey nameKey = change.getProject();
LabelTypes labelTypes = projectCache.get(nameKey).orElseThrow(illegalState(nameKey)).getLabelTypes();
if (approvals.size() > 0) {
ApprovalAttribute[] r = new ApprovalAttribute[approvals.size()];
int i = 0;
for (Map.Entry<String, Short> approval : approvals.entrySet()) {
r[i++] = getApprovalAttribute(labelTypes, approval, convertApprovalsMap(oldApprovals));
}
return r;
}
return null;
});
}
use of com.google.gerrit.entities.LabelTypes in project gerrit by GerritCodeReview.
the class GerritCommonTest method setUpEnvironment.
@Override
protected void setUpEnvironment(PrologEnvironment env) throws Exception {
LabelTypes labelTypes = new LabelTypes(Arrays.asList(TestLabels.codeReview(), TestLabels.verified()));
ChangeData cd = mock(ChangeData.class);
when(cd.getLabelTypes()).thenReturn(labelTypes);
env.set(StoredValues.CHANGE_DATA, cd);
}
use of com.google.gerrit.entities.LabelTypes 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));
}
}
}
Aggregations