Search in sources :

Example 46 with SubmitRecord

use of com.google.gerrit.entities.SubmitRecord in project gerrit by GerritCodeReview.

the class SubmitRuleIT method submitRecordsForClosedChanges_recomputedIfRequested.

@Test
public void submitRecordsForClosedChanges_recomputedIfRequested() throws Exception {
    SubmitRuleEvaluator submitRuleEvaluator = submitRuleEvaluatorFactory.create(SubmitRuleOptions.builder().recomputeOnClosedChanges(true).build());
    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());
    assertThat(recordsBeforeSubmission).isNotEqualTo(recordsAfterSubmission);
    assertThat(recordsAfterSubmission).hasSize(1);
    List<SubmitRecord.Label> recordLabels = recordsAfterSubmission.get(0).labels;
    assertThat(recordLabels).hasSize(2);
    assertCodeReviewApproved(recordLabels);
    assertMyLabelNeed(recordLabels);
}
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 47 with SubmitRecord

use of com.google.gerrit.entities.SubmitRecord in project gerrit by GerritCodeReview.

the class PrologRuleEvaluatorIT method convertsPrologToSubmitRecord.

@Test
public void convertsPrologToSubmitRecord() {
    PrologRuleEvaluator evaluator = makeEvaluator();
    StructureTerm verifiedLabel = makeLabel(LabelId.VERIFIED, "may");
    StructureTerm labels = new StructureTerm("label", verifiedLabel);
    List<Term> terms = ImmutableList.of(makeTerm("ok", labels));
    SubmitRecord record = evaluator.resultsToSubmitRecord(null, terms);
    assertThat(record.status).isEqualTo(SubmitRecord.Status.OK);
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) PrologRuleEvaluator(com.google.gerrit.server.rules.PrologRuleEvaluator) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 48 with SubmitRecord

use of com.google.gerrit.entities.SubmitRecord in project gerrit by GerritCodeReview.

the class PrologRuleEvaluatorIT method abortsEarlyWithOkayRecord.

/**
 * The Prolog behavior is everything but intuitive. Several submit_rules can be defined, and each
 * will provide a different SubmitRecord answer when queried. The current implementation stops
 * parsing the Prolog terms into SubmitRecord objects once it finds an OK record. This might lead
 * to tangling results, as reproduced by this test.
 *
 * <p>Let's consider this rules.pl file (equivalent to the code in this test)
 *
 * <pre>{@code
 * submit_rule(submit(R)) :-
 *     gerrit:uploader(U),
 *     R = label('Verified', reject(U)).
 *
 * submit_rule(submit(CR, V)) :-
 *     gerrit:uploader(U),
 *     V = label('Code-Review', ok(U)).
 *
 * submit_rule(submit(R)) :-
 *     gerrit:uploader(U),
 *     R = label('Any-Label-Name', reject(U)).
 * }</pre>
 *
 * The first submit_rule always fails because the Verified label is rejected.
 *
 * <p>The second submit_rule is always valid, and provides two labels: OK and Code-Review.
 *
 * <p>The third submit_rule always fails because the Any-Label-Name label is rejected.
 *
 * <p>In this case, the last two SubmitRecords are used, the first one is discarded.
 */
@Test
public void abortsEarlyWithOkayRecord() {
    PrologRuleEvaluator evaluator = makeEvaluator();
    SubmitRecord.Label submitRecordLabel1 = new SubmitRecord.Label();
    submitRecordLabel1.label = LabelId.VERIFIED;
    submitRecordLabel1.status = SubmitRecord.Label.Status.REJECT;
    submitRecordLabel1.appliedBy = admin.id();
    SubmitRecord.Label submitRecordLabel2 = new SubmitRecord.Label();
    submitRecordLabel2.label = LabelId.CODE_REVIEW;
    submitRecordLabel2.status = SubmitRecord.Label.Status.OK;
    submitRecordLabel2.appliedBy = admin.id();
    SubmitRecord.Label submitRecordLabel3 = new SubmitRecord.Label();
    submitRecordLabel3.label = "Any-Label-Name";
    submitRecordLabel3.status = SubmitRecord.Label.Status.REJECT;
    submitRecordLabel3.appliedBy = user.id();
    List<Term> terms = new ArrayList<>();
    StructureTerm label1 = makeLabel(submitRecordLabel1.label, "reject", admin);
    StructureTerm label2 = makeLabel(submitRecordLabel2.label, "ok", admin);
    StructureTerm label3 = makeLabel(submitRecordLabel3.label, "reject", user);
    terms.add(makeTerm("not_ready", makeLabels(label1)));
    terms.add(makeTerm("ok", makeLabels(label2)));
    terms.add(makeTerm("not_ready", makeLabels(label3)));
    // When
    SubmitRecord record = evaluator.resultsToSubmitRecord(null, terms);
    // assert that
    SubmitRecord expectedRecord = new SubmitRecord();
    expectedRecord.status = SubmitRecord.Status.OK;
    expectedRecord.labels = new ArrayList<>();
    expectedRecord.labels.add(submitRecordLabel2);
    expectedRecord.labels.add(submitRecordLabel3);
    assertThat(record).isEqualTo(expectedRecord);
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) ArrayList(java.util.ArrayList) PrologRuleEvaluator(com.google.gerrit.server.rules.PrologRuleEvaluator) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 49 with SubmitRecord

use of com.google.gerrit.entities.SubmitRecord in project gerrit by GerritCodeReview.

the class RulesIT method getStatus.

private SubmitRecord.Status getStatus(PushOneCommit.Result result) throws Exception {
    ChangeData cd = result.getChange();
    Collection<SubmitRecord> records;
    try (AutoCloseable ignored1 = disableChangeIndex();
        AutoCloseable ignored2 = disableAccountIndex()) {
        SubmitRuleEvaluator ruleEvaluator = evaluatorFactory.create(SubmitRuleOptions.defaults());
        records = ruleEvaluator.evaluate(cd);
    }
    assertThat(records).hasSize(1);
    SubmitRecord record = records.iterator().next();
    return record.status;
}
Also used : SubmitRuleEvaluator(com.google.gerrit.server.project.SubmitRuleEvaluator) SubmitRecord(com.google.gerrit.entities.SubmitRecord) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Aggregations

SubmitRecord (com.google.gerrit.entities.SubmitRecord)49 Test (org.junit.Test)24 SubmitRequirementResult (com.google.gerrit.entities.SubmitRequirementResult)14 Change (com.google.gerrit.entities.Change)7 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)6 LabelType (com.google.gerrit.entities.LabelType)6 List (java.util.List)6 PatchSetApproval (com.google.gerrit.entities.PatchSetApproval)5 ChangeData (com.google.gerrit.server.query.change.ChangeData)5 Inject (com.google.inject.Inject)5 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5 ImmutableList (com.google.common.collect.ImmutableList)4 FluentLogger (com.google.common.flogger.FluentLogger)4 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)4 LabelFunction (com.google.gerrit.entities.LabelFunction)4 PatchSet (com.google.gerrit.entities.PatchSet)4 Project (com.google.gerrit.entities.Project)4 StorageException (com.google.gerrit.exceptions.StorageException)4 DefaultSubmitRule (com.google.gerrit.server.rules.DefaultSubmitRule)4