public ItemFilter getFilter()

in rest-api/src/jetbrains/buildServer/server/rest/data/finder/impl/BuildTypeFinder.java [263:476]


  public ItemFilter<BuildTypeOrTemplate> getFilter(@NotNull final Locator locator) {
    final MultiCheckerFilter<BuildTypeOrTemplate> result = new MultiCheckerFilter<BuildTypeOrTemplate>();

    if (locator.isUnused(DIMENSION_ID)) {
      final String id = locator.getSingleDimensionValue(DIMENSION_ID);
      if (id != null) {
        result.add(item -> id.equals(item.getId()));
      }
    }
    if (locator.isUnused(DIMENSION_INTERNAL_ID)) {
      final String internalId = locator.getSingleDimensionValue(DIMENSION_INTERNAL_ID);
      if (internalId != null) {
        result.add(item -> internalId.equals(item.getInternalId()));
      }
    }
    if (locator.isUnused(DIMENSION_UUID)) {
      final String uuid = locator.getSingleDimensionValue(DIMENSION_UUID);
      if (uuid != null) {
        result.add(item -> uuid.equals(((BuildTypeIdentityEx)item.getIdentity()).getEntityId().getConfigId()));
      }
    }

    final String name = locator.getSingleDimensionValue(DIMENSION_NAME);
    if (name != null) {
      result.add(item -> name.equalsIgnoreCase(item.getName()));
    }

    if (locator.isUnused(DIMENSION_PROJECT)) {
      final String projectLocator = locator.getSingleDimensionValue(DIMENSION_PROJECT);
      if (projectLocator != null) {
        final List<SProject> projects = myProjectFinder.getItems(projectLocator).getEntries();
        if (projects.size() == 1) {
          final SProject internalProject = projects.iterator().next();
          result.add(item -> internalProject.getProjectId().equals(item.getProject().getProjectId()));
        } else {
          result.add(item -> projects.contains(item.getProject()));
        }
      }
    }

    final String affectedProjectDimension = locator.getSingleDimensionValue(AFFECTED_PROJECT);
    if (affectedProjectDimension != null) {
      @NotNull final SProject parentProject = myProjectFinder.getItem(affectedProjectDimension);
      result.add(item -> ProjectFinder.isSameOrParent(parentProject, item.getProject()));
    }

    final Boolean paused = locator.getSingleDimensionValueAsBoolean(PAUSED);
    if (paused != null) {
      result.add(item -> {
          final Boolean pausedState = item.isPaused();
          return FilterUtil.isIncludedByBooleanFilter(paused, pausedState != null && pausedState);
      });
    }

    if (locator.isUnused(BUILD)) {
      String buildLocator = locator.getSingleDimensionValue(BUILD);
      if (buildLocator != null) {
        List<BuildPromotion> builds = myServiceLocator.getSingletonService(BuildPromotionFinder.class).getItems(buildLocator).getEntries();
        Set<String> buldTypeIds = builds.stream().map(build -> build.getBuildType()).filter(Objects::nonNull).map(buildType -> buildType.getInternalId()).collect(Collectors.toSet());
        result.add(item -> buldTypeIds.contains(item.getInternalId()));
      }
    }

    final String compatibleAagentLocator = locator.getSingleDimensionValue(COMPATIBLE_AGENT); //experimental
    if (compatibleAagentLocator != null) {
      final List<SBuildAgent> agents = myAgentFinder.getItems(compatibleAagentLocator).getEntries();
      result.add(item -> {
          if (item.getBuildType() == null) return false;
          for (SBuildAgent agent : agents) {
            if (AgentFinder.canActuallyRun(agent, item.getBuildType())) return true;
          }
          return false;
      });
    }

    final Long compatibleAgentsCount = locator.getSingleDimensionValueAsLong(COMPATIBLE_AGENTS_COUNT); //experimental
    if (compatibleAgentsCount != null) {
      result.add(item -> {
          if (item.getBuildType() == null) return false;
          long count = 0;
          for (SBuildAgent agent : myAgentFinder.getItems(null).getEntries()) { //or should process unauthorized as well?
            if (AgentFinder.canActuallyRun(agent, item.getBuildType()) && agent.isRegistered() && agent.isAuthorized() && agent.isEnabled()) count++;
            if (count > compatibleAgentsCount) return false;
          }
          return count == compatibleAgentsCount;
      });
    }

    if (locator.isUnused(DIMENSION_SELECTED)) {
      final String selectedByUser = locator.getSingleDimensionValue(DIMENSION_SELECTED);
      if (selectedByUser != null) {
        List<BuildTypeOrTemplate> filterSet = getSelectedByUser(locator, selectedByUser);
        result.add(item -> filterSet.contains(item));
      }
    }

    final String parameterDimension = locator.getSingleDimensionValue(PARAMETER);
    if (parameterDimension != null) {
      final ParameterCondition parameterCondition = ParameterCondition.create(parameterDimension);
      result.add(item -> {
          final boolean canView = !BuildType.shouldRestrictSettingsViewing(item.get(), myPermissionChecker);
          if (!canView) {
            LOG.debug("While filtering build types by " + PARAMETER + " user does not have enough permissions to see settings. Excluding build type: " + item.describe(false));
            return false;
          }
          return parameterCondition.matches(item.get());
      });
    }

    final String settingDimension = locator.getSingleDimensionValue(SETTING);
    if (settingDimension != null) {
      final ParameterCondition condition = ParameterCondition.create(settingDimension);
      result.add(item -> {
        final boolean canView = !BuildType.shouldRestrictSettingsViewing(item.get(), myPermissionChecker);
        if (!canView) {
          LOG.debug("While filtering build types by " + SETTING + " user does not have enough permissions to see settings. Excluding build type: " + item.describe(false));
          return false;
        }
        return condition.matches(new MapParametersProviderImpl(BuildTypeUtil.getSettingsParameters(item, null, true, false)),
                                 new MapParametersProviderImpl(BuildTypeUtil.getSettingsParameters(item, null, true, false)));
      });
    }

    final Boolean template = locator.getSingleDimensionValueAsBoolean(TEMPLATE_FLAG_DIMENSION_NAME);
    if (template != null) {
      result.add(item -> FilterUtil.isIncludedByBooleanFilter(template, item.isTemplate()));
    }

    final String type = locator.getSingleDimensionValue(TYPE);
    if (type != null) {
      String typeOptionValue = TypedFinderBuilder.getEnumValue(type, BuildTypeOptions.BuildConfigurationType.class).name();
      result.add(item -> typeOptionValue.equals(item.get().getOption(BuildTypeOptions.BT_BUILD_CONFIGURATION_TYPE)));
    }

    final String filterBuilds = locator.getSingleDimensionValue(FILTER_BUILDS); //experimental
    if (filterBuilds != null) {
      BuildPromotionFinder promotionFinder = myServiceLocator.getSingletonService(BuildPromotionFinder.class);
      FinderSearchMatcher<BuildPromotion> matcher = new FinderSearchMatcher<>(filterBuilds, promotionFinder);
      result.add(item -> {
          SBuildType buildType = item.getBuildType();
          if (buildType == null) return false;
          String defaults = Locator.getStringLocator(BuildPromotionFinder.BUILD_TYPE, BuildTypeFinder.getLocator(buildType), PagerData.COUNT, "1");
          return matcher.matches(defaults);
      });
    }

    final String snapshotDependencies = locator.getSingleDimensionValue(SNAPSHOT_DEPENDENCY);
    if (snapshotDependencies != null) {
      final GraphFinder<BuildTypeOrTemplate> graphFinder = new GraphFinder<>(this, new SnapshotDepsTraverser(myPermissionChecker));
      final List<BuildTypeOrTemplate> boundingList = graphFinder.getItems(snapshotDependencies).getEntries();
      result.add(item -> boundingList.contains(item));
    }

    final String artifactDependencies = locator.getSingleDimensionValue(ARTIFACT_DEPENDENCY);
    if (artifactDependencies != null) {
      final GraphFinder<BuildTypeOrTemplate> graphFinder = new GraphFinder<>(this, new ArtifactDepsTraverser(myPermissionChecker));
      final List<BuildTypeOrTemplate> boundingList = graphFinder.getItems(artifactDependencies).getEntries();
      result.add(item -> boundingList.contains(item));
    }

    final String templateLocator = locator.getSingleDimensionValue(TEMPLATE_DIMENSION_NAME);
    if (templateLocator != null) {
      try {
        final BuildTypeTemplate buildTemplate = getBuildTemplate(null, templateLocator, true); //only this can throw exceptions caught later
        final List<BuildTypeOrTemplate> boundingList = BuildTypes.fromBuildTypes(buildTemplate.getUsages());
        result.add(item -> boundingList.contains(item));
      } catch (NotFoundException e) {
        //legacy support for boolean template
        Boolean legacyTemplateFlag = null;
        try {
          legacyTemplateFlag = locator.getSingleDimensionValueAsBoolean(TEMPLATE_DIMENSION_NAME);
        } catch (LocatorProcessException eNested) {
          //not a boolean, throw original error
          throw new NotFoundException("No templates found by locator '" + templateLocator + "' specified in '" + TEMPLATE_DIMENSION_NAME + "' dimension : " + e.getMessage());
        }
        //legacy request detected
        if (legacyTemplateFlag != null) {
          final boolean legacyTemplateFlagFinal = legacyTemplateFlag;
          result.add(item -> FilterUtil.isIncludedByBooleanFilter(legacyTemplateFlagFinal, item.isTemplate()));
        }
      } catch (BadRequestException e) {
        throw new BadRequestException(
          "Error while searching for templates by locator '" + templateLocator + "' specified in '" + TEMPLATE_DIMENSION_NAME + "' dimension : " + e.getMessage(), e);
      }
    }

    if (locator.isUnused(VCS_ROOT_DIMENSION)) {
      final String vcsRoot = locator.getSingleDimensionValue(VCS_ROOT_DIMENSION);
      if (vcsRoot != null) {
        final Set<SVcsRoot> vcsRoots = new HashSet<>(myServiceLocator.getSingletonService(VcsRootFinder.class).getItems(vcsRoot).getEntries());
        result.add(item -> {
            for (VcsRootInstanceEntry vcsRootInstanceEntry : item.getVcsRootInstanceEntries()) {
              if (vcsRoots.contains(vcsRootInstanceEntry.getVcsRoot().getParent())) return true;
            }
            return false;
        });
      }
    }

    if (locator.isUnused(VCS_ROOT_INSTANCE_DIMENSION)) {
      final String vcsRootInstance = locator.getSingleDimensionValue(VCS_ROOT_INSTANCE_DIMENSION);
      if (vcsRootInstance != null) {
        final Set<jetbrains.buildServer.vcs.VcsRootInstance> vcsRootInstances =
          new HashSet<>(myServiceLocator.getSingletonService(VcsRootInstanceFinder.class).getItems(vcsRootInstance).getEntries());
        result.add(item -> {
            for (VcsRootInstanceEntry vcsRootInstanceEntry : item.getVcsRootInstanceEntries()) {
              if (vcsRootInstances.contains(vcsRootInstanceEntry.getVcsRoot())) return true;
            }
            return false;
        });
      }
    }
    return result.toItemFilter();
  }