Search in sources :

Example 1 with LabelFunction

use of com.google.gerrit.entities.LabelFunction 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 2 with LabelFunction

use of com.google.gerrit.entities.LabelFunction 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 3 with LabelFunction

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

the class ProjectConfig method loadLabelSections.

private void loadLabelSections(Config rc) {
    Map<String, String> lowerNames = Maps.newHashMapWithExpectedSize(2);
    labelSections = new LinkedHashMap<>();
    for (String name : rc.getSubsections(LABEL)) {
        String lower = name.toLowerCase();
        if (lowerNames.containsKey(lower)) {
            error(String.format("Label \"%s\" conflicts with \"%s\"", name, lowerNames.get(lower)));
        }
        lowerNames.put(lower, name);
        List<LabelValue> values = new ArrayList<>();
        Set<Short> allValues = new HashSet<>();
        for (String value : rc.getStringList(LABEL, name, KEY_VALUE)) {
            try {
                LabelValue labelValue = parseLabelValue(value);
                if (allValues.add(labelValue.getValue())) {
                    values.add(labelValue);
                } else {
                    error(String.format("Duplicate %s \"%s\" for label \"%s\"", KEY_VALUE, value, name));
                }
            } catch (IllegalArgumentException notValue) {
                error(String.format("Invalid %s \"%s\" for label \"%s\": %s", KEY_VALUE, value, name, notValue.getMessage()));
            }
        }
        LabelType.Builder label;
        try {
            label = LabelType.builder(name, values);
        } catch (IllegalArgumentException badName) {
            error(String.format("Invalid label \"%s\"", name));
            continue;
        }
        label.setDescription(Optional.ofNullable(rc.getString(LABEL, name, KEY_LABEL_DESCRIPTION)));
        String functionName = rc.getString(LABEL, name, KEY_FUNCTION);
        Optional<LabelFunction> function = functionName != null ? LabelFunction.parse(functionName) : Optional.of(LabelFunction.MAX_WITH_BLOCK);
        if (!function.isPresent()) {
            error(String.format("Invalid %s for label \"%s\". Valid names are: %s", KEY_FUNCTION, name, Joiner.on(", ").join(LabelFunction.ALL.keySet())));
        }
        label.setFunction(function.orElse(null));
        label.setCopyCondition(rc.getString(LABEL, name, KEY_COPY_CONDITION));
        if (!values.isEmpty()) {
            short dv = (short) rc.getInt(LABEL, name, KEY_DEFAULT_VALUE, 0);
            if (isInRange(dv, values)) {
                label.setDefaultValue(dv);
            } else {
                error(String.format("Invalid %s \"%s\" for label \"%s\"", KEY_DEFAULT_VALUE, dv, name));
            }
        }
        label.setAllowPostSubmit(rc.getBoolean(LABEL, name, KEY_ALLOW_POST_SUBMIT, LabelType.DEF_ALLOW_POST_SUBMIT));
        label.setIgnoreSelfApproval(rc.getBoolean(LABEL, name, KEY_IGNORE_SELF_APPROVAL, LabelType.DEF_IGNORE_SELF_APPROVAL));
        label.setCopyAnyScore(rc.getBoolean(LABEL, name, KEY_COPY_ANY_SCORE, LabelType.DEF_COPY_ANY_SCORE));
        label.setCopyMinScore(rc.getBoolean(LABEL, name, KEY_COPY_MIN_SCORE, LabelType.DEF_COPY_MIN_SCORE));
        label.setCopyMaxScore(rc.getBoolean(LABEL, name, KEY_COPY_MAX_SCORE, LabelType.DEF_COPY_MAX_SCORE));
        label.setCopyAllScoresIfListOfFilesDidNotChange(rc.getBoolean(LABEL, name, KEY_COPY_ALL_SCORES_IF_LIST_OF_FILES_DID_NOT_CHANGE, LabelType.DEF_COPY_ALL_SCORES_IF_LIST_OF_FILES_DID_NOT_CHANGE));
        label.setCopyAllScoresOnMergeFirstParentUpdate(rc.getBoolean(LABEL, name, KEY_COPY_ALL_SCORES_ON_MERGE_FIRST_PARENT_UPDATE, LabelType.DEF_COPY_ALL_SCORES_ON_MERGE_FIRST_PARENT_UPDATE));
        label.setCopyAllScoresOnTrivialRebase(rc.getBoolean(LABEL, name, KEY_COPY_ALL_SCORES_ON_TRIVIAL_REBASE, LabelType.DEF_COPY_ALL_SCORES_ON_TRIVIAL_REBASE));
        label.setCopyAllScoresIfNoCodeChange(rc.getBoolean(LABEL, name, KEY_COPY_ALL_SCORES_IF_NO_CODE_CHANGE, LabelType.DEF_COPY_ALL_SCORES_IF_NO_CODE_CHANGE));
        label.setCopyAllScoresIfNoChange(rc.getBoolean(LABEL, name, KEY_COPY_ALL_SCORES_IF_NO_CHANGE, LabelType.DEF_COPY_ALL_SCORES_IF_NO_CHANGE));
        Set<Short> copyValues = new HashSet<>();
        for (String value : rc.getStringList(LABEL, name, KEY_COPY_VALUE)) {
            try {
                short copyValue = Shorts.checkedCast(PermissionRule.parseInt(value));
                if (!copyValues.add(copyValue)) {
                    error(String.format("Duplicate %s \"%s\" for label \"%s\"", KEY_COPY_VALUE, value, name));
                }
            } catch (IllegalArgumentException notValue) {
                error(String.format("Invalid %s \"%s\" for label \"%s\": %s", KEY_COPY_VALUE, value, name, notValue.getMessage()));
            }
        }
        label.setCopyValues(copyValues);
        label.setCanOverride(rc.getBoolean(LABEL, name, KEY_CAN_OVERRIDE, LabelType.DEF_CAN_OVERRIDE));
        List<String> refPatterns = getStringListOrNull(rc, LABEL, name, KEY_BRANCH);
        if (refPatterns == null) {
            label.setRefPatterns(null);
        } else {
            for (String pattern : refPatterns) {
                if (pattern.startsWith("^")) {
                    try {
                        Pattern.compile(pattern);
                    } catch (PatternSyntaxException e) {
                        error(String.format("Invalid ref pattern \"%s\" in %s.%s.%s: %s", pattern, LABEL, name, KEY_BRANCH, e.getMessage()));
                    }
                }
            }
            label.setRefPatterns(ImmutableList.copyOf(refPatterns));
        }
        labelSections.put(name, label.build());
    }
}
Also used : LabelValue(com.google.gerrit.entities.LabelValue) ArrayList(java.util.ArrayList) LabelFunction(com.google.gerrit.entities.LabelFunction) LabelType(com.google.gerrit.entities.LabelType) HashSet(java.util.HashSet) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

LabelFunction (com.google.gerrit.entities.LabelFunction)3 LabelType (com.google.gerrit.entities.LabelType)3 PatchSetApproval (com.google.gerrit.entities.PatchSetApproval)2 SubmitRecord (com.google.gerrit.entities.SubmitRecord)2 Account (com.google.gerrit.entities.Account)1 LabelValue (com.google.gerrit.entities.LabelValue)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1