Search in sources :

Example 1 with LabelType

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

the class PatchSetUtil method isPatchSetLocked.

/**
 * Is the current patch set locked against state changes?
 */
public boolean isPatchSetLocked(ChangeNotes notes) {
    Change change = notes.getChange();
    if (change.isMerged()) {
        return false;
    }
    ProjectState projectState = projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName()));
    ApprovalsUtil approvalsUtil = approvalsUtilProvider.get();
    for (PatchSetApproval ap : approvalsUtil.byPatchSet(notes, change.currentPatchSetId())) {
        Optional<LabelType> type = projectState.getLabelTypes(notes).byLabel(ap.label());
        if (type.isPresent() && ap.value() == 1 && type.get().getFunction() == LabelFunction.PATCH_SET_LOCK) {
            return true;
        }
    }
    return false;
}
Also used : LabelType(com.google.gerrit.entities.LabelType) ApprovalsUtil(com.google.gerrit.server.approval.ApprovalsUtil) ProjectState(com.google.gerrit.server.project.ProjectState) Change(com.google.gerrit.entities.Change) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 2 with LabelType

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

the class ApprovalsUtil method addApprovalsForNewPatchSet.

/**
 * Adds approvals to ChangeUpdate for a new patch set, and writes to NoteDb.
 *
 * @param update change update.
 * @param labelTypes label types for the containing project.
 * @param ps patch set being approved.
 * @param user user adding approvals.
 * @param approvals approvals to add.
 */
public Iterable<PatchSetApproval> addApprovalsForNewPatchSet(ChangeUpdate update, LabelTypes labelTypes, PatchSet ps, CurrentUser user, Map<String, Short> approvals) throws RestApiException, PermissionBackendException {
    Account.Id accountId = user.getAccountId();
    checkArgument(accountId.equals(ps.uploader()), "expected user %s to match patch set uploader %s", accountId, ps.uploader());
    if (approvals.isEmpty()) {
        return ImmutableList.of();
    }
    checkApprovals(approvals, permissionBackend.user(user).change(update.getNotes()));
    List<PatchSetApproval> cells = new ArrayList<>(approvals.size());
    Instant ts = update.getWhen();
    for (Map.Entry<String, Short> vote : approvals.entrySet()) {
        Optional<LabelType> lt = labelTypes.byLabel(vote.getKey());
        if (!lt.isPresent()) {
            throw new BadRequestException(String.format("label \"%s\" is not a configured label", vote.getKey()));
        }
        cells.add(newApproval(ps.id(), user, lt.get().getLabelId(), vote.getValue(), ts).build());
    }
    for (PatchSetApproval psa : cells) {
        update.putApproval(psa.label(), psa.value());
    }
    return cells;
}
Also used : Account(com.google.gerrit.entities.Account) Instant(java.time.Instant) ArrayList(java.util.ArrayList) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) LabelType(com.google.gerrit.entities.LabelType) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Map(java.util.Map)

Example 3 with LabelType

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

the class SubmitRequirementsAdapter method getLegacyRequirements.

/**
 * Retrieve legacy submit records (created by label functions and other {@link
 * com.google.gerrit.server.rules.SubmitRule}s) and convert them to submit requirement results.
 */
public static Map<SubmitRequirement, SubmitRequirementResult> getLegacyRequirements(ChangeData cd) {
    // We use SubmitRuleOptions.defaults() which does not recompute submit rules for closed changes.
    // This doesn't have an effect since we never call this class (i.e. to evaluate submit
    // requirements) for closed changes.
    List<SubmitRecord> records = cd.submitRecords(SubmitRuleOptions.defaults());
    boolean areForced = records.stream().anyMatch(record -> SubmitRecord.Status.FORCED.equals(record.status));
    List<LabelType> labelTypes = cd.getLabelTypes().getLabelTypes();
    ObjectId commitId = cd.currentPatchSet().commitId();
    Map<String, List<SubmitRequirementResult>> srsByName = records.stream().filter(r -> !SubmitRecord.Status.FORCED.equals(r.status)).map(r -> createResult(r, labelTypes, commitId, areForced)).flatMap(List::stream).collect(Collectors.groupingBy(sr -> sr.submitRequirement().name()));
    // We convert submit records to submit requirements by generating a separate
    // submit requirement result for each available label in each submit record.
    // The SR status is derived from the label status of the submit record.
    // This conversion might result in duplicate entries.
    // One such example can be a prolog rule emitting the same label name twice.
    // Another case might happen if two different submit rules emit the same label
    // name. In such cases, we need to merge these entries and return a single submit
    // requirement result. If both entries agree in their status, return any of them.
    // Otherwise, favour the entry that is blocking submission.
    ImmutableMap.Builder<SubmitRequirement, SubmitRequirementResult> result = ImmutableMap.builder();
    for (Map.Entry<String, List<SubmitRequirementResult>> entry : srsByName.entrySet()) {
        if (entry.getValue().size() == 1) {
            SubmitRequirementResult srResult = entry.getValue().iterator().next();
            result.put(srResult.submitRequirement(), srResult);
            continue;
        }
        // If all submit requirements with the same name match in status, return the first one.
        List<SubmitRequirementResult> resultsSameName = entry.getValue();
        boolean allNonBlocking = resultsSameName.stream().allMatch(sr -> sr.fulfilled());
        if (allNonBlocking) {
            result.put(resultsSameName.get(0).submitRequirement(), resultsSameName.get(0));
        } else {
            // Otherwise, return the first submit requirement result that is blocking submission.
            Optional<SubmitRequirementResult> nonFulfilled = resultsSameName.stream().filter(sr -> !sr.fulfilled()).findFirst();
            if (nonFulfilled.isPresent()) {
                result.put(nonFulfilled.get().submitRequirement(), nonFulfilled.get());
            }
        }
    }
    return result.build();
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) SubmitRequirementExpression(com.google.gerrit.entities.SubmitRequirementExpression) SubmitRecord(com.google.gerrit.entities.SubmitRecord) DefaultSubmitRule(com.google.gerrit.server.rules.DefaultSubmitRule) Collectors(java.util.stream.Collectors) ObjectId(org.eclipse.jgit.lib.ObjectId) SubmitRequirementExpressionResult(com.google.gerrit.entities.SubmitRequirementExpressionResult) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult) Strings(com.google.common.base.Strings) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) Label(com.google.gerrit.entities.SubmitRecord.Label) ImmutableList(com.google.common.collect.ImmutableList) LabelType(com.google.gerrit.entities.LabelType) ChangeQueryBuilder(com.google.gerrit.server.query.change.ChangeQueryBuilder) Map(java.util.Map) Optional(java.util.Optional) FluentLogger(com.google.common.flogger.FluentLogger) Status(com.google.gerrit.entities.SubmitRequirementExpressionResult.Status) SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) ObjectId(org.eclipse.jgit.lib.ObjectId) ImmutableMap(com.google.common.collect.ImmutableMap) SubmitRecord(com.google.gerrit.entities.SubmitRecord) SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) LabelType(com.google.gerrit.entities.LabelType) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 4 with LabelType

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

the class SubmitRequirementsAdapter method createFromDefaultSubmitRecord.

private static List<SubmitRequirementResult> createFromDefaultSubmitRecord(List<Label> labels, List<LabelType> labelTypes, ObjectId psCommitId, boolean isForced) {
    ImmutableList.Builder<SubmitRequirementResult> result = ImmutableList.builder();
    for (Label label : labels) {
        if (skipSubmitRequirementFor(label)) {
            continue;
        }
        Optional<LabelType> maybeLabelType = getLabelType(labelTypes, label.label);
        if (!maybeLabelType.isPresent()) {
            // if it was blocking or not, hence we skip the label.
            continue;
        }
        LabelType labelType = maybeLabelType.get();
        if (!isBlocking(labelType)) {
            continue;
        }
        ImmutableList<String> atoms = toExpressionAtomList(labelType);
        SubmitRequirement.Builder req = SubmitRequirement.builder().setName(label.label).setSubmittabilityExpression(toExpression(atoms)).setAllowOverrideInChildProjects(labelType.isCanOverride());
        result.add(SubmitRequirementResult.builder().legacy(Optional.of(true)).submitRequirement(req.build()).submittabilityExpressionResult(createExpressionResult(toExpression(atoms), mapStatus(label), atoms)).patchSetCommitId(psCommitId).forced(Optional.of(isForced)).build());
    }
    return result.build();
}
Also used : SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) ImmutableList(com.google.common.collect.ImmutableList) LabelType(com.google.gerrit.entities.LabelType) Label(com.google.gerrit.entities.SubmitRecord.Label) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult)

Example 5 with LabelType

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

the class MagicLabelPredicate method match.

@Override
public boolean match(ChangeData changeData) {
    Change change = changeData.change();
    if (change == null) {
        // 
        return false;
    }
    Optional<ProjectState> project = args.projectCache.get(change.getDest().project());
    if (!project.isPresent()) {
        // 
        return false;
    }
    LabelType labelType = type(project.get().getLabelTypes(), magicLabelVote.label());
    if (labelType == null) {
        // Label is not defined by this project.
        return false;
    }
    switch(magicLabelVote.value()) {
        case ANY:
            return matchAny(changeData, labelType);
        case MIN:
            return matchNumeric(changeData, magicLabelVote.label(), labelType.getMin().getValue());
        case MAX:
            return matchNumeric(changeData, magicLabelVote.label(), labelType.getMax().getValue());
    }
    throw new IllegalStateException("Unsupported magic label value: " + magicLabelVote.value());
}
Also used : LabelType(com.google.gerrit.entities.LabelType) ProjectState(com.google.gerrit.server.project.ProjectState) Change(com.google.gerrit.entities.Change)

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