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 "";
}
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();
}
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);
}
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;
}
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("");
}
Aggregations