use of jetbrains.buildServer.server.rest.data.util.AggregatingItemHolder in project teamcity-rest by JetBrains.
the class BranchFinder method getPrefilteredItemsInternal.
@NotNull
private ItemHolder<BranchData> getPrefilteredItemsInternal(@NotNull final Locator locator, boolean existenseCheck) {
String buildLocator = locator.getSingleDimensionValue(BUILD);
if (!StringUtil.isEmpty(buildLocator)) {
BuildPromotion build = myServiceLocator.getSingletonService(BuildPromotionFinder.class).getItem(buildLocator);
return getItemHolder(Collections.singleton(BranchData.fromBuild(build)));
}
final String buildTypeLocator = locator.getSingleDimensionValue(BUILD_TYPE);
if (buildTypeLocator == null) {
throw new BadRequestException("No '" + BUILD_TYPE + "' dimension is present but it is required for searching branches. Locator: '" + locator.getStringRepresentation() + "'");
}
final List<SBuildType> buildTypes = myBuildTypeFinder.getBuildTypes(null, buildTypeLocator);
AggregatingItemHolder<BranchData> result = new AggregatingItemHolder<>();
final String groupsInclude = locator.getSingleDimensionValue(GROUP_INCLUDE);
if (groupsInclude != null) {
SUser user = validateAndgetGroupIncludeUser(groupsInclude);
BranchGroupsService branchGroupsService = myServiceLocator.getSingletonService(BranchGroupsService.class);
result.add(FinderDataBinding.getItemHolder(buildTypes.stream().flatMap(buildType -> branchGroupsService.getAvailableBranchGroups(new BranchGroupsProvider.Context((BuildTypeEx) buildType, user)).stream()).distinct().map(branchGroup -> BranchData.fromBranchGroup(branchGroup))));
}
final String groupDimension = locator.getSingleDimensionValue(BRANCH_GROUP);
if (groupDimension != null) {
UserFinder userFinder = myServiceLocator.getSingletonService(UserFinder.class);
BranchGroupsService branchGroupsService = myServiceLocator.getSingletonService(BranchGroupsService.class);
Locator branchGroupLocator = new Locator(groupDimension, "id", "user", Locator.LOCATOR_SINGLE_VALUE_UNUSED_NAME);
String userLocator = branchGroupLocator.getSingleDimensionValue("user");
SUser user = userLocator == null ? userFinder.getCurrentUser() : userFinder.getItem(userLocator);
if (user == null) {
throw new BadRequestException("Can only filter by branch group when the user is present");
} else {
userFinder.checkViewUserPermission(user);
}
String branchGroupId = branchGroupLocator.isSingleValue() ? branchGroupLocator.getSingleValue() : branchGroupLocator.getSingleDimensionValue("id");
if (branchGroupId == null) {
throw new BadRequestException("Dimension '" + BRANCH_GROUP + "' does not specify 'id' subdimension. Example: " + branchGroupsService.getAvailableBranchGroups(new BranchGroupsProvider.Context((BuildTypeEx) buildTypes.get(0), user)).stream().map(branchGroup -> branchGroup.getId()).collect(Collectors.joining(", ")));
}
branchGroupLocator.checkLocatorFullyProcessed();
result.add(processor -> {
try {
buildTypes.forEach(buildType -> branchGroupsService.collectBranches(branchGroupId, new BranchGroupsProvider.Context((BuildTypeEx) buildType, user), item -> processor.processItem(BranchData.fromBranchEx(item, myServiceLocator, null, true))));
} catch (IllegalStateException e) {
throw new BadRequestException("Error retrieving branch groups: " + e.getMessage());
}
});
return result;
}
BranchSearchOptions searchOptions = getBranchSearchOptions(locator);
boolean lookingForDefaultBranch = BooleanUtils.isTrue(locator.getSingleDimensionValueAsBoolean(DEFAULT));
// i.e. loose relevant item.
if (lookingForDefaultBranch && existenseCheck && locator.getUnusedDimensions().isEmpty()) {
for (SBuildType buildType : buildTypes) {
final BranchData branch = detectDefaultBranch(buildType, searchOptions);
// As soon as default branch is found, create a simple processor and don't look into other buildTypes.
if (branch != null) {
result.add(processor -> processor.processItem(branch));
break;
}
}
} else {
locator.markUnused(DEFAULT);
Filter<SBuildType> dependenciesFilter = getBranchDependenciesFilter(buildTypes);
if (existenseCheck) {
// We don't use deduplication here as in this case we have a chance to stop earlier
// using lazy stream evaluation and avoid getting branches from all build types.
// We also skip computing timestamps as that is unnecessary for an existence check.
locator.markUsed(COMPUTE_TIMESTAMPS);
Stream<BranchData> branchStream = buildTypes.stream().flatMap(buildType -> getBranches(buildType, searchOptions, false, dependenciesFilter));
result.add(FinderDataBinding.getItemHolder(branchStream));
} else {
DeduplicatingAccumulator resultAccumulator = new DeduplicatingAccumulator();
for (SBuildType buildType : buildTypes) {
Boolean locatorComputeTimestamps = locator.getSingleDimensionValueAsBoolean(COMPUTE_TIMESTAMPS);
boolean finalComputeTimestamps = locatorComputeTimestamps != null ? locatorComputeTimestamps : TeamCityProperties.getBoolean("rest.beans.branch.defaultComputeTimestamp");
Stream<BranchData> branchStream = getBranches(buildType, searchOptions, finalComputeTimestamps, dependenciesFilter);
resultAccumulator.addAll(branchStream.collect(Collectors.toList()));
}
result.add(FinderDataBinding.getItemHolder(resultAccumulator.get()));
}
}
return result;
}
Aggregations