Search in sources :

Example 1 with LabelTypes

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

the class OutputStreamQuery method buildChangeAttribute.

private ChangeAttribute buildChangeAttribute(ChangeData d, Map<Project.NameKey, Repository> repos, Map<Project.NameKey, RevWalk> revWalks) throws IOException {
    LabelTypes labelTypes = d.getLabelTypes();
    ChangeAttribute c = eventFactory.asChangeAttribute(d.change(), d.notes());
    eventFactory.extend(c, d.change());
    if (!trackingFooters.isEmpty()) {
        eventFactory.addTrackingIds(c, d.trackingFooters());
    }
    if (includeAllReviewers) {
        eventFactory.addAllReviewers(c, d.notes());
    }
    if (includeSubmitRecords) {
        SubmitRuleOptions options = SubmitRuleOptions.builder().recomputeOnClosedChanges(true).build();
        eventFactory.addSubmitRecords(c, submitRuleEvaluatorFactory.create(options).evaluate(d));
    }
    if (includeCommitMessage) {
        eventFactory.addCommitMessage(c, d.commitMessage());
    }
    RevWalk rw = null;
    if (includePatchSets || includeCurrentPatchSet || includeDependencies) {
        Project.NameKey p = d.change().getProject();
        rw = revWalks.get(p);
        // Cache and reuse repos and revwalks.
        if (rw == null) {
            Repository repo = repoManager.openRepository(p);
            checkState(repos.put(p, repo) == null);
            rw = new RevWalk(repo);
            revWalks.put(p, rw);
        }
    }
    if (includePatchSets) {
        eventFactory.addPatchSets(rw, c, d.patchSets(), includeApprovals ? d.approvals().asMap() : null, includeFiles, d.change(), labelTypes);
    }
    if (includeCurrentPatchSet) {
        PatchSet current = d.currentPatchSet();
        if (current != null) {
            c.currentPatchSet = eventFactory.asPatchSetAttribute(rw, d.change(), current);
            eventFactory.addApprovals(c.currentPatchSet, d.currentApprovals(), labelTypes);
            if (includeFiles) {
                eventFactory.addPatchSetFileNames(c.currentPatchSet, d.change(), d.currentPatchSet());
            }
            if (includeComments) {
                eventFactory.addPatchSetComments(c.currentPatchSet, d.publishedComments());
            }
        }
    }
    if (includeComments) {
        eventFactory.addComments(c, d.messages());
        if (includePatchSets) {
            eventFactory.addPatchSets(rw, c, d.patchSets(), includeApprovals ? d.approvals().asMap() : null, includeFiles, d.change(), labelTypes);
            for (PatchSetAttribute attribute : c.patchSets) {
                eventFactory.addPatchSetComments(attribute, d.publishedComments());
            }
        }
    }
    if (includeDependencies) {
        eventFactory.addDependencies(rw, c, d.change(), d.currentPatchSet());
    }
    List<PluginDefinedInfo> pluginInfos = pluginInfosByChange.get(d.getId());
    if (!pluginInfos.isEmpty()) {
        c.plugins = pluginInfos;
    }
    return c;
}
Also used : Project(com.google.gerrit.entities.Project) LabelTypes(com.google.gerrit.entities.LabelTypes) Repository(org.eclipse.jgit.lib.Repository) ChangeAttribute(com.google.gerrit.server.data.ChangeAttribute) SubmitRuleOptions(com.google.gerrit.server.project.SubmitRuleOptions) PatchSetAttribute(com.google.gerrit.server.data.PatchSetAttribute) PatchSet(com.google.gerrit.entities.PatchSet) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PluginDefinedInfo(com.google.gerrit.extensions.common.PluginDefinedInfo)

Example 2 with LabelTypes

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

the class LabelsJson method labelsFor.

/**
 * Returns all {@link LabelInfo}s for a single change. Uses the provided {@link AccountLoader} to
 * lazily populate accounts. Callers have to call {@link AccountLoader#fill()} afterwards to
 * populate all accounts in the returned {@link LabelInfo}s.
 */
Map<String, LabelInfo> labelsFor(AccountLoader accountLoader, ChangeData cd, boolean standard, boolean detailed) throws PermissionBackendException {
    if (!standard && !detailed) {
        return null;
    }
    LabelTypes labelTypes = cd.getLabelTypes();
    Map<String, LabelWithStatus> withStatus = cd.change().isMerged() ? labelsForSubmittedChange(accountLoader, cd, labelTypes, standard, detailed) : labelsForUnsubmittedChange(accountLoader, cd, labelTypes, standard, detailed);
    return ImmutableMap.copyOf(Maps.transformValues(withStatus, LabelWithStatus::label));
}
Also used : LabelTypes(com.google.gerrit.entities.LabelTypes)

Example 3 with LabelTypes

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

the class StreamEventsApiListener method approvalsAttributeSupplier.

private Supplier<ApprovalAttribute[]> approvalsAttributeSupplier(final Change change, Map<String, ApprovalInfo> newApprovals, final Map<String, ApprovalInfo> oldApprovals) {
    final Map<String, Short> approvals = convertApprovalsMap(newApprovals);
    return Suppliers.memoize(() -> {
        Project.NameKey nameKey = change.getProject();
        LabelTypes labelTypes = projectCache.get(nameKey).orElseThrow(illegalState(nameKey)).getLabelTypes();
        if (approvals.size() > 0) {
            ApprovalAttribute[] r = new ApprovalAttribute[approvals.size()];
            int i = 0;
            for (Map.Entry<String, Short> approval : approvals.entrySet()) {
                r[i++] = getApprovalAttribute(labelTypes, approval, convertApprovalsMap(oldApprovals));
            }
            return r;
        }
        return null;
    });
}
Also used : Project(com.google.gerrit.entities.Project) LabelTypes(com.google.gerrit.entities.LabelTypes) ApprovalAttribute(com.google.gerrit.server.data.ApprovalAttribute) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with LabelTypes

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

the class GerritCommonTest method setUpEnvironment.

@Override
protected void setUpEnvironment(PrologEnvironment env) throws Exception {
    LabelTypes labelTypes = new LabelTypes(Arrays.asList(TestLabels.codeReview(), TestLabels.verified()));
    ChangeData cd = mock(ChangeData.class);
    when(cd.getLabelTypes()).thenReturn(labelTypes);
    env.set(StoredValues.CHANGE_DATA, cd);
}
Also used : LabelTypes(com.google.gerrit.entities.LabelTypes) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 5 with LabelTypes

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

the class LabelsJson method setAllApprovals.

private void setAllApprovals(AccountLoader accountLoader, ChangeData cd, Map<String, LabelWithStatus> labels, boolean detailed) throws PermissionBackendException {
    checkState(!cd.change().isMerged(), "should not call setAllApprovals on %s change", ChangeUtil.status(cd.change()));
    // 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.accountId());
    }
    Table<Account.Id, String, PatchSetApproval> current = HashBasedTable.create(allUsers.size(), cd.getLabelTypes().getLabelTypes().size());
    for (PatchSetApproval psa : cd.currentApprovals()) {
        current.put(psa.accountId(), psa.label(), psa);
    }
    LabelTypes labelTypes = cd.getLabelTypes();
    for (Account.Id accountId : allUsers) {
        Map<String, VotingRangeInfo> pvr = null;
        PermissionBackend.ForChange perm = null;
        if (detailed) {
            perm = permissionBackend.absentUser(accountId).change(cd);
            pvr = getPermittedVotingRanges(permittedLabels(accountId, cd));
        }
        for (Map.Entry<String, LabelWithStatus> e : labels.entrySet()) {
            Optional<LabelType> lt = labelTypes.byLabel(e.getKey());
            if (!lt.isPresent()) {
                // author didn't intend for the label to show up in the table.
                continue;
            }
            Integer value;
            VotingRangeInfo permittedVotingRange = pvr == null ? null : pvr.getOrDefault(lt.get().getName(), null);
            String tag = null;
            Instant date = null;
            PatchSetApproval psa = current.get(accountId, lt.get().getName());
            if (psa != null) {
                value = Integer.valueOf(psa.value());
                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 != null && perm.test(new LabelPermission(lt.get())) ? 0 : null;
                }
                tag = psa.tag().orElse(null);
                date = psa.granted();
                if (psa.postSubmit()) {
                    logger.atWarning().log("unexpected post-submit approval on open change: %s", 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 != null && perm.test(new LabelPermission(lt.get())) ? 0 : null;
            }
            addApproval(e.getValue().label(), approvalInfo(accountLoader, accountId, value, permittedVotingRange, tag, date));
        }
    }
}
Also used : Account(com.google.gerrit.entities.Account) LabelTypes(com.google.gerrit.entities.LabelTypes) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) Instant(java.time.Instant) VotingRangeInfo(com.google.gerrit.extensions.common.VotingRangeInfo) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) LabelType(com.google.gerrit.entities.LabelType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) TreeMap(java.util.TreeMap) LabelPermission(com.google.gerrit.server.permissions.LabelPermission) HashSet(java.util.HashSet)

Aggregations

LabelTypes (com.google.gerrit.entities.LabelTypes)16 LabelType (com.google.gerrit.entities.LabelType)11 PatchSetApproval (com.google.gerrit.entities.PatchSetApproval)8 Map (java.util.Map)6 Account (com.google.gerrit.entities.Account)5 PatchSet (com.google.gerrit.entities.PatchSet)5 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)5 ChangeData (com.google.gerrit.server.query.change.ChangeData)5 HashMap (java.util.HashMap)5 LabelPermission (com.google.gerrit.server.permissions.LabelPermission)4 HashSet (java.util.HashSet)4 LinkedHashMap (java.util.LinkedHashMap)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Lists (com.google.common.collect.Lists)3 Maps (com.google.common.collect.Maps)3 FluentLogger (com.google.common.flogger.FluentLogger)3 Nullable (com.google.gerrit.common.Nullable)3 Change (com.google.gerrit.entities.Change)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)3