use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class ProjectConfig method saveLabelSections.
private void saveLabelSections(Config rc) {
List<String> existing = new ArrayList<>(rc.getSubsections(LABEL));
if (!new ArrayList<>(labelSections.keySet()).equals(existing)) {
// Order of sections changed, remove and rewrite them all.
unsetSection(rc, LABEL);
}
Set<String> toUnset = new HashSet<>(existing);
for (Map.Entry<String, LabelType> e : labelSections.entrySet()) {
String name = e.getKey();
LabelType label = e.getValue();
toUnset.remove(name);
if (label.getDescription().isPresent() && !label.getDescription().get().isEmpty()) {
rc.setString(LABEL, name, KEY_LABEL_DESCRIPTION, label.getDescription().get());
} else {
rc.unset(LABEL, name, KEY_LABEL_DESCRIPTION);
}
rc.setString(LABEL, name, KEY_FUNCTION, label.getFunction().getFunctionName());
rc.setInt(LABEL, name, KEY_DEFAULT_VALUE, label.getDefaultValue());
setBooleanConfigKey(rc, LABEL, name, KEY_ALLOW_POST_SUBMIT, label.isAllowPostSubmit(), LabelType.DEF_ALLOW_POST_SUBMIT);
setBooleanConfigKey(rc, LABEL, name, KEY_IGNORE_SELF_APPROVAL, label.isIgnoreSelfApproval(), LabelType.DEF_IGNORE_SELF_APPROVAL);
setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ANY_SCORE, label.isCopyAnyScore(), LabelType.DEF_COPY_ANY_SCORE);
setBooleanConfigKey(rc, LABEL, name, KEY_COPY_MIN_SCORE, label.isCopyMinScore(), LabelType.DEF_COPY_MIN_SCORE);
setBooleanConfigKey(rc, LABEL, name, KEY_COPY_MAX_SCORE, label.isCopyMaxScore(), LabelType.DEF_COPY_MAX_SCORE);
setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_ON_TRIVIAL_REBASE, label.isCopyAllScoresOnTrivialRebase(), LabelType.DEF_COPY_ALL_SCORES_ON_TRIVIAL_REBASE);
setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_IF_NO_CODE_CHANGE, label.isCopyAllScoresIfNoCodeChange(), LabelType.DEF_COPY_ALL_SCORES_IF_NO_CODE_CHANGE);
setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_IF_NO_CHANGE, label.isCopyAllScoresIfNoChange(), LabelType.DEF_COPY_ALL_SCORES_IF_NO_CHANGE);
setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_IF_LIST_OF_FILES_DID_NOT_CHANGE, label.isCopyAllScoresIfListOfFilesDidNotChange(), LabelType.DEF_COPY_ALL_SCORES_IF_LIST_OF_FILES_DID_NOT_CHANGE);
setBooleanConfigKey(rc, LABEL, name, KEY_COPY_ALL_SCORES_ON_MERGE_FIRST_PARENT_UPDATE, label.isCopyAllScoresOnMergeFirstParentUpdate(), LabelType.DEF_COPY_ALL_SCORES_ON_MERGE_FIRST_PARENT_UPDATE);
rc.setStringList(LABEL, name, KEY_COPY_VALUE, label.getCopyValues().stream().map(LabelValue::formatValue).collect(toList()));
setBooleanConfigKey(rc, LABEL, name, KEY_CAN_OVERRIDE, label.isCanOverride(), LabelType.DEF_CAN_OVERRIDE);
List<String> values = new ArrayList<>(label.getValues().size());
for (LabelValue value : label.getValues()) {
values.add(value.format().trim());
}
rc.setStringList(LABEL, name, KEY_VALUE, values);
if (label.getCopyCondition().isPresent()) {
rc.setString(LABEL, name, KEY_COPY_CONDITION, label.getCopyCondition().get());
} else {
rc.unset(LABEL, name, KEY_COPY_CONDITION);
}
List<String> refPatterns = label.getRefPatterns();
if (refPatterns != null && !refPatterns.isEmpty()) {
rc.setStringList(LABEL, name, KEY_BRANCH, refPatterns);
} else {
rc.unset(LABEL, name, KEY_BRANCH);
}
}
for (String name : toUnset) {
rc.unsetSection(LABEL, name);
}
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class DefaultSubmitRule method evaluate.
@Override
public Optional<SubmitRecord> evaluate(ChangeData cd) {
SubmitRecord submitRecord = new SubmitRecord();
submitRecord.status = SubmitRecord.Status.OK;
List<LabelType> labelTypes = cd.getLabelTypes().getLabelTypes();
List<PatchSetApproval> approvals = cd.currentApprovals();
submitRecord.labels = new ArrayList<>(labelTypes.size());
for (LabelType t : labelTypes) {
LabelFunction labelFunction = t.getFunction();
checkState(labelFunction != null, "Unable to find the LabelFunction for label %s, change %s", t.getName(), cd.getId());
Collection<PatchSetApproval> approvalsForLabel = getApprovalsForLabel(approvals, t);
SubmitRecord.Label label = labelFunction.check(t, approvalsForLabel);
submitRecord.labels.add(label);
switch(label.status) {
case OK:
case MAY:
break;
case NEED:
case REJECT:
case IMPOSSIBLE:
submitRecord.status = SubmitRecord.Status.NOT_READY;
break;
}
}
return Optional.of(submitRecord);
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class IgnoreSelfApprovalRule method evaluate.
@Override
public Optional<SubmitRecord> evaluate(ChangeData cd) {
List<LabelType> labelTypes = cd.getLabelTypes().getLabelTypes();
List<PatchSetApproval> approvals = cd.currentApprovals();
boolean shouldIgnoreSelfApproval = labelTypes.stream().anyMatch(LabelType::isIgnoreSelfApproval);
if (!shouldIgnoreSelfApproval) {
// Shortcut to avoid further processing if no label should ignore uploader approvals
return Optional.empty();
}
Account.Id uploader = cd.currentPatchSet().uploader();
SubmitRecord submitRecord = new SubmitRecord();
submitRecord.status = SubmitRecord.Status.OK;
submitRecord.labels = new ArrayList<>(labelTypes.size());
submitRecord.requirements = new ArrayList<>();
for (LabelType t : labelTypes) {
if (!t.isIgnoreSelfApproval()) {
// The default rules are enough in this case.
continue;
}
LabelFunction labelFunction = t.getFunction();
if (labelFunction == null) {
continue;
}
Collection<PatchSetApproval> allApprovalsForLabel = filterApprovalsByLabel(approvals, t);
SubmitRecord.Label allApprovalsCheckResult = labelFunction.check(t, allApprovalsForLabel);
SubmitRecord.Label ignoreSelfApprovalCheckResult = labelFunction.check(t, filterOutPositiveApprovalsOfUser(allApprovalsForLabel, uploader));
if (labelCheckPassed(allApprovalsCheckResult) && !labelCheckPassed(ignoreSelfApprovalCheckResult)) {
// The label has a valid approval from the uploader and no other valid approval. Set the
// label
// to NOT_READY and indicate the need for non-uploader approval as requirement.
submitRecord.labels.add(ignoreSelfApprovalCheckResult);
submitRecord.status = SubmitRecord.Status.NOT_READY;
// Add an additional requirement to be more descriptive on why the label counts as not
// approved.
submitRecord.requirements.add(LegacySubmitRequirement.builder().setFallbackText("Approval from non-uploader required").setType("non_uploader_approval").build());
}
}
if (submitRecord.labels.isEmpty()) {
return Optional.empty();
}
return Optional.of(submitRecord);
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class SetLabel method apply.
@Override
public Response<LabelDefinitionInfo> apply(LabelResource rsrc, LabelDefinitionInput input) throws AuthException, BadRequestException, ResourceConflictException, PermissionBackendException, IOException, ConfigInvalidException {
if (!user.get().isIdentifiedUser()) {
throw new AuthException("Authentication required");
}
permissionBackend.currentUser().project(rsrc.getProject().getNameKey()).check(ProjectPermission.WRITE_CONFIG);
if (input == null) {
input = new LabelDefinitionInput();
}
LabelType labelType = rsrc.getLabelType();
try (MetaDataUpdate md = updateFactory.create(rsrc.getProject().getNameKey())) {
ProjectConfig config = projectConfigFactory.read(md);
if (updateLabel(config, labelType, input)) {
if (input.commitMessage != null) {
md.setMessage(Strings.emptyToNull(input.commitMessage.trim()));
} else {
md.setMessage("Update label");
}
String newName = Strings.nullToEmpty(input.name).trim();
labelType = config.getLabelSections().get(newName.isEmpty() ? labelType.getName() : newName);
config.commit(md);
projectCache.evictAndReindex(rsrc.getProject().getProjectState().getProject());
}
}
return Response.ok(LabelDefinitionJson.format(rsrc.getProject().getNameKey(), labelType));
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class AllProjectsCreator method initDefaultAcls.
private void initDefaultAcls(ProjectConfig config, AllProjectsInput input) {
checkArgument(input.codeReviewLabel().isPresent());
LabelType codeReviewLabel = input.codeReviewLabel().get();
config.upsertAccessSection(AccessSection.HEADS, heads -> {
initDefaultAclsForRegisteredUsers(heads, codeReviewLabel, config);
});
config.upsertAccessSection(AccessSection.GLOBAL_CAPABILITIES, capabilities -> {
input.serviceUsersGroup().ifPresent(batchUsersGroup -> initDefaultAclsForBatchUsers(capabilities, config, batchUsersGroup));
});
input.administratorsGroup().ifPresent(adminsGroup -> initDefaultAclsForAdmins(config, codeReviewLabel, adminsGroup));
}
Aggregations