Search in sources :

Example 16 with LabelType

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

the class ProjectConfig method saveLabelSections.

private void saveLabelSections(Config rc) {
    List<String> existing = new ArrayList<>(rc.getSubsections(LABEL));
    if (!new ArrayList<>(labelSections.keySet()).equals(existing)) {
        // Order of sections changed, remove and rewrite them all.
        unsetSection(rc, LABEL);
    }
    Set<String> toUnset = new HashSet<>(existing);
    for (Map.Entry<String, LabelType> e : labelSections.entrySet()) {
        String name = e.getKey();
        LabelType label = e.getValue();
        toUnset.remove(name);
        if (label.getDescription().isPresent() && !label.getDescription().get().isEmpty()) {
            rc.setString(LABEL, name, KEY_LABEL_DESCRIPTION, label.getDescription().get());
        } else {
            rc.unset(LABEL, name, KEY_LABEL_DESCRIPTION);
        }
        rc.setString(LABEL, name, KEY_FUNCTION, label.getFunction().getFunctionName());
        rc.setInt(LABEL, name, KEY_DEFAULT_VALUE, label.getDefaultValue());
        setBooleanConfigKey(rc, LABEL, name, KEY_ALLOW_POST_SUBMIT, label.isAllowPostSubmit(), LabelType.DEF_ALLOW_POST_SUBMIT);
        setBooleanConfigKey(rc, LABEL, name, KEY_IGNORE_SELF_APPROVAL, label.isIgnoreSelfApproval(), LabelType.DEF_IGNORE_SELF_APPROVAL);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ANY_SCORE, label.isCopyAnyScore(), LabelType.DEF_COPY_ANY_SCORE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_MIN_SCORE, label.isCopyMinScore(), LabelType.DEF_COPY_MIN_SCORE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_MAX_SCORE, label.isCopyMaxScore(), LabelType.DEF_COPY_MAX_SCORE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_ON_TRIVIAL_REBASE, label.isCopyAllScoresOnTrivialRebase(), LabelType.DEF_COPY_ALL_SCORES_ON_TRIVIAL_REBASE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_IF_NO_CODE_CHANGE, label.isCopyAllScoresIfNoCodeChange(), LabelType.DEF_COPY_ALL_SCORES_IF_NO_CODE_CHANGE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_IF_NO_CHANGE, label.isCopyAllScoresIfNoChange(), LabelType.DEF_COPY_ALL_SCORES_IF_NO_CHANGE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_IF_LIST_OF_FILES_DID_NOT_CHANGE, label.isCopyAllScoresIfListOfFilesDidNotChange(), LabelType.DEF_COPY_ALL_SCORES_IF_LIST_OF_FILES_DID_NOT_CHANGE);
        setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_ON_MERGE_FIRST_PARENT_UPDATE, label.isCopyAllScoresOnMergeFirstParentUpdate(), LabelType.DEF_COPY_ALL_SCORES_ON_MERGE_FIRST_PARENT_UPDATE);
        rc.setStringList(LABEL, name, KEY_COPY_VALUE, label.getCopyValues().stream().map(LabelValue::formatValue).collect(toList()));
        setBooleanConfigKey(rc, LABEL, name, KEY_CAN_OVERRIDE, label.isCanOverride(), LabelType.DEF_CAN_OVERRIDE);
        List<String> values = new ArrayList<>(label.getValues().size());
        for (LabelValue value : label.getValues()) {
            values.add(value.format().trim());
        }
        rc.setStringList(LABEL, name, KEY_VALUE, values);
        if (label.getCopyCondition().isPresent()) {
            rc.setString(LABEL, name, KEY_COPY_CONDITION, label.getCopyCondition().get());
        } else {
            rc.unset(LABEL, name, KEY_COPY_CONDITION);
        }
        List<String> refPatterns = label.getRefPatterns();
        if (refPatterns != null && !refPatterns.isEmpty()) {
            rc.setStringList(LABEL, name, KEY_BRANCH, refPatterns);
        } else {
            rc.unset(LABEL, name, KEY_BRANCH);
        }
    }
    for (String name : toUnset) {
        rc.unsetSection(LABEL, name);
    }
}
Also used : LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet)

Example 17 with LabelType

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

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

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

the class SetLabel method apply.

@Override
public Response<LabelDefinitionInfo> apply(LabelResource rsrc, LabelDefinitionInput input) throws AuthException, BadRequestException, ResourceConflictException, PermissionBackendException, IOException, ConfigInvalidException {
    if (!user.get().isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    permissionBackend.currentUser().project(rsrc.getProject().getNameKey()).check(ProjectPermission.WRITE_CONFIG);
    if (input == null) {
        input = new LabelDefinitionInput();
    }
    LabelType labelType = rsrc.getLabelType();
    try (MetaDataUpdate md = updateFactory.create(rsrc.getProject().getNameKey())) {
        ProjectConfig config = projectConfigFactory.read(md);
        if (updateLabel(config, labelType, input)) {
            if (input.commitMessage != null) {
                md.setMessage(Strings.emptyToNull(input.commitMessage.trim()));
            } else {
                md.setMessage("Update label");
            }
            String newName = Strings.nullToEmpty(input.name).trim();
            labelType = config.getLabelSections().get(newName.isEmpty() ? labelType.getName() : newName);
            config.commit(md);
            projectCache.evictAndReindex(rsrc.getProject().getProjectState().getProject());
        }
    }
    return Response.ok(LabelDefinitionJson.format(rsrc.getProject().getNameKey(), labelType));
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) LabelType(com.google.gerrit.entities.LabelType) AuthException(com.google.gerrit.extensions.restapi.AuthException) LabelDefinitionInput(com.google.gerrit.extensions.common.LabelDefinitionInput) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 20 with LabelType

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

the class AllProjectsCreator method initDefaultAcls.

private void initDefaultAcls(ProjectConfig config, AllProjectsInput input) {
    checkArgument(input.codeReviewLabel().isPresent());
    LabelType codeReviewLabel = input.codeReviewLabel().get();
    config.upsertAccessSection(AccessSection.HEADS, heads -> {
        initDefaultAclsForRegisteredUsers(heads, codeReviewLabel, config);
    });
    config.upsertAccessSection(AccessSection.GLOBAL_CAPABILITIES, capabilities -> {
        input.serviceUsersGroup().ifPresent(batchUsersGroup -> initDefaultAclsForBatchUsers(capabilities, config, batchUsersGroup));
    });
    input.administratorsGroup().ifPresent(adminsGroup -> initDefaultAclsForAdmins(config, codeReviewLabel, adminsGroup));
}
Also used : LabelType(com.google.gerrit.entities.LabelType)

Aggregations

LabelType (com.google.gerrit.entities.LabelType)71 Test (org.junit.Test)26 PatchSetApproval (com.google.gerrit.entities.PatchSetApproval)20 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)16 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)14 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)13 Map (java.util.Map)12 LabelTypes (com.google.gerrit.entities.LabelTypes)10 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)9 Account (com.google.gerrit.entities.Account)8 LabelValue (com.google.gerrit.entities.LabelValue)8 AuthException (com.google.gerrit.extensions.restapi.AuthException)8 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)8 HashMap (java.util.HashMap)8 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)7 ArrayList (java.util.ArrayList)7 LabelPermission (com.google.gerrit.server.permissions.LabelPermission)6 ProjectState (com.google.gerrit.server.project.ProjectState)6 Change (com.google.gerrit.entities.Change)5 SubmitRecord (com.google.gerrit.entities.SubmitRecord)5