Search in sources :

Example 41 with LabelType

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

the class ProjectState method getLabelTypes.

/**
 * All available label types.
 */
public LabelTypes getLabelTypes() {
    Map<String, LabelType> types = new LinkedHashMap<>();
    for (ProjectState s : treeInOrder()) {
        for (LabelType type : s.getConfig().getLabelSections().values()) {
            String lower = type.getName().toLowerCase();
            LabelType old = types.get(lower);
            if (old == null || old.isCanOverride()) {
                types.put(lower, type);
            }
        }
    }
    List<LabelType> all = Lists.newArrayListWithCapacity(types.size());
    for (LabelType type : types.values()) {
        if (!type.getValues().isEmpty()) {
            all.add(type);
        }
    }
    return new LabelTypes(Collections.unmodifiableList(all));
}
Also used : LabelTypes(com.google.gerrit.entities.LabelTypes) LabelType(com.google.gerrit.entities.LabelType) LinkedHashMap(java.util.LinkedHashMap)

Example 42 with LabelType

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

the class ProjectState method getLabelTypes.

/**
 * All available label types for this branch.
 */
public LabelTypes getLabelTypes(BranchNameKey destination) {
    List<LabelType> all = getLabelTypes().getLabelTypes();
    List<LabelType> r = Lists.newArrayListWithCapacity(all.size());
    for (LabelType l : all) {
        List<String> refs = l.getRefPatterns();
        if (refs == null) {
            r.add(l);
        } else {
            for (String refPattern : refs) {
                if (refPattern.contains("${")) {
                    logger.atWarning().log("Ref pattern for label %s in project %s contains illegal expanded parameters: %s." + " Ref pattern will be ignored.", l, getName(), refPattern);
                    continue;
                }
                if (AccessSection.isValidRefSectionName(refPattern) && match(destination, refPattern)) {
                    r.add(l);
                    break;
                }
            }
        }
    }
    return new LabelTypes(r);
}
Also used : LabelTypes(com.google.gerrit.entities.LabelTypes) LabelType(com.google.gerrit.entities.LabelType)

Example 43 with LabelType

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

the class SetLabel method updateLabel.

/**
 * Updates the given label.
 *
 * @param config the project config
 * @param labelType the label type that should be updated
 * @param input the input that describes the label update
 * @return whether the label type was modified
 * @throws BadRequestException if there was invalid data in the input
 * @throws ResourceConflictException if the update cannot be applied due to a conflict
 */
public boolean updateLabel(ProjectConfig config, LabelType labelType, LabelDefinitionInput input) throws BadRequestException, ResourceConflictException {
    boolean dirty = false;
    LabelType.Builder labelTypeBuilder = labelType.toBuilder();
    if (input.name != null) {
        String newName = input.name.trim();
        if (newName.isEmpty()) {
            throw new BadRequestException("name cannot be empty");
        }
        if (!newName.equals(labelType.getName())) {
            if (config.getLabelSections().containsKey(newName)) {
                throw new ResourceConflictException(String.format("name %s already in use", newName));
            }
            for (String labelName : config.getLabelSections().keySet()) {
                if (labelName.equalsIgnoreCase(newName)) {
                    throw new ResourceConflictException(String.format("name %s conflicts with existing label %s", newName, labelName));
                }
            }
            try {
                LabelType.checkName(newName);
            } catch (IllegalArgumentException e) {
                throw new BadRequestException("invalid name: " + input.name, e);
            }
            labelTypeBuilder.setName(newName);
            dirty = true;
        }
    }
    if (input.description != null) {
        String description = Strings.emptyToNull(input.description.trim());
        labelTypeBuilder.setDescription(Optional.ofNullable(description));
        dirty = true;
    }
    if (input.function != null) {
        if (input.function.trim().isEmpty()) {
            throw new BadRequestException("function cannot be empty");
        }
        labelTypeBuilder.setFunction(LabelDefinitionInputParser.parseFunction(input.function));
        dirty = true;
    }
    if (input.values != null) {
        if (input.values.isEmpty()) {
            throw new BadRequestException("values cannot be empty");
        }
        labelTypeBuilder.setValues(LabelDefinitionInputParser.parseValues(input.values));
        dirty = true;
    }
    if (input.defaultValue != null) {
        labelTypeBuilder.setDefaultValue(LabelDefinitionInputParser.parseDefaultValue(labelTypeBuilder, input.defaultValue));
        dirty = true;
    }
    if (input.branches != null) {
        labelTypeBuilder.setRefPatterns(LabelDefinitionInputParser.parseBranches(input.branches));
        dirty = true;
    }
    if (input.canOverride != null) {
        labelTypeBuilder.setCanOverride(input.canOverride);
        dirty = true;
    }
    input.copyCondition = Strings.emptyToNull(input.copyCondition);
    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);
        }
        labelTypeBuilder.setCopyCondition(input.copyCondition);
        dirty = true;
        if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
            throw new BadRequestException("can't set and unset copyCondition in the same request");
        }
    }
    if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
        labelTypeBuilder.setCopyCondition(null);
        dirty = true;
    }
    if (input.copyAnyScore != null) {
        labelTypeBuilder.setCopyAnyScore(input.copyAnyScore);
        dirty = true;
    }
    if (input.copyMinScore != null) {
        labelTypeBuilder.setCopyMinScore(input.copyMinScore);
        dirty = true;
    }
    if (input.copyMaxScore != null) {
        labelTypeBuilder.setCopyMaxScore(input.copyMaxScore);
        dirty = true;
    }
    if (input.copyAllScoresIfListOfFilesDidNotChange != null) {
        labelTypeBuilder.setCopyAllScoresIfListOfFilesDidNotChange(input.copyAllScoresIfListOfFilesDidNotChange);
        dirty = true;
    }
    if (input.copyAllScoresIfNoChange != null) {
        labelTypeBuilder.setCopyAllScoresIfNoChange(input.copyAllScoresIfNoChange);
        dirty = true;
    }
    if (input.copyAllScoresIfNoCodeChange != null) {
        labelTypeBuilder.setCopyAllScoresIfNoCodeChange(input.copyAllScoresIfNoCodeChange);
        dirty = true;
    }
    if (input.copyAllScoresOnTrivialRebase != null) {
        labelTypeBuilder.setCopyAllScoresOnTrivialRebase(input.copyAllScoresOnTrivialRebase);
        dirty = true;
    }
    if (input.copyAllScoresOnMergeFirstParentUpdate != null) {
        labelTypeBuilder.setCopyAllScoresOnMergeFirstParentUpdate(input.copyAllScoresOnMergeFirstParentUpdate);
        dirty = true;
    }
    if (input.copyValues != null) {
        labelTypeBuilder.setCopyValues(input.copyValues);
        dirty = true;
    }
    if (input.allowPostSubmit != null) {
        labelTypeBuilder.setAllowPostSubmit(input.allowPostSubmit);
        dirty = true;
    }
    if (input.ignoreSelfApproval != null) {
        labelTypeBuilder.setIgnoreSelfApproval(input.ignoreSelfApproval);
        dirty = true;
    }
    config.getLabelSections().remove(labelType.getName());
    config.upsertLabelType(labelTypeBuilder.build());
    return dirty;
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) LabelType(com.google.gerrit.entities.LabelType) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 44 with LabelType

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

the class ApprovalInference method getForPatchSetWithoutNormalization.

private Collection<PatchSetApproval> getForPatchSetWithoutNormalization(ChangeNotes notes, ProjectState project, PatchSet patchSet, RevWalk rw, Config repoConfig) {
    checkState(project.getNameKey().equals(notes.getProjectName()), "project must match %s, %s", project.getNameKey(), notes.getProjectName());
    PatchSet.Id psId = patchSet.id();
    // Add approvals on the given patch set to the result
    Table<String, Account.Id, PatchSetApproval> resultByUser = HashBasedTable.create();
    ImmutableList<PatchSetApproval> nonCopiedApprovalsForGivenPatchSet = notes.load().getApprovals().get(patchSet.id());
    nonCopiedApprovalsForGivenPatchSet.forEach(psa -> resultByUser.put(psa.label(), psa.accountId(), psa));
    // given patch set.
    if (psId.get() == 1) {
        return resultByUser.values();
    }
    Map.Entry<PatchSet.Id, PatchSet> priorPatchSet = notes.load().getPatchSets().lowerEntry(psId);
    if (priorPatchSet == null) {
        return resultByUser.values();
    }
    ImmutableList<PatchSetApproval> priorApprovalsIncludingCopied = notes.load().getApprovalsWithCopied().get(priorPatchSet.getKey());
    // Add labels from the previous patch set to the result in case the label isn't already there
    // and settings as well as change kind allow copying.
    ChangeKind changeKind = changeKindCache.getChangeKind(project.getNameKey(), rw, repoConfig, priorPatchSet.getValue().commitId(), patchSet.commitId());
    logger.atFine().log("change kind for patch set %d of change %d against prior patch set %s is %s", patchSet.id().get(), patchSet.id().changeId().get(), priorPatchSet.getValue().id().changeId(), changeKind);
    Map<String, ModifiedFile> baseVsCurrent = null;
    Map<String, ModifiedFile> baseVsPrior = null;
    Map<String, ModifiedFile> priorVsCurrent = null;
    LabelTypes labelTypes = project.getLabelTypes();
    for (PatchSetApproval psa : priorApprovalsIncludingCopied) {
        if (resultByUser.contains(psa.label(), psa.accountId())) {
            continue;
        }
        Optional<LabelType> type = labelTypes.byLabel(psa.labelId());
        // Only compute modified files if there is a relevant label, since this is expensive.
        if (baseVsCurrent == null && type.isPresent() && type.get().isCopyAllScoresIfListOfFilesDidNotChange()) {
            baseVsCurrent = listModifiedFiles(project, patchSet, rw, repoConfig);
            baseVsPrior = listModifiedFiles(project, priorPatchSet.getValue(), rw, repoConfig);
            priorVsCurrent = listModifiedFiles(project, priorPatchSet.getValue().commitId(), patchSet.commitId(), rw, repoConfig);
        }
        if (!type.isPresent()) {
            logger.atFine().log("approval %d on label %s of patch set %d of change %d cannot be copied" + " to patch set %d because the label no longer exists on project %s", psa.value(), psa.label(), psa.key().patchSetId().get(), psa.key().patchSetId().changeId().get(), psId.get(), project.getName());
            continue;
        }
        if (!canCopyBasedOnBooleanLabelConfigs(project, psa, patchSet.id(), changeKind, type.get(), baseVsCurrent, baseVsPrior, priorVsCurrent) && !canCopyBasedOnCopyCondition(notes, psa, patchSet, type.get(), changeKind, rw, repoConfig)) {
            continue;
        }
        resultByUser.put(psa.label(), psa.accountId(), psa.copyWithPatchSet(patchSet.id()));
    }
    return resultByUser.values();
}
Also used : LabelTypes(com.google.gerrit.entities.LabelTypes) PatchSet(com.google.gerrit.entities.PatchSet) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) LabelType(com.google.gerrit.entities.LabelType) ModifiedFile(com.google.gerrit.server.patch.gitdiff.ModifiedFile) ObjectId(org.eclipse.jgit.lib.ObjectId) Map(java.util.Map) ChangeKind(com.google.gerrit.extensions.client.ChangeKind)

Example 45 with LabelType

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

the class ApprovalsUtil method addReviewers.

private List<PatchSetApproval> addReviewers(ChangeUpdate update, LabelTypes labelTypes, Change change, PatchSet.Id psId, Account.Id authorId, Account.Id committerId, Iterable<Account.Id> wantReviewers, Collection<Account.Id> existingReviewers) {
    List<LabelType> allTypes = labelTypes.getLabelTypes();
    if (allTypes.isEmpty()) {
        return ImmutableList.of();
    }
    Set<Account.Id> need = Sets.newLinkedHashSet(wantReviewers);
    if (authorId != null && canSee(update.getNotes(), authorId)) {
        need.add(authorId);
    }
    if (committerId != null && canSee(update.getNotes(), committerId)) {
        need.add(committerId);
    }
    need.remove(change.getOwner());
    need.removeAll(existingReviewers);
    if (need.isEmpty()) {
        return ImmutableList.of();
    }
    List<PatchSetApproval> cells = Lists.newArrayListWithCapacity(need.size());
    LabelId labelId = Iterables.getLast(allTypes).getLabelId();
    for (Account.Id account : need) {
        cells.add(PatchSetApproval.builder().key(PatchSetApproval.key(psId, account, labelId)).value(0).granted(update.getWhen()).build());
        update.putReviewer(account, REVIEWER);
    }
    return Collections.unmodifiableList(cells);
}
Also used : Account(com.google.gerrit.entities.Account) LabelType(com.google.gerrit.entities.LabelType) LabelId(com.google.gerrit.entities.LabelId) LabelId(com.google.gerrit.entities.LabelId) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

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