Search in sources :

Example 26 with SubmitRecord

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

the class TestSubmitRule method newSubmitRuleInfo.

private static TestSubmitRuleInfo newSubmitRuleInfo(SubmitRecord r, AccountLoader accounts) {
    TestSubmitRuleInfo info = new TestSubmitRuleInfo();
    info.status = r.status.name();
    info.errorMessage = r.errorMessage;
    if (r.labels != null) {
        for (SubmitRecord.Label n : r.labels) {
            AccountInfo who = n.appliedBy != null ? accounts.get(n.appliedBy) : new AccountInfo(null);
            label(info, n, who);
        }
    }
    return info;
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) TestSubmitRuleInfo(com.google.gerrit.extensions.common.TestSubmitRuleInfo) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Example 27 with SubmitRecord

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

the class PrologRuleEvaluator method resultsToSubmitRecord.

/**
 * Convert the results from Prolog Cafe's format to Gerrit's common format.
 *
 * <p>can_submit/1 terminates when an ok(P) record is found. Therefore walk the results backwards,
 * using only that ok(P) record if it exists. This skips partial results that occur early in the
 * output. Later after the loop the out collection is reversed to restore it to the original
 * ordering.
 */
public SubmitRecord resultsToSubmitRecord(Term submitRule, List<Term> results) {
    checkState(!results.isEmpty(), "the list of Prolog terms must not be empty");
    SubmitRecord resultSubmitRecord = new SubmitRecord();
    resultSubmitRecord.labels = new ArrayList<>();
    for (int resultIdx = results.size() - 1; 0 <= resultIdx; resultIdx--) {
        Term submitRecord = results.get(resultIdx);
        if (!(submitRecord instanceof StructureTerm) || 1 != submitRecord.arity()) {
            return invalidResult(submitRule, submitRecord);
        }
        if (!"ok".equals(submitRecord.name()) && !"not_ready".equals(submitRecord.name())) {
            return invalidResult(submitRule, submitRecord);
        }
        // the change to be submittable when at least one result is OK.
        if ("ok".equals(submitRecord.name())) {
            resultSubmitRecord.status = SubmitRecord.Status.OK;
        } else if ("not_ready".equals(submitRecord.name()) && resultSubmitRecord.status == null) {
            resultSubmitRecord.status = SubmitRecord.Status.NOT_READY;
        }
        // Unpack the one argument. This should also be a structure with one
        // argument per label that needs to be reported on to the caller.
        // 
        submitRecord = submitRecord.arg(0);
        if (!(submitRecord instanceof StructureTerm)) {
            return invalidResult(submitRule, submitRecord);
        }
        for (Term state : ((StructureTerm) submitRecord).args()) {
            if (!(state instanceof StructureTerm) || 2 != state.arity() || !"label".equals(state.name())) {
                return invalidResult(submitRule, submitRecord);
            }
            SubmitRecord.Label lbl = new SubmitRecord.Label();
            resultSubmitRecord.labels.add(lbl);
            lbl.label = checkLabelName(state.arg(0).name());
            Term status = state.arg(1);
            try {
                if ("ok".equals(status.name())) {
                    lbl.status = SubmitRecord.Label.Status.OK;
                    appliedBy(lbl, status);
                } else if ("reject".equals(status.name())) {
                    lbl.status = SubmitRecord.Label.Status.REJECT;
                    appliedBy(lbl, status);
                } else if ("need".equals(status.name())) {
                    lbl.status = SubmitRecord.Label.Status.NEED;
                } else if ("may".equals(status.name())) {
                    lbl.status = SubmitRecord.Label.Status.MAY;
                } else if ("impossible".equals(status.name())) {
                    lbl.status = SubmitRecord.Label.Status.IMPOSSIBLE;
                } else {
                    return invalidResult(submitRule, submitRecord);
                }
            } catch (UserTermExpected e) {
                return invalidResult(submitRule, submitRecord, e.getMessage());
            }
        }
        if (resultSubmitRecord.status == SubmitRecord.Status.OK) {
            break;
        }
    }
    Collections.reverse(resultSubmitRecord.labels);
    return resultSubmitRecord;
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm)

Example 28 with SubmitRecord

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

the class ChangeNotesParser method parseSubmitRecords.

private void parseSubmitRecords(List<String> lines) throws ConfigInvalidException {
    SubmitRecord rec = null;
    for (String line : lines) {
        int c = line.indexOf(": ");
        if (c < 0) {
            rec = new SubmitRecord();
            submitRecords.add(rec);
            int s = line.indexOf(' ');
            String statusStr = s >= 0 ? line.substring(0, s) : line;
            rec.status = Enums.getIfPresent(SubmitRecord.Status.class, statusStr).orNull();
            checkFooter(rec.status != null, FOOTER_SUBMITTED_WITH, line);
            if (s >= 0) {
                rec.errorMessage = line.substring(s);
            }
        } else {
            checkFooter(rec != null, FOOTER_SUBMITTED_WITH, line);
            if (line.startsWith("Rule-Name: ")) {
                String ruleName = RULE_SPLITTER.splitToList(line).get(1);
                rec.ruleName = ruleName;
                continue;
            }
            SubmitRecord.Label label = new SubmitRecord.Label();
            if (rec.labels == null) {
                rec.labels = new ArrayList<>();
            }
            rec.labels.add(label);
            label.status = Enums.getIfPresent(SubmitRecord.Label.Status.class, line.substring(0, c)).orNull();
            checkFooter(label.status != null, FOOTER_SUBMITTED_WITH, line);
            int c2 = line.indexOf(": ", c + 2);
            if (c2 >= 0) {
                label.label = line.substring(c + 2, c2);
                PersonIdent ident = RawParseUtils.parsePersonIdent(line.substring(c2 + 2));
                checkFooter(ident != null, FOOTER_SUBMITTED_WITH, line);
                label.appliedBy = parseIdent(ident);
            } else {
                label.label = line.substring(c + 2);
            }
        }
    }
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) PersonIdent(org.eclipse.jgit.lib.PersonIdent)

Example 29 with SubmitRecord

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

the class EventFactory method addSubmitRecordLabels.

private void addSubmitRecordLabels(SubmitRecord submitRecord, SubmitRecordAttribute sa) {
    if (submitRecord.labels != null && !submitRecord.labels.isEmpty()) {
        sa.labels = new ArrayList<>();
        for (SubmitRecord.Label lbl : submitRecord.labels) {
            SubmitLabelAttribute la = new SubmitLabelAttribute();
            la.label = lbl.label;
            la.status = lbl.status.name();
            if (lbl.appliedBy != null) {
                la.by = asAccountAttribute(lbl.appliedBy);
            }
            sa.labels.add(la);
        }
    }
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) SubmitLabelAttribute(com.google.gerrit.server.data.SubmitLabelAttribute)

Example 30 with SubmitRecord

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

the class EventFactory method addSubmitRecords.

/**
 * Add submitRecords to an existing {@link ChangeAttribute}.
 */
public void addSubmitRecords(ChangeAttribute ca, List<SubmitRecord> submitRecords) {
    ca.submitRecords = new ArrayList<>();
    for (SubmitRecord submitRecord : submitRecords) {
        SubmitRecordAttribute sa = new SubmitRecordAttribute();
        sa.status = submitRecord.status.name();
        if (submitRecord.status != SubmitRecord.Status.RULE_ERROR) {
            addSubmitRecordLabels(submitRecord, sa);
            addSubmitRecordRequirements(submitRecord, sa);
        }
        ca.submitRecords.add(sa);
    }
    // Remove empty lists so a confusing label won't be displayed in the output.
    if (ca.submitRecords.isEmpty()) {
        ca.submitRecords = null;
    }
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) SubmitRecordAttribute(com.google.gerrit.server.data.SubmitRecordAttribute)

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