Search in sources :

Example 1 with DefaultSubmitRule

use of com.google.gerrit.server.rules.DefaultSubmitRule in project gerrit by GerritCodeReview.

the class SubmitRuleEvaluator method evaluate.

/**
 * Evaluate the submit rules.
 *
 * @return List of {@link SubmitRecord} objects returned from the evaluated rules, including any
 *     errors.
 * @param cd ChangeData to evaluate
 */
public List<SubmitRecord> evaluate(ChangeData cd) {
    logger.atFine().log("Evaluate submit rules for change %d (caller: %s)", cd.change().getId().get(), callerFinder.findCallerLazy());
    try (Timer0.Context ignored = submitRuleEvaluationLatency.start()) {
        Change change;
        ProjectState projectState;
        try {
            change = cd.change();
            if (change == null) {
                throw new StorageException("Change not found");
            }
            Project.NameKey name = cd.project();
            Optional<ProjectState> projectStateOptional = projectCache.get(name);
            if (!projectStateOptional.isPresent()) {
                throw new NoSuchProjectException(name);
            }
            projectState = projectStateOptional.get();
        } catch (NoSuchProjectException e) {
            throw new IllegalStateException("Unable to find project while evaluating submit rule", e);
        }
        if (change.isClosed() && (!opts.recomputeOnClosedChanges() || OnlineReindexMode.isActive())) {
            return cd.notes().getSubmitRecords().stream().map(r -> {
                SubmitRecord record = r.deepCopy();
                if (record.status == SubmitRecord.Status.OK) {
                    // Submit records that were OK when they got merged are CLOSED now.
                    record.status = SubmitRecord.Status.CLOSED;
                }
                return record;
            }).collect(toImmutableList());
        }
        // and then we collect the results in one list.
        return Streams.stream(submitRules).filter(projectState.hasPrologRules() ? rule -> !(rule.get() instanceof DefaultSubmitRule) : rule -> true).map(c -> c.call(s -> {
            Optional<SubmitRecord> record = s.evaluate(cd);
            if (record.isPresent() && record.get().ruleName == null) {
                // Only back-fill the ruleName if it was not populated by the "submit
                // rule".
                record.get().ruleName = c.getPluginName() + "~" + s.getClass().getSimpleName();
            }
            return record;
        })).filter(Optional::isPresent).map(Optional::get).collect(toImmutableList());
    }
}
Also used : Inject(com.google.inject.Inject) Assisted(com.google.inject.assistedinject.Assisted) CallerFinder(com.google.gerrit.server.logging.CallerFinder) Description(com.google.gerrit.metrics.Description) PrologRule(com.google.gerrit.server.rules.PrologRule) Change(com.google.gerrit.entities.Change) OnlineReindexMode(com.google.gerrit.server.index.OnlineReindexMode) ChangeApi(com.google.gerrit.extensions.api.changes.ChangeApi) SubmitRule(com.google.gerrit.server.rules.SubmitRule) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StorageException(com.google.gerrit.exceptions.StorageException) Timer0(com.google.gerrit.metrics.Timer0) Units(com.google.gerrit.metrics.Description.Units) Streams(com.google.common.collect.Streams) SubmitRecord(com.google.gerrit.entities.SubmitRecord) DefaultSubmitRule(com.google.gerrit.server.rules.DefaultSubmitRule) SubmitTypeRecord(com.google.gerrit.entities.SubmitTypeRecord) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) Project(com.google.gerrit.entities.Project) PluginSetContext(com.google.gerrit.server.plugincontext.PluginSetContext) ChangeJson(com.google.gerrit.server.change.ChangeJson) Optional(java.util.Optional) MetricMaker(com.google.gerrit.metrics.MetricMaker) FluentLogger(com.google.common.flogger.FluentLogger) Optional(java.util.Optional) DefaultSubmitRule(com.google.gerrit.server.rules.DefaultSubmitRule) Change(com.google.gerrit.entities.Change) Project(com.google.gerrit.entities.Project) SubmitRecord(com.google.gerrit.entities.SubmitRecord) Timer0(com.google.gerrit.metrics.Timer0) StorageException(com.google.gerrit.exceptions.StorageException)

Aggregations

ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Streams (com.google.common.collect.Streams)1 FluentLogger (com.google.common.flogger.FluentLogger)1 Change (com.google.gerrit.entities.Change)1 Project (com.google.gerrit.entities.Project)1 SubmitRecord (com.google.gerrit.entities.SubmitRecord)1 SubmitTypeRecord (com.google.gerrit.entities.SubmitTypeRecord)1 StorageException (com.google.gerrit.exceptions.StorageException)1 ChangeApi (com.google.gerrit.extensions.api.changes.ChangeApi)1 Description (com.google.gerrit.metrics.Description)1 Units (com.google.gerrit.metrics.Description.Units)1 MetricMaker (com.google.gerrit.metrics.MetricMaker)1 Timer0 (com.google.gerrit.metrics.Timer0)1 ChangeJson (com.google.gerrit.server.change.ChangeJson)1 OnlineReindexMode (com.google.gerrit.server.index.OnlineReindexMode)1 CallerFinder (com.google.gerrit.server.logging.CallerFinder)1 PluginSetContext (com.google.gerrit.server.plugincontext.PluginSetContext)1 ChangeData (com.google.gerrit.server.query.change.ChangeData)1 DefaultSubmitRule (com.google.gerrit.server.rules.DefaultSubmitRule)1 PrologRule (com.google.gerrit.server.rules.PrologRule)1