Search in sources :

Example 11 with SubmitRecord

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

the class ChangeJson method permittedLabels.

private Map<String, Collection<String>> permittedLabels(PermissionBackend.ForChange perm, ChangeData cd) throws OrmException, PermissionBackendException {
    boolean isMerged = cd.change().getStatus() == Change.Status.MERGED;
    LabelTypes labelTypes = cd.getLabelTypes();
    Map<String, LabelType> toCheck = new HashMap<>();
    for (SubmitRecord rec : submitRecords(cd)) {
        if (rec.labels != null) {
            for (SubmitRecord.Label r : rec.labels) {
                LabelType type = labelTypes.byLabel(r.label);
                if (type != null && (!isMerged || type.allowPostSubmit())) {
                    toCheck.put(type.getName(), type);
                }
            }
        }
    }
    Map<String, Short> labels = null;
    Set<LabelPermission.WithValue> can = perm.testLabels(toCheck.values());
    SetMultimap<String, String> permitted = LinkedHashMultimap.create();
    for (SubmitRecord rec : submitRecords(cd)) {
        if (rec.labels == null) {
            continue;
        }
        for (SubmitRecord.Label r : rec.labels) {
            LabelType type = labelTypes.byLabel(r.label);
            if (type == null || (isMerged && !type.allowPostSubmit())) {
                continue;
            }
            for (LabelValue v : type.getValues()) {
                boolean ok = can.contains(new LabelPermission.WithValue(type, v));
                if (isMerged) {
                    if (labels == null) {
                        labels = currentLabels(perm, cd);
                    }
                    short prev = labels.getOrDefault(type.getName(), (short) 0);
                    ok &= v.getValue() >= prev;
                }
                if (ok) {
                    permitted.put(r.label, v.formatValue());
                }
            }
        }
    }
    List<String> toClear = Lists.newArrayListWithCapacity(permitted.keySet().size());
    for (Map.Entry<String, Collection<String>> e : permitted.asMap().entrySet()) {
        if (isOnlyZero(e.getValue())) {
            toClear.add(e.getKey());
        }
    }
    for (String label : toClear) {
        permitted.removeAll(label);
    }
    return permitted.asMap();
}
Also used : LabelTypes(com.google.gerrit.common.data.LabelTypes) LabelValue(com.google.gerrit.common.data.LabelValue) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SubmitRecord(com.google.gerrit.common.data.SubmitRecord) LabelType(com.google.gerrit.common.data.LabelType) ChangeIndexCollection(com.google.gerrit.server.index.change.ChangeIndexCollection) Collection(java.util.Collection) LabelPermission(com.google.gerrit.server.permissions.LabelPermission) 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)

Example 12 with SubmitRecord

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

the class ChangeField method formatSubmitRecordValues.

@VisibleForTesting
static List<String> formatSubmitRecordValues(List<SubmitRecord> records, Account.Id changeOwner) {
    List<String> result = new ArrayList<>();
    for (SubmitRecord rec : records) {
        result.add(rec.status.name());
        if (rec.labels == null) {
            continue;
        }
        for (SubmitRecord.Label label : rec.labels) {
            String sl = label.status.toString() + ',' + label.label.toLowerCase();
            result.add(sl);
            String slc = sl + ',';
            if (label.appliedBy != null) {
                result.add(slc + label.appliedBy.get());
                if (label.appliedBy.equals(changeOwner)) {
                    result.add(slc + ChangeQueryBuilder.OWNER_ACCOUNT_ID.get());
                }
            }
        }
    }
    return result;
}
Also used : SubmitRecord(com.google.gerrit.common.data.SubmitRecord) ArrayList(java.util.ArrayList) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 13 with SubmitRecord

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

the class SubmitStrategyOp method setApproval.

private void setApproval(ChangeContext ctx, IdentifiedUser user) throws OrmException {
    Change.Id id = ctx.getChange().getId();
    List<SubmitRecord> records = args.commitStatus.getSubmitRecords(id);
    PatchSet.Id oldPsId = toMerge.getPatchsetId();
    PatchSet.Id newPsId = ctx.getChange().currentPatchSetId();
    logDebug("Add approval for " + id);
    ChangeUpdate origPsUpdate = ctx.getUpdate(oldPsId);
    origPsUpdate.putReviewer(user.getAccountId(), REVIEWER);
    LabelNormalizer.Result normalized = approve(ctx, origPsUpdate);
    ChangeUpdate newPsUpdate = ctx.getUpdate(newPsId);
    newPsUpdate.merge(args.submissionId, records);
    // approvals as well.
    if (!newPsId.equals(oldPsId)) {
        saveApprovals(normalized, ctx, newPsUpdate, true);
        submitter = convertPatchSet(newPsId).apply(submitter);
    }
}
Also used : SubmitRecord(com.google.gerrit.common.data.SubmitRecord) LabelNormalizer(com.google.gerrit.server.git.LabelNormalizer) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 14 with SubmitRecord

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

the class TestSubmitRule method apply.

@Override
public List<Record> apply(RevisionResource rsrc, TestSubmitRuleInput input) throws AuthException, OrmException {
    if (input == null) {
        input = new TestSubmitRuleInput();
    }
    if (input.rule != null && !rules.isProjectRulesEnabled()) {
        throw new AuthException("project rules are disabled");
    }
    input.filters = MoreObjects.firstNonNull(input.filters, filters);
    SubmitRuleEvaluator evaluator = new SubmitRuleEvaluator(changeDataFactory.create(db.get(), rsrc.getControl()));
    List<SubmitRecord> records = evaluator.setPatchSet(rsrc.getPatchSet()).setLogErrors(false).setSkipSubmitFilters(input.filters == Filters.SKIP).setRule(input.rule).evaluate();
    List<Record> out = Lists.newArrayListWithCapacity(records.size());
    AccountLoader accounts = accountInfoFactory.create(true);
    for (SubmitRecord r : records) {
        out.add(new Record(r, accounts));
    }
    if (!out.isEmpty()) {
        out.get(0).prologReductionCount = evaluator.getReductionsConsumed();
    }
    accounts.fill();
    return out;
}
Also used : SubmitRuleEvaluator(com.google.gerrit.server.project.SubmitRuleEvaluator) SubmitRecord(com.google.gerrit.common.data.SubmitRecord) TestSubmitRuleInput(com.google.gerrit.extensions.common.TestSubmitRuleInput) AccountLoader(com.google.gerrit.server.account.AccountLoader) AuthException(com.google.gerrit.extensions.restapi.AuthException) SubmitRecord(com.google.gerrit.common.data.SubmitRecord)

Example 15 with SubmitRecord

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

the class ChangeNotesParser method parseSubmitRecords.

private void parseSubmitRecords(List<String> lines) throws ConfigInvalidException {
    SubmitRecord rec = null;
    for (String line : lines) {
        int c = line.indexOf(": ");
        if (c < 0) {
            rec = new SubmitRecord();
            submitRecords.add(rec);
            int s = line.indexOf(' ');
            String statusStr = s >= 0 ? line.substring(0, s) : line;
            rec.status = Enums.getIfPresent(SubmitRecord.Status.class, statusStr).orNull();
            checkFooter(rec.status != null, FOOTER_SUBMITTED_WITH, line);
            if (s >= 0) {
                rec.errorMessage = line.substring(s);
            }
        } else {
            checkFooter(rec != null, FOOTER_SUBMITTED_WITH, line);
            SubmitRecord.Label label = new SubmitRecord.Label();
            if (rec.labels == null) {
                rec.labels = new ArrayList<>();
            }
            rec.labels.add(label);
            label.status = Enums.getIfPresent(SubmitRecord.Label.Status.class, line.substring(0, c)).orNull();
            checkFooter(label.status != null, FOOTER_SUBMITTED_WITH, line);
            int c2 = line.indexOf(": ", c + 2);
            if (c2 >= 0) {
                label.label = line.substring(c + 2, c2);
                PersonIdent ident = RawParseUtils.parsePersonIdent(line.substring(c2 + 2));
                checkFooter(ident != null, FOOTER_SUBMITTED_WITH, line);
                label.appliedBy = noteUtil.parseIdent(ident, id);
            } else {
                label.label = line.substring(c + 2);
            }
        }
    }
}
Also used : SubmitRecord(com.google.gerrit.common.data.SubmitRecord) PersonIdent(org.eclipse.jgit.lib.PersonIdent)

Aggregations

SubmitRecord (com.google.gerrit.common.data.SubmitRecord)19 Change (com.google.gerrit.reviewdb.client.Change)3 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)3 SubmitRuleEvaluator (com.google.gerrit.server.project.SubmitRuleEvaluator)3 OrmException (com.google.gwtorm.server.OrmException)3 ArrayList (java.util.ArrayList)3 LabelType (com.google.gerrit.common.data.LabelType)2 LabelTypes (com.google.gerrit.common.data.LabelTypes)2 Account (com.google.gerrit.reviewdb.client.Account)2 LabelPermission (com.google.gerrit.server.permissions.LabelPermission)2 RequestId (com.google.gerrit.server.util.RequestId)2 IntegerTerm (com.googlecode.prolog_cafe.lang.IntegerTerm)2 ListTerm (com.googlecode.prolog_cafe.lang.ListTerm)2 StructureTerm (com.googlecode.prolog_cafe.lang.StructureTerm)2 SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)2 Term (com.googlecode.prolog_cafe.lang.Term)2 VariableTerm (com.googlecode.prolog_cafe.lang.VariableTerm)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2