use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class CreateLabel method createLabel.
/**
* Creates a new label.
*
* @param config the project config
* @param label the name of the new label
* @param input the input that describes the new label
* @return the created label type
* @throws BadRequestException if there was invalid data in the input
* @throws ResourceConflictException if the label cannot be created due to a conflict
*/
public LabelType createLabel(ProjectConfig config, String label, LabelDefinitionInput input) throws BadRequestException, ResourceConflictException {
if (config.getLabelSections().containsKey(label)) {
throw new ResourceConflictException(String.format("label %s already exists", label));
}
for (String labelName : config.getLabelSections().keySet()) {
if (labelName.equalsIgnoreCase(label)) {
throw new ResourceConflictException(String.format("label %s conflicts with existing label %s", label, labelName));
}
}
if (input.values == null || input.values.isEmpty()) {
throw new BadRequestException("values are required");
}
try {
LabelType.checkName(label);
} catch (IllegalArgumentException e) {
throw new BadRequestException("invalid name: " + label, e);
}
List<LabelValue> values = LabelDefinitionInputParser.parseValues(input.values);
LabelType.Builder labelType = LabelType.builder(LabelType.checkName(label), values);
if (input.description != null) {
String description = Strings.emptyToNull(input.description.trim());
labelType.setDescription(Optional.ofNullable(description));
}
if (input.function != null && !input.function.trim().isEmpty()) {
labelType.setFunction(LabelDefinitionInputParser.parseFunction(input.function));
} else {
labelType.setFunction(LabelFunction.MAX_WITH_BLOCK);
}
if (input.defaultValue != null) {
labelType.setDefaultValue(LabelDefinitionInputParser.parseDefaultValue(labelType, input.defaultValue));
}
if (input.branches != null) {
labelType.setRefPatterns(LabelDefinitionInputParser.parseBranches(input.branches));
}
if (input.canOverride != null) {
labelType.setCanOverride(input.canOverride);
}
if (input.copyAnyScore != null) {
labelType.setCopyAnyScore(input.copyAnyScore);
}
if (input.copyCondition != null) {
try {
approvalQueryBuilder.parse(input.copyCondition);
} catch (QueryParseException e) {
throw new BadRequestException("unable to parse copy condition. got: " + input.copyCondition + ". " + e.getMessage(), e);
}
if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
throw new BadRequestException("can't set and unset copyCondition in the same request");
}
labelType.setCopyCondition(Strings.emptyToNull(input.copyCondition));
}
if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
labelType.setCopyCondition(null);
}
if (input.copyMinScore != null) {
labelType.setCopyMinScore(input.copyMinScore);
}
if (input.copyMaxScore != null) {
labelType.setCopyMaxScore(input.copyMaxScore);
}
if (input.copyAllScoresIfListOfFilesDidNotChange != null) {
labelType.setCopyAllScoresIfListOfFilesDidNotChange(input.copyAllScoresIfListOfFilesDidNotChange);
}
if (input.copyAllScoresIfNoChange != null) {
labelType.setCopyAllScoresIfNoChange(input.copyAllScoresIfNoChange);
}
if (input.copyAllScoresIfNoCodeChange != null) {
labelType.setCopyAllScoresIfNoCodeChange(input.copyAllScoresIfNoCodeChange);
}
if (input.copyAllScoresOnTrivialRebase != null) {
labelType.setCopyAllScoresOnTrivialRebase(input.copyAllScoresOnTrivialRebase);
}
if (input.copyAllScoresOnMergeFirstParentUpdate != null) {
labelType.setCopyAllScoresOnMergeFirstParentUpdate(input.copyAllScoresOnMergeFirstParentUpdate);
}
if (input.copyValues != null) {
labelType.setCopyValues(input.copyValues);
}
if (input.allowPostSubmit != null) {
labelType.setAllowPostSubmit(input.allowPostSubmit);
}
if (input.ignoreSelfApproval != null) {
labelType.setIgnoreSelfApproval(input.ignoreSelfApproval);
}
LabelType lt = labelType.build();
config.upsertLabelType(lt);
return lt;
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class CreateLabel method apply.
@Override
public Response<LabelDefinitionInfo> apply(ProjectResource rsrc, IdString id, LabelDefinitionInput input) throws AuthException, BadRequestException, ResourceConflictException, PermissionBackendException, IOException, ConfigInvalidException {
if (!user.get().isIdentifiedUser()) {
throw new AuthException("Authentication required");
}
permissionBackend.currentUser().project(rsrc.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
if (input == null) {
input = new LabelDefinitionInput();
}
if (input.name != null && !input.name.equals(id.get())) {
throw new BadRequestException("name in input must match name in URL");
}
try (MetaDataUpdate md = updateFactory.create(rsrc.getNameKey())) {
ProjectConfig config = projectConfigFactory.read(md);
LabelType labelType = createLabel(config, id.get(), input);
if (input.commitMessage != null) {
md.setMessage(Strings.emptyToNull(input.commitMessage.trim()));
} else {
md.setMessage("Update label");
}
config.commit(md);
projectCache.evictAndReindex(rsrc.getProjectState().getProject());
return Response.created(LabelDefinitionJson.format(rsrc.getNameKey(), labelType));
}
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class PRED__load_commit_labels_1 method exec.
@Override
public Operation exec(Prolog engine) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
Term listHead = Prolog.Nil;
ChangeData cd = StoredValues.CHANGE_DATA.get(engine);
LabelTypes types = cd.getLabelTypes();
for (PatchSetApproval a : cd.currentApprovals()) {
Optional<LabelType> t = types.byLabel(a.labelId());
if (!t.isPresent()) {
continue;
}
StructureTerm labelTerm = new StructureTerm(sym_label, SymbolTerm.intern(t.get().getName()), new IntegerTerm(a.value()));
StructureTerm userTerm = new StructureTerm(sym_user, new IntegerTerm(a.accountId().get()));
listHead = new ListTerm(new StructureTerm(sym_commit_label, labelTerm, userTerm), listHead);
}
if (!a1.unify(listHead, engine.trail)) {
return engine.fail();
}
return cont;
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class EventFactory method asApprovalAttribute.
/**
* Create an ApprovalAttribute for the given approval suitable for serialization to JSON.
*
* @param labelTypes label types for the containing project
* @return object suitable for serialization to JSON
*/
public ApprovalAttribute asApprovalAttribute(PatchSetApproval approval, LabelTypes labelTypes) {
ApprovalAttribute a = new ApprovalAttribute();
a.type = approval.labelId().get();
a.value = Short.toString(approval.value());
a.by = asAccountAttribute(approval.accountId());
a.grantedOn = approval.granted().getEpochSecond();
a.oldValue = null;
Optional<LabelType> lt = labelTypes.byLabel(approval.labelId());
lt.ifPresent(l -> a.description = l.getName());
return a;
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class ReplaceOp method fireApprovalsEvent.
private void fireApprovalsEvent(PostUpdateContext ctx) {
if (approvals.isEmpty()) {
return;
}
/* For labels that are not set in this operation, show the "current" value
* of 0, and no oldValue as the value was not modified by this operation.
* For labels that are set in this operation, the value was modified, so
* show a transition from an oldValue of 0 to the new value.
*/
List<LabelType> labels = projectCache.get(ctx.getProject()).orElseThrow(illegalState(ctx.getProject())).getLabelTypes(notes).getLabelTypes();
Map<String, Short> allApprovals = new HashMap<>();
Map<String, Short> oldApprovals = new HashMap<>();
for (LabelType lt : labels) {
allApprovals.put(lt.getName(), (short) 0);
oldApprovals.put(lt.getName(), null);
}
for (Map.Entry<String, Short> entry : approvals.entrySet()) {
if (entry.getValue() != 0) {
allApprovals.put(entry.getKey(), entry.getValue());
oldApprovals.put(entry.getKey(), (short) 0);
}
}
commentAdded.fire(ctx.getChangeData(notes), newPatchSet, ctx.getAccount(), null, allApprovals, oldApprovals, ctx.getWhen());
}
Aggregations