Search in sources :

Example 1 with SubmitRecord

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

the class FakeSubmitRule method evaluate.

@Override
public Optional<SubmitRecord> evaluate(ChangeData cd) {
    SubmitRecord record = new SubmitRecord();
    record.status = cd.hashtags().isEmpty() ? Status.NOT_READY : Status.OK;
    record.ruleName = FakeSubmitRule.class.getSimpleName();
    return Optional.of(record);
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord)

Example 2 with SubmitRecord

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

the class ChangeJson method submitRecordToInfo.

private SubmitRecordInfo submitRecordToInfo(SubmitRecord record) {
    SubmitRecordInfo info = new SubmitRecordInfo();
    if (record.status != null) {
        info.status = SubmitRecordInfo.Status.valueOf(record.status.name());
    }
    info.ruleName = record.ruleName;
    info.errorMessage = record.errorMessage;
    if (record.labels != null) {
        info.labels = new ArrayList<>();
        for (SubmitRecord.Label label : record.labels) {
            SubmitRecordInfo.Label labelInfo = new SubmitRecordInfo.Label();
            labelInfo.label = label.label;
            if (label.status != null) {
                labelInfo.status = SubmitRecordInfo.Label.Status.valueOf(label.status.name());
            }
            labelInfo.appliedBy = accountLoader.get(label.appliedBy);
            info.labels.add(labelInfo);
        }
    }
    if (record.requirements != null) {
        info.requirements = new ArrayList<>();
        for (LegacySubmitRequirement requirement : record.requirements) {
            info.requirements.add(requirementToInfo(requirement, record.status));
        }
    }
    return info;
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) LegacySubmitRequirement(com.google.gerrit.entities.LegacySubmitRequirement) SubmitRecordInfo(com.google.gerrit.extensions.common.SubmitRecordInfo)

Example 3 with SubmitRecord

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

the class SubmitRequirementsAdapter method getLegacyRequirements.

/**
 * Retrieve legacy submit records (created by label functions and other {@link
 * com.google.gerrit.server.rules.SubmitRule}s) and convert them to submit requirement results.
 */
public static Map<SubmitRequirement, SubmitRequirementResult> getLegacyRequirements(ChangeData cd) {
    // We use SubmitRuleOptions.defaults() which does not recompute submit rules for closed changes.
    // This doesn't have an effect since we never call this class (i.e. to evaluate submit
    // requirements) for closed changes.
    List<SubmitRecord> records = cd.submitRecords(SubmitRuleOptions.defaults());
    boolean areForced = records.stream().anyMatch(record -> SubmitRecord.Status.FORCED.equals(record.status));
    List<LabelType> labelTypes = cd.getLabelTypes().getLabelTypes();
    ObjectId commitId = cd.currentPatchSet().commitId();
    Map<String, List<SubmitRequirementResult>> srsByName = records.stream().filter(r -> !SubmitRecord.Status.FORCED.equals(r.status)).map(r -> createResult(r, labelTypes, commitId, areForced)).flatMap(List::stream).collect(Collectors.groupingBy(sr -> sr.submitRequirement().name()));
    // We convert submit records to submit requirements by generating a separate
    // submit requirement result for each available label in each submit record.
    // The SR status is derived from the label status of the submit record.
    // This conversion might result in duplicate entries.
    // One such example can be a prolog rule emitting the same label name twice.
    // Another case might happen if two different submit rules emit the same label
    // name. In such cases, we need to merge these entries and return a single submit
    // requirement result. If both entries agree in their status, return any of them.
    // Otherwise, favour the entry that is blocking submission.
    ImmutableMap.Builder<SubmitRequirement, SubmitRequirementResult> result = ImmutableMap.builder();
    for (Map.Entry<String, List<SubmitRequirementResult>> entry : srsByName.entrySet()) {
        if (entry.getValue().size() == 1) {
            SubmitRequirementResult srResult = entry.getValue().iterator().next();
            result.put(srResult.submitRequirement(), srResult);
            continue;
        }
        // If all submit requirements with the same name match in status, return the first one.
        List<SubmitRequirementResult> resultsSameName = entry.getValue();
        boolean allNonBlocking = resultsSameName.stream().allMatch(sr -> sr.fulfilled());
        if (allNonBlocking) {
            result.put(resultsSameName.get(0).submitRequirement(), resultsSameName.get(0));
        } else {
            // Otherwise, return the first submit requirement result that is blocking submission.
            Optional<SubmitRequirementResult> nonFulfilled = resultsSameName.stream().filter(sr -> !sr.fulfilled()).findFirst();
            if (nonFulfilled.isPresent()) {
                result.put(nonFulfilled.get().submitRequirement(), nonFulfilled.get());
            }
        }
    }
    return result.build();
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) SubmitRequirementExpression(com.google.gerrit.entities.SubmitRequirementExpression) SubmitRecord(com.google.gerrit.entities.SubmitRecord) DefaultSubmitRule(com.google.gerrit.server.rules.DefaultSubmitRule) Collectors(java.util.stream.Collectors) ObjectId(org.eclipse.jgit.lib.ObjectId) SubmitRequirementExpressionResult(com.google.gerrit.entities.SubmitRequirementExpressionResult) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult) Strings(com.google.common.base.Strings) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) Label(com.google.gerrit.entities.SubmitRecord.Label) ImmutableList(com.google.common.collect.ImmutableList) LabelType(com.google.gerrit.entities.LabelType) ChangeQueryBuilder(com.google.gerrit.server.query.change.ChangeQueryBuilder) Map(java.util.Map) Optional(java.util.Optional) FluentLogger(com.google.common.flogger.FluentLogger) Status(com.google.gerrit.entities.SubmitRequirementExpressionResult.Status) SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) ObjectId(org.eclipse.jgit.lib.ObjectId) ImmutableMap(com.google.common.collect.ImmutableMap) SubmitRecord(com.google.gerrit.entities.SubmitRecord) SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) LabelType(com.google.gerrit.entities.LabelType) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 4 with SubmitRecord

use of com.google.gerrit.entities.SubmitRecord 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)

Example 5 with SubmitRecord

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

the class ChangeNotesTest method submitRecords.

@Test
public void submitRecords() throws Exception {
    Change c = newChange();
    SubmissionId submissionId = new SubmissionId(c);
    ChangeUpdate update = newUpdate(c, changeOwner);
    update.setSubjectForCommit("Submit patch set 1");
    update.merge(submissionId, ImmutableList.of(submitRecord("NOT_READY", null, submitLabel(LabelId.VERIFIED, "OK", changeOwner.getAccountId()), submitLabel(LabelId.CODE_REVIEW, "NEED", null)), submitRecord("NOT_READY", null, submitLabel(LabelId.VERIFIED, "OK", changeOwner.getAccountId()), submitLabel("Alternative-Code-Review", "NEED", null))));
    update.commit();
    ChangeNotes notes = newNotes(c);
    List<SubmitRecord> recs = notes.getSubmitRecords();
    assertThat(recs).hasSize(2);
    assertThat(recs.get(0)).isEqualTo(submitRecord("NOT_READY", null, submitLabel(LabelId.VERIFIED, "OK", changeOwner.getAccountId()), submitLabel(LabelId.CODE_REVIEW, "NEED", null)));
    assertThat(recs.get(1)).isEqualTo(submitRecord("NOT_READY", null, submitLabel(LabelId.VERIFIED, "OK", changeOwner.getAccountId()), submitLabel("Alternative-Code-Review", "NEED", null)));
    assertThat(notes.getChange().getSubmissionId()).isEqualTo(submissionId.toString());
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) SubmissionId(com.google.gerrit.entities.SubmissionId) Change(com.google.gerrit.entities.Change) Test(org.junit.Test)

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