Search in sources :

Example 1 with SubmitRuleEvaluator

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;
}
Also used : SubmitRuleEvaluator(com.google.gerrit.server.project.SubmitRuleEvaluator) SubmitRecord(com.google.gerrit.common.data.SubmitRecord) LabelTypes(com.google.gerrit.common.data.LabelTypes) PermissionRange(com.google.gerrit.common.data.PermissionRange) LabelType(com.google.gerrit.common.data.LabelType) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) LabelPermission(com.google.gerrit.server.permissions.LabelPermission)

Example 2 with SubmitRuleEvaluator

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;
}
Also used : SubmitRuleEvaluator(com.google.gerrit.server.project.SubmitRuleEvaluator) TestSubmitRuleInput(com.google.gerrit.extensions.common.TestSubmitRuleInput) SubmitTypeRecord(com.google.gerrit.common.data.SubmitTypeRecord) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Example 3 with SubmitRuleEvaluator

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);
}
Also used : SubmitRuleEvaluator(com.google.gerrit.server.project.SubmitRuleEvaluator) Inject(com.google.inject.Inject) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) SubmitRecord(com.google.gerrit.entities.SubmitRecord) SubmitRuleOptions(com.google.gerrit.server.project.SubmitRuleOptions) DefaultSubmitRule(com.google.gerrit.server.rules.DefaultSubmitRule) Collectors(java.util.stream.Collectors) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) List(java.util.List) SubmitRuleEvaluator(com.google.gerrit.server.project.SubmitRuleEvaluator) ImmutableList(com.google.common.collect.ImmutableList) LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) LabelFunction(com.google.gerrit.entities.LabelFunction) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) SubmitRecord(com.google.gerrit.entities.SubmitRecord) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 4 with SubmitRuleEvaluator

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;
}
Also used : SubmitRuleEvaluator(com.google.gerrit.server.project.SubmitRuleEvaluator) SubmitRecord(com.google.gerrit.common.data.SubmitRecord) TestSubmitRuleInput(com.google.gerrit.extensions.common.TestSubmitRuleInput) AccountLoader(com.google.gerrit.server.account.AccountLoader) AuthException(com.google.gerrit.extensions.restapi.AuthException) SubmitRecord(com.google.gerrit.common.data.SubmitRecord)

Example 5 with SubmitRuleEvaluator

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;
}
Also used : SubmitRuleEvaluator(com.google.gerrit.server.project.SubmitRuleEvaluator) SubmitRecord(com.google.gerrit.common.data.SubmitRecord)

Aggregations

SubmitRuleEvaluator (com.google.gerrit.server.project.SubmitRuleEvaluator)7 SubmitRecord (com.google.gerrit.common.data.SubmitRecord)3 SubmitRecord (com.google.gerrit.entities.SubmitRecord)3 ImmutableList (com.google.common.collect.ImmutableList)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)2 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)2 LabelFunction (com.google.gerrit.entities.LabelFunction)2 LabelType (com.google.gerrit.entities.LabelType)2 LabelValue (com.google.gerrit.entities.LabelValue)2 TestSubmitRuleInput (com.google.gerrit.extensions.common.TestSubmitRuleInput)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 SubmitRuleOptions (com.google.gerrit.server.project.SubmitRuleOptions)2 DefaultSubmitRule (com.google.gerrit.server.rules.DefaultSubmitRule)2 Inject (com.google.inject.Inject)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.Test)2 LabelType (com.google.gerrit.common.data.LabelType)1 LabelTypes (com.google.gerrit.common.data.LabelTypes)1