Search in sources :

Example 11 with SubmitRecord

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

the class ChangeFieldTest method record.

private static SubmitRecord record(SubmitRecord.Status status, SubmitRecord.Label... labels) {
    SubmitRecord r = new SubmitRecord();
    r.status = status;
    if (labels.length > 0) {
        r.labels = ImmutableList.copyOf(labels);
    }
    return r;
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord)

Example 12 with SubmitRecord

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

the class CommitRewriter method collectAccounts.

/**
 * Retrieves accounts, that are associated with a change (e.g. reviewers, commenters, etc.). These
 * accounts are used to verify that commits do not contain user data. See {@link #verifyCommit}
 *
 * @param changeNotes {@link ChangeNotes} of the change to retrieve associated accounts from.
 * @return {@link AccountState} of accounts, that are associated with the change.
 */
private ImmutableSet<AccountState> collectAccounts(ChangeNotes changeNotes) {
    Set<Account.Id> accounts = new HashSet<>();
    accounts.add(changeNotes.getChange().getOwner());
    for (PatchSetApproval patchSetApproval : changeNotes.getApprovals().values()) {
        if (patchSetApproval.accountId() != null) {
            accounts.add(patchSetApproval.accountId());
        }
        if (patchSetApproval.realAccountId() != null) {
            accounts.add(patchSetApproval.realAccountId());
        }
    }
    accounts.addAll(changeNotes.getAllPastReviewers());
    accounts.addAll(changeNotes.getPastAssignees());
    changeNotes.getAttentionSetUpdates().forEach(attentionSetUpdate -> accounts.add(attentionSetUpdate.account()));
    for (SubmitRecord submitRecord : changeNotes.getSubmitRecords()) {
        if (submitRecord.labels != null) {
            accounts.addAll(submitRecord.labels.stream().map(label -> label.appliedBy).filter(Objects::nonNull).collect(Collectors.toSet()));
        }
    }
    for (HumanComment comment : changeNotes.getHumanComments().values()) {
        if (comment.author != null) {
            accounts.add(comment.author.getId());
        }
        if (comment.getRealAuthor() != null) {
            accounts.add(comment.getRealAuthor().getId());
        }
    }
    return ImmutableSet.copyOf(accountCache.get(accounts).values());
}
Also used : Arrays(java.util.Arrays) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) FOOTER_SUBMITTED_WITH(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_SUBMITTED_WITH) Inject(com.google.inject.Inject) RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) HumanComment(com.google.gerrit.entities.HumanComment) StringUtils(org.apache.commons.lang3.StringUtils) RevWalk(org.eclipse.jgit.revwalk.RevWalk) FOOTER_ASSIGNEE(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_ASSIGNEE) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) OutputFormat(com.google.gerrit.json.OutputFormat) Matcher(java.util.regex.Matcher) FooterLine(org.eclipse.jgit.revwalk.FooterLine) Gson(com.google.gson.Gson) Map(java.util.Map) RefNames(com.google.gerrit.entities.RefNames) RawText(org.eclipse.jgit.diff.RawText) CommitMessageRange(com.google.gerrit.server.notedb.ChangeNoteUtil.CommitMessageRange) ACCOUNT_TEMPLATE_REGEX(com.google.gerrit.server.util.AccountTemplateUtil.ACCOUNT_TEMPLATE_REGEX) RefUpdateUtil(com.google.gerrit.git.RefUpdateUtil) Splitter(com.google.common.base.Splitter) FOOTER_ATTENTION(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_ATTENTION) ImmutableSet(com.google.common.collect.ImmutableSet) EditList(org.eclipse.jgit.diff.EditList) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) RawParseUtils(org.eclipse.jgit.util.RawParseUtils) Account(com.google.gerrit.entities.Account) Set(java.util.Set) RevSort(org.eclipse.jgit.revwalk.RevSort) Constants(org.eclipse.jgit.lib.Constants) SubmitRecord(com.google.gerrit.entities.SubmitRecord) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Serializable(java.io.Serializable) Objects(java.util.Objects) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) Stream(java.util.stream.Stream) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) Ref(org.eclipse.jgit.lib.Ref) AutoValue(com.google.auto.value.AutoValue) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) FOOTER_LABEL(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_LABEL) Pattern(java.util.regex.Pattern) FluentLogger(com.google.common.flogger.FluentLogger) ChangeMessagesUtil(com.google.gerrit.server.ChangeMessagesUtil) Singleton(com.google.inject.Singleton) AccountCache(com.google.gerrit.server.account.AccountCache) HistogramDiff(org.eclipse.jgit.diff.HistogramDiff) Iterables(com.google.common.collect.Iterables) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FOOTER_TAG(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_TAG) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) FOOTER_REAL_USER(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_REAL_USER) Charset(java.nio.charset.Charset) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) UsedAt(com.google.gerrit.common.UsedAt) Change(com.google.gerrit.entities.Change) AttentionStatusInNoteDb(com.google.gerrit.server.notedb.ChangeNoteUtil.AttentionStatusInNoteDb) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) IOException(java.io.IOException) DiffAlgorithm(org.eclipse.jgit.diff.DiffAlgorithm) ObjectId(org.eclipse.jgit.lib.ObjectId) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) AccountTemplateUtil(com.google.gerrit.server.util.AccountTemplateUtil) PackInserter(org.eclipse.jgit.internal.storage.file.PackInserter) Project(com.google.gerrit.entities.Project) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AccountState(com.google.gerrit.server.account.AccountState) ACCOUNT_TEMPLATE_PATTERN(com.google.gerrit.server.util.AccountTemplateUtil.ACCOUNT_TEMPLATE_PATTERN) Repository(org.eclipse.jgit.lib.Repository) SubmitRecord(com.google.gerrit.entities.SubmitRecord) Objects(java.util.Objects) ObjectId(org.eclipse.jgit.lib.ObjectId) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) HumanComment(com.google.gerrit.entities.HumanComment) HashSet(java.util.HashSet)

Example 13 with SubmitRecord

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

the class DefaultSubmitRule method evaluate.

@Override
public Optional<SubmitRecord> evaluate(ChangeData cd) {
    SubmitRecord submitRecord = new SubmitRecord();
    submitRecord.status = SubmitRecord.Status.OK;
    List<LabelType> labelTypes = cd.getLabelTypes().getLabelTypes();
    List<PatchSetApproval> approvals = cd.currentApprovals();
    submitRecord.labels = new ArrayList<>(labelTypes.size());
    for (LabelType t : labelTypes) {
        LabelFunction labelFunction = t.getFunction();
        checkState(labelFunction != null, "Unable to find the LabelFunction for label %s, change %s", t.getName(), cd.getId());
        Collection<PatchSetApproval> approvalsForLabel = getApprovalsForLabel(approvals, t);
        SubmitRecord.Label label = labelFunction.check(t, approvalsForLabel);
        submitRecord.labels.add(label);
        switch(label.status) {
            case OK:
            case MAY:
                break;
            case NEED:
            case REJECT:
            case IMPOSSIBLE:
                submitRecord.status = SubmitRecord.Status.NOT_READY;
                break;
        }
    }
    return Optional.of(submitRecord);
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) LabelType(com.google.gerrit.entities.LabelType) LabelFunction(com.google.gerrit.entities.LabelFunction) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 14 with SubmitRecord

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

the class IgnoreSelfApprovalRule method evaluate.

@Override
public Optional<SubmitRecord> evaluate(ChangeData cd) {
    List<LabelType> labelTypes = cd.getLabelTypes().getLabelTypes();
    List<PatchSetApproval> approvals = cd.currentApprovals();
    boolean shouldIgnoreSelfApproval = labelTypes.stream().anyMatch(LabelType::isIgnoreSelfApproval);
    if (!shouldIgnoreSelfApproval) {
        // Shortcut to avoid further processing if no label should ignore uploader approvals
        return Optional.empty();
    }
    Account.Id uploader = cd.currentPatchSet().uploader();
    SubmitRecord submitRecord = new SubmitRecord();
    submitRecord.status = SubmitRecord.Status.OK;
    submitRecord.labels = new ArrayList<>(labelTypes.size());
    submitRecord.requirements = new ArrayList<>();
    for (LabelType t : labelTypes) {
        if (!t.isIgnoreSelfApproval()) {
            // The default rules are enough in this case.
            continue;
        }
        LabelFunction labelFunction = t.getFunction();
        if (labelFunction == null) {
            continue;
        }
        Collection<PatchSetApproval> allApprovalsForLabel = filterApprovalsByLabel(approvals, t);
        SubmitRecord.Label allApprovalsCheckResult = labelFunction.check(t, allApprovalsForLabel);
        SubmitRecord.Label ignoreSelfApprovalCheckResult = labelFunction.check(t, filterOutPositiveApprovalsOfUser(allApprovalsForLabel, uploader));
        if (labelCheckPassed(allApprovalsCheckResult) && !labelCheckPassed(ignoreSelfApprovalCheckResult)) {
            // The label has a valid approval from the uploader and no other valid approval. Set the
            // label
            // to NOT_READY and indicate the need for non-uploader approval as requirement.
            submitRecord.labels.add(ignoreSelfApprovalCheckResult);
            submitRecord.status = SubmitRecord.Status.NOT_READY;
            // Add an additional requirement to be more descriptive on why the label counts as not
            // approved.
            submitRecord.requirements.add(LegacySubmitRequirement.builder().setFallbackText("Approval from non-uploader required").setType("non_uploader_approval").build());
        }
    }
    if (submitRecord.labels.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(submitRecord);
}
Also used : Account(com.google.gerrit.entities.Account) SubmitRecord(com.google.gerrit.entities.SubmitRecord) LabelType(com.google.gerrit.entities.LabelType) LabelFunction(com.google.gerrit.entities.LabelFunction) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 15 with SubmitRecord

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

the class PrologRuleEvaluator method evaluate.

/**
 * Evaluate the submit rules.
 *
 * @return {@link SubmitRecord} returned from the evaluated rules. Can include errors.
 */
public SubmitRecord evaluate() {
    Change change;
    try {
        change = cd.change();
        if (change == null) {
            throw new StorageException("No change found");
        }
        if (projectState == null) {
            throw new NoSuchProjectException(cd.project());
        }
    } catch (StorageException | NoSuchProjectException e) {
        return ruleError("Error looking up change " + cd.getId(), e);
    }
    logger.atFine().log("input approvals: %s", cd.approvals());
    List<Term> results;
    try {
        results = evaluateImpl("locate_submit_rule", "can_submit", "locate_submit_filter", "filter_submit_results");
    } catch (RuleEvalException e) {
        return ruleError(e.getMessage(), e);
    }
    if (results.isEmpty()) {
        // whether or not that is actually possible given the permissions.
        return ruleError(String.format("Submit rule '%s' for change %s of %s has no solution.", getSubmitRuleName(), cd.getId(), projectState.getName()));
    }
    SubmitRecord submitRecord = resultsToSubmitRecord(getSubmitRule(), results);
    logger.atFine().log("submit record: %s", submitRecord);
    return submitRecord;
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) RuleEvalException(com.google.gerrit.server.project.RuleEvalException) Change(com.google.gerrit.entities.Change) 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) StorageException(com.google.gerrit.exceptions.StorageException)

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