use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class ProjectState method getLabelTypes.
/**
* All available label types.
*/
public LabelTypes getLabelTypes() {
Map<String, LabelType> types = new LinkedHashMap<>();
for (ProjectState s : treeInOrder()) {
for (LabelType type : s.getConfig().getLabelSections().values()) {
String lower = type.getName().toLowerCase();
LabelType old = types.get(lower);
if (old == null || old.isCanOverride()) {
types.put(lower, type);
}
}
}
List<LabelType> all = Lists.newArrayListWithCapacity(types.size());
for (LabelType type : types.values()) {
if (!type.getValues().isEmpty()) {
all.add(type);
}
}
return new LabelTypes(Collections.unmodifiableList(all));
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class ProjectState method getLabelTypes.
/**
* All available label types for this branch.
*/
public LabelTypes getLabelTypes(BranchNameKey destination) {
List<LabelType> all = getLabelTypes().getLabelTypes();
List<LabelType> r = Lists.newArrayListWithCapacity(all.size());
for (LabelType l : all) {
List<String> refs = l.getRefPatterns();
if (refs == null) {
r.add(l);
} else {
for (String refPattern : refs) {
if (refPattern.contains("${")) {
logger.atWarning().log("Ref pattern for label %s in project %s contains illegal expanded parameters: %s." + " Ref pattern will be ignored.", l, getName(), refPattern);
continue;
}
if (AccessSection.isValidRefSectionName(refPattern) && match(destination, refPattern)) {
r.add(l);
break;
}
}
}
}
return new LabelTypes(r);
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class SetLabel method updateLabel.
/**
* Updates the given label.
*
* @param config the project config
* @param labelType the label type that should be updated
* @param input the input that describes the label update
* @return whether the label type was modified
* @throws BadRequestException if there was invalid data in the input
* @throws ResourceConflictException if the update cannot be applied due to a conflict
*/
public boolean updateLabel(ProjectConfig config, LabelType labelType, LabelDefinitionInput input) throws BadRequestException, ResourceConflictException {
boolean dirty = false;
LabelType.Builder labelTypeBuilder = labelType.toBuilder();
if (input.name != null) {
String newName = input.name.trim();
if (newName.isEmpty()) {
throw new BadRequestException("name cannot be empty");
}
if (!newName.equals(labelType.getName())) {
if (config.getLabelSections().containsKey(newName)) {
throw new ResourceConflictException(String.format("name %s already in use", newName));
}
for (String labelName : config.getLabelSections().keySet()) {
if (labelName.equalsIgnoreCase(newName)) {
throw new ResourceConflictException(String.format("name %s conflicts with existing label %s", newName, labelName));
}
}
try {
LabelType.checkName(newName);
} catch (IllegalArgumentException e) {
throw new BadRequestException("invalid name: " + input.name, e);
}
labelTypeBuilder.setName(newName);
dirty = true;
}
}
if (input.description != null) {
String description = Strings.emptyToNull(input.description.trim());
labelTypeBuilder.setDescription(Optional.ofNullable(description));
dirty = true;
}
if (input.function != null) {
if (input.function.trim().isEmpty()) {
throw new BadRequestException("function cannot be empty");
}
labelTypeBuilder.setFunction(LabelDefinitionInputParser.parseFunction(input.function));
dirty = true;
}
if (input.values != null) {
if (input.values.isEmpty()) {
throw new BadRequestException("values cannot be empty");
}
labelTypeBuilder.setValues(LabelDefinitionInputParser.parseValues(input.values));
dirty = true;
}
if (input.defaultValue != null) {
labelTypeBuilder.setDefaultValue(LabelDefinitionInputParser.parseDefaultValue(labelTypeBuilder, input.defaultValue));
dirty = true;
}
if (input.branches != null) {
labelTypeBuilder.setRefPatterns(LabelDefinitionInputParser.parseBranches(input.branches));
dirty = true;
}
if (input.canOverride != null) {
labelTypeBuilder.setCanOverride(input.canOverride);
dirty = true;
}
input.copyCondition = Strings.emptyToNull(input.copyCondition);
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);
}
labelTypeBuilder.setCopyCondition(input.copyCondition);
dirty = true;
if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
throw new BadRequestException("can't set and unset copyCondition in the same request");
}
}
if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
labelTypeBuilder.setCopyCondition(null);
dirty = true;
}
if (input.copyAnyScore != null) {
labelTypeBuilder.setCopyAnyScore(input.copyAnyScore);
dirty = true;
}
if (input.copyMinScore != null) {
labelTypeBuilder.setCopyMinScore(input.copyMinScore);
dirty = true;
}
if (input.copyMaxScore != null) {
labelTypeBuilder.setCopyMaxScore(input.copyMaxScore);
dirty = true;
}
if (input.copyAllScoresIfListOfFilesDidNotChange != null) {
labelTypeBuilder.setCopyAllScoresIfListOfFilesDidNotChange(input.copyAllScoresIfListOfFilesDidNotChange);
dirty = true;
}
if (input.copyAllScoresIfNoChange != null) {
labelTypeBuilder.setCopyAllScoresIfNoChange(input.copyAllScoresIfNoChange);
dirty = true;
}
if (input.copyAllScoresIfNoCodeChange != null) {
labelTypeBuilder.setCopyAllScoresIfNoCodeChange(input.copyAllScoresIfNoCodeChange);
dirty = true;
}
if (input.copyAllScoresOnTrivialRebase != null) {
labelTypeBuilder.setCopyAllScoresOnTrivialRebase(input.copyAllScoresOnTrivialRebase);
dirty = true;
}
if (input.copyAllScoresOnMergeFirstParentUpdate != null) {
labelTypeBuilder.setCopyAllScoresOnMergeFirstParentUpdate(input.copyAllScoresOnMergeFirstParentUpdate);
dirty = true;
}
if (input.copyValues != null) {
labelTypeBuilder.setCopyValues(input.copyValues);
dirty = true;
}
if (input.allowPostSubmit != null) {
labelTypeBuilder.setAllowPostSubmit(input.allowPostSubmit);
dirty = true;
}
if (input.ignoreSelfApproval != null) {
labelTypeBuilder.setIgnoreSelfApproval(input.ignoreSelfApproval);
dirty = true;
}
config.getLabelSections().remove(labelType.getName());
config.upsertLabelType(labelTypeBuilder.build());
return dirty;
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class ApprovalInference method getForPatchSetWithoutNormalization.
private Collection<PatchSetApproval> getForPatchSetWithoutNormalization(ChangeNotes notes, ProjectState project, PatchSet patchSet, RevWalk rw, Config repoConfig) {
checkState(project.getNameKey().equals(notes.getProjectName()), "project must match %s, %s", project.getNameKey(), notes.getProjectName());
PatchSet.Id psId = patchSet.id();
// Add approvals on the given patch set to the result
Table<String, Account.Id, PatchSetApproval> resultByUser = HashBasedTable.create();
ImmutableList<PatchSetApproval> nonCopiedApprovalsForGivenPatchSet = notes.load().getApprovals().get(patchSet.id());
nonCopiedApprovalsForGivenPatchSet.forEach(psa -> resultByUser.put(psa.label(), psa.accountId(), psa));
// given patch set.
if (psId.get() == 1) {
return resultByUser.values();
}
Map.Entry<PatchSet.Id, PatchSet> priorPatchSet = notes.load().getPatchSets().lowerEntry(psId);
if (priorPatchSet == null) {
return resultByUser.values();
}
ImmutableList<PatchSetApproval> priorApprovalsIncludingCopied = notes.load().getApprovalsWithCopied().get(priorPatchSet.getKey());
// Add labels from the previous patch set to the result in case the label isn't already there
// and settings as well as change kind allow copying.
ChangeKind changeKind = changeKindCache.getChangeKind(project.getNameKey(), rw, repoConfig, priorPatchSet.getValue().commitId(), patchSet.commitId());
logger.atFine().log("change kind for patch set %d of change %d against prior patch set %s is %s", patchSet.id().get(), patchSet.id().changeId().get(), priorPatchSet.getValue().id().changeId(), changeKind);
Map<String, ModifiedFile> baseVsCurrent = null;
Map<String, ModifiedFile> baseVsPrior = null;
Map<String, ModifiedFile> priorVsCurrent = null;
LabelTypes labelTypes = project.getLabelTypes();
for (PatchSetApproval psa : priorApprovalsIncludingCopied) {
if (resultByUser.contains(psa.label(), psa.accountId())) {
continue;
}
Optional<LabelType> type = labelTypes.byLabel(psa.labelId());
// Only compute modified files if there is a relevant label, since this is expensive.
if (baseVsCurrent == null && type.isPresent() && type.get().isCopyAllScoresIfListOfFilesDidNotChange()) {
baseVsCurrent = listModifiedFiles(project, patchSet, rw, repoConfig);
baseVsPrior = listModifiedFiles(project, priorPatchSet.getValue(), rw, repoConfig);
priorVsCurrent = listModifiedFiles(project, priorPatchSet.getValue().commitId(), patchSet.commitId(), rw, repoConfig);
}
if (!type.isPresent()) {
logger.atFine().log("approval %d on label %s of patch set %d of change %d cannot be copied" + " to patch set %d because the label no longer exists on project %s", psa.value(), psa.label(), psa.key().patchSetId().get(), psa.key().patchSetId().changeId().get(), psId.get(), project.getName());
continue;
}
if (!canCopyBasedOnBooleanLabelConfigs(project, psa, patchSet.id(), changeKind, type.get(), baseVsCurrent, baseVsPrior, priorVsCurrent) && !canCopyBasedOnCopyCondition(notes, psa, patchSet, type.get(), changeKind, rw, repoConfig)) {
continue;
}
resultByUser.put(psa.label(), psa.accountId(), psa.copyWithPatchSet(patchSet.id()));
}
return resultByUser.values();
}
use of com.google.gerrit.entities.LabelType in project gerrit by GerritCodeReview.
the class ApprovalsUtil method addReviewers.
private List<PatchSetApproval> addReviewers(ChangeUpdate update, LabelTypes labelTypes, Change change, PatchSet.Id psId, Account.Id authorId, Account.Id committerId, Iterable<Account.Id> wantReviewers, Collection<Account.Id> existingReviewers) {
List<LabelType> allTypes = labelTypes.getLabelTypes();
if (allTypes.isEmpty()) {
return ImmutableList.of();
}
Set<Account.Id> need = Sets.newLinkedHashSet(wantReviewers);
if (authorId != null && canSee(update.getNotes(), authorId)) {
need.add(authorId);
}
if (committerId != null && canSee(update.getNotes(), committerId)) {
need.add(committerId);
}
need.remove(change.getOwner());
need.removeAll(existingReviewers);
if (need.isEmpty()) {
return ImmutableList.of();
}
List<PatchSetApproval> cells = Lists.newArrayListWithCapacity(need.size());
LabelId labelId = Iterables.getLast(allTypes).getLabelId();
for (Account.Id account : need) {
cells.add(PatchSetApproval.builder().key(PatchSetApproval.key(psId, account, labelId)).value(0).granted(update.getWhen()).build());
update.putReviewer(account, REVIEWER);
}
return Collections.unmodifiableList(cells);
}
Aggregations