Search in sources :

Example 46 with LabelType

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

the class MergedSender method getApprovals.

public String getApprovals() {
    try {
        Table<Account.Id, String, PatchSetApproval> pos = HashBasedTable.create();
        Table<Account.Id, String, PatchSetApproval> neg = HashBasedTable.create();
        for (PatchSetApproval ca : args.approvalsUtil.byPatchSet(changeData.notes(), patchSet.id())) {
            Optional<LabelType> lt = labelTypes.byLabel(ca.labelId());
            if (!lt.isPresent()) {
                continue;
            }
            if (ca.value() > 0) {
                pos.put(ca.accountId(), lt.get().getName(), ca);
            } else if (ca.value() < 0) {
                neg.put(ca.accountId(), lt.get().getName(), ca);
            }
        }
        return format("Approvals", pos) + format("Objections", neg);
    } catch (StorageException err) {
    // Don't list the approvals
    }
    return "";
}
Also used : LabelType(com.google.gerrit.entities.LabelType) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) StorageException(com.google.gerrit.exceptions.StorageException)

Example 47 with LabelType

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

the class MergedSender method format.

private String format(String type, Table<Account.Id, String, PatchSetApproval> approvals) {
    StringBuilder txt = new StringBuilder();
    if (approvals.isEmpty()) {
        return "";
    }
    txt.append(type).append(":\n");
    for (Account.Id id : approvals.rowKeySet()) {
        txt.append("  ");
        txt.append(getNameFor(id));
        txt.append(": ");
        boolean first = true;
        for (LabelType lt : labelTypes.getLabelTypes()) {
            PatchSetApproval ca = approvals.get(id, lt.getName());
            if (ca == null) {
                continue;
            }
            if (first) {
                first = false;
            } else {
                txt.append("; ");
            }
            LabelValue v = lt.getValue(ca);
            if (v != null) {
                txt.append(v.getText());
            } else {
                txt.append(lt.getName());
                txt.append('=');
                txt.append(LabelValue.formatValue(ca.value()));
            }
        }
        txt.append('\n');
    }
    txt.append('\n');
    return txt.toString();
}
Also used : Account(com.google.gerrit.entities.Account) LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 48 with LabelType

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

the class LabelsCollection method parse.

@Override
public LabelResource parse(ProjectResource parent, IdString id) throws AuthException, ResourceNotFoundException, PermissionBackendException {
    if (!user.get().isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    permissionBackend.currentUser().project(parent.getNameKey()).check(ProjectPermission.READ_CONFIG);
    LabelType labelType = parent.getProjectState().getConfig().getLabelSections().get(id.get());
    if (labelType == null) {
        throw new ResourceNotFoundException(id);
    }
    return new LabelResource(parent, labelType);
}
Also used : LabelResource(com.google.gerrit.server.project.LabelResource) LabelType(com.google.gerrit.entities.LabelType) AuthException(com.google.gerrit.extensions.restapi.AuthException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 49 with LabelType

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

the class ListLabels method listLabels.

private List<LabelDefinitionInfo> listLabels(ProjectState projectState) {
    Collection<LabelType> labelTypes = projectState.getConfig().getLabelSections().values();
    List<LabelDefinitionInfo> labels = new ArrayList<>(labelTypes.size());
    for (LabelType labelType : labelTypes) {
        labels.add(LabelDefinitionJson.format(projectState.getNameKey(), labelType));
    }
    labels.sort(Comparator.comparing(l -> l.name));
    return labels;
}
Also used : PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RestReadView(com.google.gerrit.extensions.restapi.RestReadView) CurrentUser(com.google.gerrit.server.CurrentUser) Inject(com.google.inject.Inject) Collection(java.util.Collection) ProjectState(com.google.gerrit.server.project.ProjectState) ProjectResource(com.google.gerrit.server.project.ProjectResource) Option(org.kohsuke.args4j.Option) Response(com.google.gerrit.extensions.restapi.Response) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) LabelDefinitionJson(com.google.gerrit.server.project.LabelDefinitionJson) ArrayList(java.util.ArrayList) ProjectPermission(com.google.gerrit.server.permissions.ProjectPermission) Provider(com.google.inject.Provider) List(java.util.List) LabelType(com.google.gerrit.entities.LabelType) AuthException(com.google.gerrit.extensions.restapi.AuthException) Comparator(java.util.Comparator) LabelDefinitionInfo(com.google.gerrit.extensions.common.LabelDefinitionInfo) LabelType(com.google.gerrit.entities.LabelType) LabelDefinitionInfo(com.google.gerrit.extensions.common.LabelDefinitionInfo) ArrayList(java.util.ArrayList)

Example 50 with LabelType

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

the class PostLabels method apply.

@Override
public Response<?> apply(ProjectResource rsrc, BatchLabelInput input) throws AuthException, UnprocessableEntityException, PermissionBackendException, IOException, ConfigInvalidException, BadRequestException, ResourceConflictException {
    if (!user.get().isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    permissionBackend.currentUser().project(rsrc.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
    if (input == null) {
        input = new BatchLabelInput();
    }
    try (MetaDataUpdate md = updateFactory.create(rsrc.getNameKey())) {
        boolean dirty = false;
        ProjectConfig config = projectConfigFactory.read(md);
        if (input.delete != null && !input.delete.isEmpty()) {
            for (String labelName : input.delete) {
                if (!deleteLabel.deleteLabel(config, labelName.trim())) {
                    throw new UnprocessableEntityException(String.format("label %s not found", labelName));
                }
            }
            dirty = true;
        }
        if (input.create != null && !input.create.isEmpty()) {
            for (LabelDefinitionInput labelInput : input.create) {
                if (labelInput.name == null || labelInput.name.trim().isEmpty()) {
                    throw new BadRequestException("label name is required for new label");
                }
                if (labelInput.commitMessage != null) {
                    throw new BadRequestException("commit message on label definition input not supported");
                }
                createLabel.createLabel(config, labelInput.name.trim(), labelInput);
            }
            dirty = true;
        }
        if (input.update != null && !input.update.isEmpty()) {
            for (Map.Entry<String, LabelDefinitionInput> e : input.update.entrySet()) {
                LabelType labelType = config.getLabelSections().get(e.getKey().trim());
                if (labelType == null) {
                    throw new UnprocessableEntityException(String.format("label %s not found", e.getKey()));
                }
                if (e.getValue().commitMessage != null) {
                    throw new BadRequestException("commit message on label definition input not supported");
                }
                setLabel.updateLabel(config, labelType, e.getValue());
            }
            dirty = true;
        }
        if (input.commitMessage != null) {
            md.setMessage(Strings.emptyToNull(input.commitMessage.trim()));
        } else {
            md.setMessage("Update labels");
        }
        if (dirty) {
            config.commit(md);
            projectCache.evictAndReindex(rsrc.getProjectState().getProject());
        }
    }
    return Response.ok("");
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) LabelType(com.google.gerrit.entities.LabelType) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) BatchLabelInput(com.google.gerrit.extensions.common.BatchLabelInput) LabelDefinitionInput(com.google.gerrit.extensions.common.LabelDefinitionInput) Map(java.util.Map) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

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