Search in sources :

Example 1 with LabelTypes

use of com.google.gerrit.common.data.LabelTypes in project gerrit by GerritCodeReview.

the class ReviewerJson method format.

public ReviewerInfo format(ReviewerInfo out, PermissionBackend.ForChange perm, ChangeData cd, Iterable<PatchSetApproval> approvals) throws OrmException, PermissionBackendException {
    LabelTypes labelTypes = cd.getLabelTypes();
    // Don't use Maps.newTreeMap(Comparator) due to OpenJDK bug 100167.
    out.approvals = new TreeMap<>(labelTypes.nameComparator());
    for (PatchSetApproval ca : approvals) {
        for (PermissionRange pr : cd.changeControl().getLabelRanges()) {
            if (!pr.isEmpty()) {
                LabelType at = labelTypes.byLabel(ca.getLabelId());
                if (at != null) {
                    out.approvals.put(at.getName(), formatValue(ca.getValue()));
                }
            }
        }
    }
    // Add dummy approvals for all permitted labels for the user even if they
    // do not exist in the DB.
    PatchSet ps = cd.currentPatchSet();
    if (ps != null) {
        for (SubmitRecord rec : new SubmitRuleEvaluator(cd).setFastEvalLabels(true).setAllowDraft(true).evaluate()) {
            if (rec.labels == null) {
                continue;
            }
            for (SubmitRecord.Label label : rec.labels) {
                String name = label.label;
                LabelType type = labelTypes.byLabel(name);
                if (!out.approvals.containsKey(name) && type != null && perm.test(new LabelPermission(type))) {
                    out.approvals.put(name, formatValue((short) 0));
                }
            }
        }
    }
    if (out.approvals.isEmpty()) {
        out.approvals = null;
    }
    return out;
}
Also used : SubmitRuleEvaluator(com.google.gerrit.server.project.SubmitRuleEvaluator) SubmitRecord(com.google.gerrit.common.data.SubmitRecord) LabelTypes(com.google.gerrit.common.data.LabelTypes) PermissionRange(com.google.gerrit.common.data.PermissionRange) LabelType(com.google.gerrit.common.data.LabelType) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) LabelPermission(com.google.gerrit.server.permissions.LabelPermission)

Example 2 with LabelTypes

use of com.google.gerrit.common.data.LabelTypes in project gerrit by GerritCodeReview.

the class SchemaCreatorTest method getLabelTypes.

private LabelTypes getLabelTypes() throws Exception {
    db.create();
    ProjectConfig c = new ProjectConfig(allProjects);
    try (Repository repo = repoManager.openRepository(allProjects)) {
        c.load(repo);
        return new LabelTypes(ImmutableList.copyOf(c.getLabelSections().values()));
    }
}
Also used : ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Repository(org.eclipse.jgit.lib.Repository) LabelTypes(com.google.gerrit.common.data.LabelTypes)

Example 3 with LabelTypes

use of com.google.gerrit.common.data.LabelTypes in project gerrit by GerritCodeReview.

the class LabelNormalizer method normalize.

/**
   * @param ctl change control containing the given approvals.
   * @param approvals list of approvals.
   * @return copies of approvals normalized to the defined ranges for the label type and permissions
   *     for the user. Approvals for unknown labels are not included in the output, nor are
   *     approvals where the user has no permissions for that label.
   */
public Result normalize(ChangeControl ctl, Collection<PatchSetApproval> approvals) {
    List<PatchSetApproval> unchanged = Lists.newArrayListWithCapacity(approvals.size());
    List<PatchSetApproval> updated = Lists.newArrayListWithCapacity(approvals.size());
    List<PatchSetApproval> deleted = Lists.newArrayListWithCapacity(approvals.size());
    LabelTypes labelTypes = ctl.getLabelTypes();
    for (PatchSetApproval psa : approvals) {
        Change.Id changeId = psa.getKey().getParentKey().getParentKey();
        checkArgument(changeId.equals(ctl.getId()), "Approval %s does not match change %s", psa.getKey(), ctl.getChange().getKey());
        if (psa.isLegacySubmit()) {
            unchanged.add(psa);
            continue;
        }
        LabelType label = labelTypes.byLabel(psa.getLabelId());
        if (label == null) {
            deleted.add(psa);
            continue;
        }
        PatchSetApproval copy = copy(psa);
        applyTypeFloor(label, copy);
        if (!applyRightFloor(ctl, label, copy)) {
            deleted.add(psa);
        } else if (copy.getValue() != psa.getValue()) {
            updated.add(copy);
        } else {
            unchanged.add(psa);
        }
    }
    return Result.create(unchanged, updated, deleted);
}
Also used : LabelTypes(com.google.gerrit.common.data.LabelTypes) LabelType(com.google.gerrit.common.data.LabelType) Change(com.google.gerrit.reviewdb.client.Change) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval)

Example 4 with LabelTypes

use of com.google.gerrit.common.data.LabelTypes in project gerrit by GerritCodeReview.

the class ChangeJson method setAllApprovals.

private void setAllApprovals(PermissionBackend.ForChange basePerm, ChangeData cd, Map<String, LabelWithStatus> labels) throws OrmException, PermissionBackendException {
    Change.Status status = cd.change().getStatus();
    checkState(status.isOpen(), "should not call setAllApprovals on %s change", status);
    // Include a user in the output for this label if either:
    //  - They are an explicit reviewer.
    //  - They ever voted on this change.
    Set<Account.Id> allUsers = new HashSet<>();
    allUsers.addAll(cd.reviewers().byState(ReviewerStateInternal.REVIEWER));
    for (PatchSetApproval psa : cd.approvals().values()) {
        allUsers.add(psa.getAccountId());
    }
    Table<Account.Id, String, PatchSetApproval> current = HashBasedTable.create(allUsers.size(), cd.getLabelTypes().getLabelTypes().size());
    for (PatchSetApproval psa : cd.currentApprovals()) {
        current.put(psa.getAccountId(), psa.getLabel(), psa);
    }
    LabelTypes labelTypes = cd.getLabelTypes();
    for (Account.Id accountId : allUsers) {
        PermissionBackend.ForChange perm = basePerm.user(userFactory.create(accountId));
        Map<String, VotingRangeInfo> pvr = getPermittedVotingRanges(permittedLabels(perm, cd));
        for (Map.Entry<String, LabelWithStatus> e : labels.entrySet()) {
            LabelType lt = labelTypes.byLabel(e.getKey());
            if (lt == null) {
                // author didn't intend for the label to show up in the table.
                continue;
            }
            Integer value;
            VotingRangeInfo permittedVotingRange = pvr.getOrDefault(lt.getName(), null);
            String tag = null;
            Timestamp date = null;
            PatchSetApproval psa = current.get(accountId, lt.getName());
            if (psa != null) {
                value = Integer.valueOf(psa.getValue());
                if (value == 0) {
                    // This may be a dummy approval that was inserted when the reviewer
                    // was added. Explicitly check whether the user can vote on this
                    // label.
                    value = perm.test(new LabelPermission(lt)) ? 0 : null;
                }
                tag = psa.getTag();
                date = psa.getGranted();
                if (psa.isPostSubmit()) {
                    log.warn("unexpected post-submit approval on open change: {}", psa);
                }
            } else {
                // Either the user cannot vote on this label, or they were added as a
                // reviewer but have not responded yet. Explicitly check whether the
                // user can vote on this label.
                value = perm.test(new LabelPermission(lt)) ? 0 : null;
            }
            addApproval(e.getValue().label(), approvalInfo(accountId, value, permittedVotingRange, tag, date));
        }
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) LabelTypes(com.google.gerrit.common.data.LabelTypes) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) VotingRangeInfo(com.google.gerrit.extensions.common.VotingRangeInfo) Change(com.google.gerrit.reviewdb.client.Change) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) Timestamp(java.sql.Timestamp) LabelType(com.google.gerrit.common.data.LabelType) ObjectId(org.eclipse.jgit.lib.ObjectId) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LabelPermission(com.google.gerrit.server.permissions.LabelPermission) HashSet(java.util.HashSet)

Example 5 with LabelTypes

use of com.google.gerrit.common.data.LabelTypes in project gerrit by GerritCodeReview.

the class ChangeControl method getLabelTypes.

/** All available label types for this change. */
public LabelTypes getLabelTypes() {
    String destBranch = getChange().getDest().get();
    List<LabelType> all = getProjectControl().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 (RefConfigSection.isValid(refPattern) && match(destBranch, refPattern)) {
                    r.add(l);
                    break;
                }
            }
        }
    }
    return new LabelTypes(r);
}
Also used : LabelTypes(com.google.gerrit.common.data.LabelTypes) LabelType(com.google.gerrit.common.data.LabelType)

Aggregations

LabelTypes (com.google.gerrit.common.data.LabelTypes)11 LabelType (com.google.gerrit.common.data.LabelType)8 LabelPermission (com.google.gerrit.server.permissions.LabelPermission)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 SubmitRecord (com.google.gerrit.common.data.SubmitRecord)3 DynamicMap (com.google.gerrit.extensions.registration.DynamicMap)3 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)3 LinkedHashMap (java.util.LinkedHashMap)3 TreeMap (java.util.TreeMap)3 LabelValue (com.google.gerrit.common.data.LabelValue)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 Change (com.google.gerrit.reviewdb.client.Change)2 CurrentUser (com.google.gerrit.server.CurrentUser)2 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)2 ChangeIndexCollection (com.google.gerrit.server.index.change.ChangeIndexCollection)2 ChangeControl (com.google.gerrit.server.project.ChangeControl)2