use of com.google.gerrit.server.project.SubmitRuleEvaluator 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.project.SubmitRuleEvaluator in project gerrit by GerritCodeReview.
the class TestSubmitType method apply.
@Override
public SubmitType apply(RevisionResource rsrc, TestSubmitRuleInput input) throws AuthException, BadRequestException, OrmException {
if (input == null) {
input = new TestSubmitRuleInput();
}
if (input.rule != null && !rules.isProjectRulesEnabled()) {
throw new AuthException("project rules are disabled");
}
input.filters = MoreObjects.firstNonNull(input.filters, filters);
SubmitRuleEvaluator evaluator = new SubmitRuleEvaluator(changeDataFactory.create(db.get(), rsrc.getControl()));
SubmitTypeRecord rec = evaluator.setPatchSet(rsrc.getPatchSet()).setLogErrors(false).setSkipSubmitFilters(input.filters == Filters.SKIP).setRule(input.rule).getSubmitType();
if (rec.status != SubmitTypeRecord.Status.OK) {
throw new BadRequestException(String.format("rule %s produced invalid result: %s", evaluator.getSubmitRuleName(), rec));
}
return rec.type;
}
use of com.google.gerrit.server.project.SubmitRuleEvaluator in project gerrit by GerritCodeReview.
the class SubmitRuleIT method submitRecordsForClosedChanges_parsedBackByDefault.
@Test
public void submitRecordsForClosedChanges_parsedBackByDefault() throws Exception {
SubmitRuleEvaluator submitRuleEvaluator = submitRuleEvaluatorFactory.create(SubmitRuleOptions.defaults());
PushOneCommit.Result r = createChange();
approve(r.getChangeId());
List<SubmitRecord> recordsBeforeSubmission = submitRuleEvaluator.evaluate(r.getChange());
assertThat(recordsBeforeSubmission.stream().map(record -> record.ruleName).collect(Collectors.toList())).containsExactly(DefaultSubmitRule.RULE_NAME);
gApi.changes().id(r.getChangeId()).current().submit();
// Add a new label that blocks submission if not granted. In case we reevaluate the rules,
// this would show up as blocking submission.
setupCustomBlockingLabel();
List<SubmitRecord> recordsAfterSubmission = submitRuleEvaluator.evaluate(r.getChange());
recordsBeforeSubmission.forEach(// Set status to closed
sr -> sr.status = SubmitRecord.Status.CLOSED);
assertThat(recordsBeforeSubmission).isEqualTo(recordsAfterSubmission);
}
use of com.google.gerrit.server.project.SubmitRuleEvaluator in project gerrit by GerritCodeReview.
the class TestSubmitRule method apply.
@Override
public List<Record> apply(RevisionResource rsrc, TestSubmitRuleInput input) throws AuthException, OrmException {
if (input == null) {
input = new TestSubmitRuleInput();
}
if (input.rule != null && !rules.isProjectRulesEnabled()) {
throw new AuthException("project rules are disabled");
}
input.filters = MoreObjects.firstNonNull(input.filters, filters);
SubmitRuleEvaluator evaluator = new SubmitRuleEvaluator(changeDataFactory.create(db.get(), rsrc.getControl()));
List<SubmitRecord> records = evaluator.setPatchSet(rsrc.getPatchSet()).setLogErrors(false).setSkipSubmitFilters(input.filters == Filters.SKIP).setRule(input.rule).evaluate();
List<Record> out = Lists.newArrayListWithCapacity(records.size());
AccountLoader accounts = accountInfoFactory.create(true);
for (SubmitRecord r : records) {
out.add(new Record(r, accounts));
}
if (!out.isEmpty()) {
out.get(0).prologReductionCount = evaluator.getReductionsConsumed();
}
accounts.fill();
return out;
}
use of com.google.gerrit.server.project.SubmitRuleEvaluator in project gerrit by GerritCodeReview.
the class ChangeData method submitRecords.
public List<SubmitRecord> submitRecords(SubmitRuleOptions options) throws OrmException {
List<SubmitRecord> records = submitRecords.get(options);
if (records == null) {
if (!lazyLoad) {
return Collections.emptyList();
}
records = new SubmitRuleEvaluator(this).setOptions(options).evaluate();
submitRecords.put(options, records);
}
return records;
}
Aggregations