Search in sources :

Example 51 with LabelType

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

the class CreateLabel method createLabel.

/**
 * Creates a new label.
 *
 * @param config the project config
 * @param label the name of the new label
 * @param input the input that describes the new label
 * @return the created label type
 * @throws BadRequestException if there was invalid data in the input
 * @throws ResourceConflictException if the label cannot be created due to a conflict
 */
public LabelType createLabel(ProjectConfig config, String label, LabelDefinitionInput input) throws BadRequestException, ResourceConflictException {
    if (config.getLabelSections().containsKey(label)) {
        throw new ResourceConflictException(String.format("label %s already exists", label));
    }
    for (String labelName : config.getLabelSections().keySet()) {
        if (labelName.equalsIgnoreCase(label)) {
            throw new ResourceConflictException(String.format("label %s conflicts with existing label %s", label, labelName));
        }
    }
    if (input.values == null || input.values.isEmpty()) {
        throw new BadRequestException("values are required");
    }
    try {
        LabelType.checkName(label);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("invalid name: " + label, e);
    }
    List<LabelValue> values = LabelDefinitionInputParser.parseValues(input.values);
    LabelType.Builder labelType = LabelType.builder(LabelType.checkName(label), values);
    if (input.description != null) {
        String description = Strings.emptyToNull(input.description.trim());
        labelType.setDescription(Optional.ofNullable(description));
    }
    if (input.function != null && !input.function.trim().isEmpty()) {
        labelType.setFunction(LabelDefinitionInputParser.parseFunction(input.function));
    } else {
        labelType.setFunction(LabelFunction.MAX_WITH_BLOCK);
    }
    if (input.defaultValue != null) {
        labelType.setDefaultValue(LabelDefinitionInputParser.parseDefaultValue(labelType, input.defaultValue));
    }
    if (input.branches != null) {
        labelType.setRefPatterns(LabelDefinitionInputParser.parseBranches(input.branches));
    }
    if (input.canOverride != null) {
        labelType.setCanOverride(input.canOverride);
    }
    if (input.copyAnyScore != null) {
        labelType.setCopyAnyScore(input.copyAnyScore);
    }
    if (input.copyCondition != null) {
        try {
            approvalQueryBuilder.parse(input.copyCondition);
        } catch (QueryParseException e) {
            throw new BadRequestException("unable to parse copy condition. got: " + input.copyCondition + ". " + e.getMessage(), e);
        }
        if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
            throw new BadRequestException("can't set and unset copyCondition in the same request");
        }
        labelType.setCopyCondition(Strings.emptyToNull(input.copyCondition));
    }
    if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
        labelType.setCopyCondition(null);
    }
    if (input.copyMinScore != null) {
        labelType.setCopyMinScore(input.copyMinScore);
    }
    if (input.copyMaxScore != null) {
        labelType.setCopyMaxScore(input.copyMaxScore);
    }
    if (input.copyAllScoresIfListOfFilesDidNotChange != null) {
        labelType.setCopyAllScoresIfListOfFilesDidNotChange(input.copyAllScoresIfListOfFilesDidNotChange);
    }
    if (input.copyAllScoresIfNoChange != null) {
        labelType.setCopyAllScoresIfNoChange(input.copyAllScoresIfNoChange);
    }
    if (input.copyAllScoresIfNoCodeChange != null) {
        labelType.setCopyAllScoresIfNoCodeChange(input.copyAllScoresIfNoCodeChange);
    }
    if (input.copyAllScoresOnTrivialRebase != null) {
        labelType.setCopyAllScoresOnTrivialRebase(input.copyAllScoresOnTrivialRebase);
    }
    if (input.copyAllScoresOnMergeFirstParentUpdate != null) {
        labelType.setCopyAllScoresOnMergeFirstParentUpdate(input.copyAllScoresOnMergeFirstParentUpdate);
    }
    if (input.copyValues != null) {
        labelType.setCopyValues(input.copyValues);
    }
    if (input.allowPostSubmit != null) {
        labelType.setAllowPostSubmit(input.allowPostSubmit);
    }
    if (input.ignoreSelfApproval != null) {
        labelType.setIgnoreSelfApproval(input.ignoreSelfApproval);
    }
    LabelType lt = labelType.build();
    config.upsertLabelType(lt);
    return lt;
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) IdString(com.google.gerrit.extensions.restapi.IdString) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 52 with LabelType

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

the class CreateLabel method apply.

@Override
public Response<LabelDefinitionInfo> apply(ProjectResource rsrc, IdString id, LabelDefinitionInput input) throws AuthException, BadRequestException, ResourceConflictException, PermissionBackendException, IOException, ConfigInvalidException {
    if (!user.get().isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    permissionBackend.currentUser().project(rsrc.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
    if (input == null) {
        input = new LabelDefinitionInput();
    }
    if (input.name != null && !input.name.equals(id.get())) {
        throw new BadRequestException("name in input must match name in URL");
    }
    try (MetaDataUpdate md = updateFactory.create(rsrc.getNameKey())) {
        ProjectConfig config = projectConfigFactory.read(md);
        LabelType labelType = createLabel(config, id.get(), input);
        if (input.commitMessage != null) {
            md.setMessage(Strings.emptyToNull(input.commitMessage.trim()));
        } else {
            md.setMessage("Update label");
        }
        config.commit(md);
        projectCache.evictAndReindex(rsrc.getProjectState().getProject());
        return Response.created(LabelDefinitionJson.format(rsrc.getNameKey(), labelType));
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) LabelType(com.google.gerrit.entities.LabelType) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) LabelDefinitionInput(com.google.gerrit.extensions.common.LabelDefinitionInput) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 53 with LabelType

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

the class PRED__load_commit_labels_1 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.setB0();
    Term a1 = arg1.dereference();
    Term listHead = Prolog.Nil;
    ChangeData cd = StoredValues.CHANGE_DATA.get(engine);
    LabelTypes types = cd.getLabelTypes();
    for (PatchSetApproval a : cd.currentApprovals()) {
        Optional<LabelType> t = types.byLabel(a.labelId());
        if (!t.isPresent()) {
            continue;
        }
        StructureTerm labelTerm = new StructureTerm(sym_label, SymbolTerm.intern(t.get().getName()), new IntegerTerm(a.value()));
        StructureTerm userTerm = new StructureTerm(sym_user, new IntegerTerm(a.accountId().get()));
        listHead = new ListTerm(new StructureTerm(sym_commit_label, labelTerm, userTerm), listHead);
    }
    if (!a1.unify(listHead, engine.trail)) {
        return engine.fail();
    }
    return cont;
}
Also used : LabelTypes(com.google.gerrit.entities.LabelTypes) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) LabelType(com.google.gerrit.entities.LabelType) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) ChangeData(com.google.gerrit.server.query.change.ChangeData) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 54 with LabelType

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

the class EventFactory method asApprovalAttribute.

/**
 * Create an ApprovalAttribute for the given approval suitable for serialization to JSON.
 *
 * @param labelTypes label types for the containing project
 * @return object suitable for serialization to JSON
 */
public ApprovalAttribute asApprovalAttribute(PatchSetApproval approval, LabelTypes labelTypes) {
    ApprovalAttribute a = new ApprovalAttribute();
    a.type = approval.labelId().get();
    a.value = Short.toString(approval.value());
    a.by = asAccountAttribute(approval.accountId());
    a.grantedOn = approval.granted().getEpochSecond();
    a.oldValue = null;
    Optional<LabelType> lt = labelTypes.byLabel(approval.labelId());
    lt.ifPresent(l -> a.description = l.getName());
    return a;
}
Also used : LabelType(com.google.gerrit.entities.LabelType) ApprovalAttribute(com.google.gerrit.server.data.ApprovalAttribute)

Example 55 with LabelType

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

the class ReplaceOp method fireApprovalsEvent.

private void fireApprovalsEvent(PostUpdateContext ctx) {
    if (approvals.isEmpty()) {
        return;
    }
    /* For labels that are not set in this operation, show the "current" value
     * of 0, and no oldValue as the value was not modified by this operation.
     * For labels that are set in this operation, the value was modified, so
     * show a transition from an oldValue of 0 to the new value.
     */
    List<LabelType> labels = projectCache.get(ctx.getProject()).orElseThrow(illegalState(ctx.getProject())).getLabelTypes(notes).getLabelTypes();
    Map<String, Short> allApprovals = new HashMap<>();
    Map<String, Short> oldApprovals = new HashMap<>();
    for (LabelType lt : labels) {
        allApprovals.put(lt.getName(), (short) 0);
        oldApprovals.put(lt.getName(), null);
    }
    for (Map.Entry<String, Short> entry : approvals.entrySet()) {
        if (entry.getValue() != 0) {
            allApprovals.put(entry.getKey(), entry.getValue());
            oldApprovals.put(entry.getKey(), (short) 0);
        }
    }
    commentAdded.fire(ctx.getChangeData(notes), newPatchSet, ctx.getAccount(), null, allApprovals, oldApprovals, ctx.getWhen());
}
Also used : HashMap(java.util.HashMap) LabelType(com.google.gerrit.entities.LabelType) Map(java.util.Map) HashMap(java.util.HashMap)

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