use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class PatchSetUtil method isPatchSetLocked.
/**
* Is the current patch set locked against state changes?
*/
public boolean isPatchSetLocked(ChangeNotes notes) {
Change change = notes.getChange();
if (change.isMerged()) {
return false;
}
ProjectState projectState = projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName()));
ApprovalsUtil approvalsUtil = approvalsUtilProvider.get();
for (PatchSetApproval ap : approvalsUtil.byPatchSet(notes, change.currentPatchSetId())) {
Optional<LabelType> type = projectState.getLabelTypes(notes).byLabel(ap.label());
if (type.isPresent() && ap.value() == 1 && type.get().getFunction() == LabelFunction.PATCH_SET_LOCK) {
return true;
}
}
return false;
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class ApprovalsUtil method addApprovalsForNewPatchSet.
/**
* Adds approvals to ChangeUpdate for a new patch set, and writes to NoteDb.
*
* @param update change update.
* @param labelTypes label types for the containing project.
* @param ps patch set being approved.
* @param user user adding approvals.
* @param approvals approvals to add.
*/
public Iterable<PatchSetApproval> addApprovalsForNewPatchSet(ChangeUpdate update, LabelTypes labelTypes, PatchSet ps, CurrentUser user, Map<String, Short> approvals) throws RestApiException, PermissionBackendException {
Account.Id accountId = user.getAccountId();
checkArgument(accountId.equals(ps.uploader()), "expected user %s to match patch set uploader %s", accountId, ps.uploader());
if (approvals.isEmpty()) {
return ImmutableList.of();
}
checkApprovals(approvals, permissionBackend.user(user).change(update.getNotes()));
List<PatchSetApproval> cells = new ArrayList<>(approvals.size());
Instant ts = update.getWhen();
for (Map.Entry<String, Short> vote : approvals.entrySet()) {
Optional<LabelType> lt = labelTypes.byLabel(vote.getKey());
if (!lt.isPresent()) {
throw new BadRequestException(String.format("label \"%s\" is not a configured label", vote.getKey()));
}
cells.add(newApproval(ps.id(), user, lt.get().getLabelId(), vote.getValue(), ts).build());
}
for (PatchSetApproval psa : cells) {
update.putApproval(psa.label(), psa.value());
}
return cells;
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class SubmitRequirementsAdapter method getLegacyRequirements.
/**
* Retrieve legacy submit records (created by label functions and other {@link
* com.google.gerrit.server.rules.SubmitRule}s) and convert them to submit requirement results.
*/
public static Map<SubmitRequirement, SubmitRequirementResult> getLegacyRequirements(ChangeData cd) {
// We use SubmitRuleOptions.defaults() which does not recompute submit rules for closed changes.
// This doesn't have an effect since we never call this class (i.e. to evaluate submit
// requirements) for closed changes.
List<SubmitRecord> records = cd.submitRecords(SubmitRuleOptions.defaults());
boolean areForced = records.stream().anyMatch(record -> SubmitRecord.Status.FORCED.equals(record.status));
List<LabelType> labelTypes = cd.getLabelTypes().getLabelTypes();
ObjectId commitId = cd.currentPatchSet().commitId();
Map<String, List<SubmitRequirementResult>> srsByName = records.stream().filter(r -> !SubmitRecord.Status.FORCED.equals(r.status)).map(r -> createResult(r, labelTypes, commitId, areForced)).flatMap(List::stream).collect(Collectors.groupingBy(sr -> sr.submitRequirement().name()));
// We convert submit records to submit requirements by generating a separate
// submit requirement result for each available label in each submit record.
// The SR status is derived from the label status of the submit record.
// This conversion might result in duplicate entries.
// One such example can be a prolog rule emitting the same label name twice.
// Another case might happen if two different submit rules emit the same label
// name. In such cases, we need to merge these entries and return a single submit
// requirement result. If both entries agree in their status, return any of them.
// Otherwise, favour the entry that is blocking submission.
ImmutableMap.Builder<SubmitRequirement, SubmitRequirementResult> result = ImmutableMap.builder();
for (Map.Entry<String, List<SubmitRequirementResult>> entry : srsByName.entrySet()) {
if (entry.getValue().size() == 1) {
SubmitRequirementResult srResult = entry.getValue().iterator().next();
result.put(srResult.submitRequirement(), srResult);
continue;
}
// If all submit requirements with the same name match in status, return the first one.
List<SubmitRequirementResult> resultsSameName = entry.getValue();
boolean allNonBlocking = resultsSameName.stream().allMatch(sr -> sr.fulfilled());
if (allNonBlocking) {
result.put(resultsSameName.get(0).submitRequirement(), resultsSameName.get(0));
} else {
// Otherwise, return the first submit requirement result that is blocking submission.
Optional<SubmitRequirementResult> nonFulfilled = resultsSameName.stream().filter(sr -> !sr.fulfilled()).findFirst();
if (nonFulfilled.isPresent()) {
result.put(nonFulfilled.get().submitRequirement(), nonFulfilled.get());
}
}
}
return result.build();
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class SubmitRequirementsAdapter method createFromDefaultSubmitRecord.
private static List<SubmitRequirementResult> createFromDefaultSubmitRecord(List<Label> labels, List<LabelType> labelTypes, ObjectId psCommitId, boolean isForced) {
ImmutableList.Builder<SubmitRequirementResult> result = ImmutableList.builder();
for (Label label : labels) {
if (skipSubmitRequirementFor(label)) {
continue;
}
Optional<LabelType> maybeLabelType = getLabelType(labelTypes, label.label);
if (!maybeLabelType.isPresent()) {
// if it was blocking or not, hence we skip the label.
continue;
}
LabelType labelType = maybeLabelType.get();
if (!isBlocking(labelType)) {
continue;
}
ImmutableList<String> atoms = toExpressionAtomList(labelType);
SubmitRequirement.Builder req = SubmitRequirement.builder().setName(label.label).setSubmittabilityExpression(toExpression(atoms)).setAllowOverrideInChildProjects(labelType.isCanOverride());
result.add(SubmitRequirementResult.builder().legacy(Optional.of(true)).submitRequirement(req.build()).submittabilityExpressionResult(createExpressionResult(toExpression(atoms), mapStatus(label), atoms)).patchSetCommitId(psCommitId).forced(Optional.of(isForced)).build());
}
return result.build();
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class MagicLabelPredicate method match.
@Override
public boolean match(ChangeData changeData) {
Change change = changeData.change();
if (change == null) {
//
return false;
}
Optional<ProjectState> project = args.projectCache.get(change.getDest().project());
if (!project.isPresent()) {
//
return false;
}
LabelType labelType = type(project.get().getLabelTypes(), magicLabelVote.label());
if (labelType == null) {
// Label is not defined by this project.
return false;
}
switch(magicLabelVote.value()) {
case ANY:
return matchAny(changeData, labelType);
case MIN:
return matchNumeric(changeData, magicLabelVote.label(), labelType.getMin().getValue());
case MAX:
return matchNumeric(changeData, magicLabelVote.label(), labelType.getMax().getValue());
}
throw new IllegalStateException("Unsupported magic label value: " + magicLabelVote.value());
}
Aggregations