Search in sources :

Example 1 with AggregatingItemHolder

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;
}
Also used : LocatorProcessException(jetbrains.buildServer.server.rest.errors.LocatorProcessException) Filter(jetbrains.buildServer.util.filters.Filter) java.util(java.util) LocatorName(jetbrains.buildServer.server.rest.swagger.constants.LocatorName) DuplicateChecker(jetbrains.buildServer.server.rest.data.util.DuplicateChecker) LocatorResource(jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource) ComparatorDuplicateChecker(jetbrains.buildServer.server.rest.data.util.ComparatorDuplicateChecker) BuildTypeOrTemplate(jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BooleanUtils(org.apache.commons.lang3.BooleanUtils) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) LocatorDimensionDataType(jetbrains.buildServer.server.rest.swagger.constants.LocatorDimensionDataType) StringUtil(jetbrains.buildServer.util.StringUtil) ServiceLocator(jetbrains.buildServer.ServiceLocator) jetbrains.buildServer.serverSide(jetbrains.buildServer.serverSide) ItemProcessor(jetbrains.buildServer.util.ItemProcessor) ComparisonChain(com.google.common.collect.ComparisonChain) Collectors(java.util.stream.Collectors) AggregatingItemHolder(jetbrains.buildServer.server.rest.data.util.AggregatingItemHolder) Nullable(org.jetbrains.annotations.Nullable) Contract(org.jetbrains.annotations.Contract) Stream(java.util.stream.Stream) LocatorDimension(jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension) NotNull(org.jetbrains.annotations.NotNull) SUser(jetbrains.buildServer.users.SUser) SUser(jetbrains.buildServer.users.SUser) ServiceLocator(jetbrains.buildServer.ServiceLocator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) AggregatingItemHolder(jetbrains.buildServer.server.rest.data.util.AggregatingItemHolder) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ComparisonChain (com.google.common.collect.ComparisonChain)1 java.util (java.util)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 ServiceLocator (jetbrains.buildServer.ServiceLocator)1 AggregatingItemHolder (jetbrains.buildServer.server.rest.data.util.AggregatingItemHolder)1 ComparatorDuplicateChecker (jetbrains.buildServer.server.rest.data.util.ComparatorDuplicateChecker)1 DuplicateChecker (jetbrains.buildServer.server.rest.data.util.DuplicateChecker)1 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)1 LocatorProcessException (jetbrains.buildServer.server.rest.errors.LocatorProcessException)1 LocatorDimension (jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension)1 LocatorResource (jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource)1 LocatorDimensionDataType (jetbrains.buildServer.server.rest.swagger.constants.LocatorDimensionDataType)1 LocatorName (jetbrains.buildServer.server.rest.swagger.constants.LocatorName)1 BuildTypeOrTemplate (jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate)1 jetbrains.buildServer.serverSide (jetbrains.buildServer.serverSide)1 SUser (jetbrains.buildServer.users.SUser)1 ItemProcessor (jetbrains.buildServer.util.ItemProcessor)1 StringUtil (jetbrains.buildServer.util.StringUtil)1