Search in sources :

Example 1 with TestSubmitRuleInput

use of com.google.gerrit.extensions.common.TestSubmitRuleInput 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 2 with TestSubmitRuleInput

use of com.google.gerrit.extensions.common.TestSubmitRuleInput in project gerrit by GerritCodeReview.

the class TestSubmitRule method apply.

@Override
public Response<TestSubmitRuleInfo> apply(RevisionResource rsrc, TestSubmitRuleInput input) throws AuthException, PermissionBackendException, BadRequestException {
    if (input == null) {
        input = new TestSubmitRuleInput();
    }
    if (input.rule == null) {
        throw new BadRequestException("rule is required");
    }
    if (!rules.isProjectRulesEnabled()) {
        throw new AuthException("project rules are disabled");
    }
    input.filters = MoreObjects.firstNonNull(input.filters, filters);
    Project.NameKey name = rsrc.getProject();
    Optional<ProjectState> project = projectCache.get(name);
    if (!project.isPresent()) {
        throw new BadRequestException("project not found " + name);
    }
    ChangeData cd = changeDataFactory.create(rsrc.getNotes());
    SubmitRecord record = prologRule.evaluate(cd, PrologOptions.dryRunOptions(input.rule, input.filters == Filters.SKIP));
    AccountLoader accounts = accountInfoFactory.create(true);
    TestSubmitRuleInfo out = newSubmitRuleInfo(record, accounts);
    accounts.fill();
    return Response.ok(out);
}
Also used : Project(com.google.gerrit.entities.Project) SubmitRecord(com.google.gerrit.entities.SubmitRecord) TestSubmitRuleInput(com.google.gerrit.extensions.common.TestSubmitRuleInput) AccountLoader(com.google.gerrit.server.account.AccountLoader) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) ProjectState(com.google.gerrit.server.project.ProjectState) ChangeData(com.google.gerrit.server.query.change.ChangeData) TestSubmitRuleInfo(com.google.gerrit.extensions.common.TestSubmitRuleInfo)

Example 3 with TestSubmitRuleInput

use of com.google.gerrit.extensions.common.TestSubmitRuleInput in project gerrit by GerritCodeReview.

the class TestSubmitType method apply.

@Override
public Response<SubmitType> apply(RevisionResource rsrc, TestSubmitRuleInput input) throws AuthException, BadRequestException {
    if (input == null) {
        input = new TestSubmitRuleInput();
    }
    if (input.rule == null) {
        throw new BadRequestException("rule is required");
    }
    if (!rules.isProjectRulesEnabled()) {
        throw new AuthException("project rules are disabled");
    }
    input.filters = MoreObjects.firstNonNull(input.filters, filters);
    ChangeData cd = changeDataFactory.create(rsrc.getNotes());
    SubmitTypeRecord rec = prologRule.getSubmitType(cd, PrologOptions.dryRunOptions(input.rule, input.filters == Filters.SKIP));
    if (rec.status != SubmitTypeRecord.Status.OK) {
        throw new BadRequestException(String.format("rule produced invalid result: %s", rec));
    }
    return Response.ok(rec.type);
}
Also used : TestSubmitRuleInput(com.google.gerrit.extensions.common.TestSubmitRuleInput) SubmitTypeRecord(com.google.gerrit.entities.SubmitTypeRecord) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 4 with TestSubmitRuleInput

use of com.google.gerrit.extensions.common.TestSubmitRuleInput 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 TestSubmitRuleInput

use of com.google.gerrit.extensions.common.TestSubmitRuleInput in project gerrit by GerritCodeReview.

the class SubmitTypeRuleIT method invalidSubmitRuleWithRulesInProject.

@Test
public void invalidSubmitRuleWithRulesInProject() throws Exception {
    setRulesPl(SUBMIT_TYPE_FROM_SUBJECT);
    String changeId = createChange("master", "change 1").getChangeId();
    TestSubmitRuleInput in = new TestSubmitRuleInput();
    in.rule = "invalid prolog rule";
    TestSubmitRuleInfo response = gApi.changes().id(changeId).current().testSubmitRule(in);
    assertThat(response).isEqualTo(invalidPrologRuleInfo());
}
Also used : TestSubmitRuleInput(com.google.gerrit.extensions.common.TestSubmitRuleInput) TestSubmitRuleInfo(com.google.gerrit.extensions.common.TestSubmitRuleInfo) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

TestSubmitRuleInput (com.google.gerrit.extensions.common.TestSubmitRuleInput)6 AuthException (com.google.gerrit.extensions.restapi.AuthException)4 TestSubmitRuleInfo (com.google.gerrit.extensions.common.TestSubmitRuleInfo)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)2 AccountLoader (com.google.gerrit.server.account.AccountLoader)2 SubmitRuleEvaluator (com.google.gerrit.server.project.SubmitRuleEvaluator)2 ChangeData (com.google.gerrit.server.query.change.ChangeData)2 Test (org.junit.Test)2 SubmitRecord (com.google.gerrit.common.data.SubmitRecord)1 SubmitTypeRecord (com.google.gerrit.common.data.SubmitTypeRecord)1 Project (com.google.gerrit.entities.Project)1 SubmitRecord (com.google.gerrit.entities.SubmitRecord)1 SubmitTypeRecord (com.google.gerrit.entities.SubmitTypeRecord)1 ProjectState (com.google.gerrit.server.project.ProjectState)1