use of jetbrains.buildServer.parameters.impl.MapParametersProviderImpl in project teamcity-rest by JetBrains.
the class AgentFinder method getFilter.
@NotNull
@Override
public ItemFilter<SBuildAgent> getFilter(@NotNull final Locator locator) {
final MultiCheckerFilter<SBuildAgent> result = new MultiCheckerFilter<SBuildAgent>();
Long id = locator.getSingleDimensionValueAsLong(DIMENSION_ID);
if (id != null) {
result.add(item -> id == item.getId());
}
String name = locator.getSingleDimensionValue(NAME);
if (name != null) {
result.add(item -> name.equals(item.getName()));
}
final Boolean authorizedDimension = locator.getSingleDimensionValueAsBoolean(AUTHORIZED);
if (authorizedDimension != null) {
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
return FilterUtil.isIncludedByBooleanFilter(authorizedDimension, item.isAuthorized());
}
});
}
final Boolean enabledDimension = locator.getSingleDimensionValueAsBoolean(ENABLED);
if (enabledDimension != null) {
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
return FilterUtil.isIncludedByBooleanFilter(enabledDimension, item.isEnabled());
}
});
}
final Boolean connectedDimension = locator.getSingleDimensionValueAsBoolean(CONNECTED);
if (connectedDimension != null) {
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
return FilterUtil.isIncludedByBooleanFilter(connectedDimension, item.isRegistered());
}
});
}
// see also AgentPoolsFinder.getPoolAgents()
final String poolDimension = locator.getSingleDimensionValue(POOL);
if (poolDimension != null) {
AgentPoolFinder agentPoolFinder = myServiceLocator.getSingletonService(AgentPoolFinder.class);
// get id here to support not existing pools?
final AgentPool agentPool = agentPoolFinder.getItem(poolDimension);
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
// TeamCity API issue: cast
return ((BuildAgentEx) item).getAgentType().getAgentPoolId() == agentPool.getAgentPoolId();
}
});
}
if (locator.isUnused(BUILD)) {
final String buildDimension = locator.getSingleDimensionValue(BUILD);
if (buildDimension != null) {
Set<SBuildAgent> agents = getBuildRelatedAgents(buildDimension);
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
return agents.contains(item);
}
});
}
}
if (locator.isUnused(AGENT_TYPE_ID)) {
final String agentTypeLocator = locator.getSingleDimensionValue(AGENT_TYPE_ID);
if (agentTypeLocator != null) {
int agentTypeId = getAgentType(agentTypeLocator, myServiceLocator.getSingletonService(AgentTypeFinder.class)).getAgentTypeId();
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
return agentTypeId == item.getAgentTypeId();
}
});
}
}
final String ipDimension = locator.getSingleDimensionValue(IP);
if (ipDimension != null) {
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
// name of the field, not locator dimension
return ipDimension.equals(Agent.getFieldValue(item, "ip", myServiceLocator));
}
});
}
final String protocolDimension = locator.getSingleDimensionValue(PROTOCOL);
if (protocolDimension != null) {
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
// name of the field, not locator dimension
return protocolDimension.equals(Agent.getFieldValue(item, "protocol", myServiceLocator));
}
});
}
final String parameterDimension = locator.getSingleDimensionValue(PARAMETER);
if (parameterDimension != null) {
final ParameterCondition parameterCondition = ParameterCondition.create(parameterDimension);
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
return parameterCondition.matches(new MapParametersProviderImpl(item.getAvailableParameters()));
}
});
}
if (locator.isUnused(CLOUD_INSTANCE)) {
final String cloudInstanceLocator = locator.getSingleDimensionValue(CLOUD_INSTANCE);
if (cloudInstanceLocator != null) {
List<CloudInstance> instances = myServiceLocator.getSingletonService(CloudInstanceFinder.class).getItems(cloudInstanceLocator).myEntries.stream().map(CloudInstanceData::getInstance).collect(Collectors.toList());
result.add(a -> instances.stream().anyMatch(i -> i.containsAgent(a)));
/* CloudInstance might not have equals/hashcode, if it does, it would be better to use in a set like below
Set<CloudInstance> instances = myServiceLocator.getSingletonService(CloudInstanceFinder.class).getItems(cloudInstanceLocator).myEntries.stream().map(i -> i.getInstance()).collect(
Collectors.toSet());
CloudManager cloudManager = myServiceLocator.getSingletonService(CloudManager.class);
result.add(a -> Util.resolveNull(cloudManager.findInstanceByAgent(a), pair -> instances.contains(pair.getSecond()), false));
*/
}
}
if (locator.isUnused(COMPATIBLE)) {
// compatible with at least with one of the buildTypes
final String compatible = locator.getSingleDimensionValue(COMPATIBLE);
if (compatible != null) {
final CompatibleLocatorParseResult compatibleData = getBuildTypesFromCompatibleDimension(compatible);
if (compatibleData.buildTypes != null) {
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
return isCompatibleWithAny(item, compatibleData.buildTypes);
}
});
} else {
assert compatibleData.buildPromotions != null;
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
return isCompatibleWithAnyBuild(item, compatibleData.buildPromotions);
}
});
}
}
}
// incompatible with at least with one of the buildTypes
final String incompatible = locator.getSingleDimensionValue(INCOMPATIBLE);
if (incompatible != null) {
final CompatibleLocatorParseResult compatibleData = getBuildTypesFromCompatibleDimension(incompatible);
if (compatibleData.buildTypes != null) {
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
return !isCompatibleWithAll(item, compatibleData.buildTypes);
}
});
} else {
result.add(new FilterConditionChecker<SBuildAgent>() {
public boolean isIncluded(@NotNull final SBuildAgent item) {
assert compatibleData.buildPromotions != null;
return !isCompatibleWithAllBuild(item, compatibleData.buildPromotions);
}
});
}
}
return result;
}
use of jetbrains.buildServer.parameters.impl.MapParametersProviderImpl in project teamcity-rest by JetBrains.
the class BuildTypeFinder method getFilter.
@NotNull
@Override
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(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
return 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).myEntries;
if (projects.size() == 1) {
final SProject internalProject = projects.iterator().next();
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
return internalProject.getProjectId().equals(item.getProject().getProjectId());
}
});
} else {
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
return projects.contains(item.getProject());
}
});
}
}
}
final String affectedProjectDimension = locator.getSingleDimensionValue(AFFECTED_PROJECT);
if (affectedProjectDimension != null) {
@NotNull final SProject parentProject = myProjectFinder.getItem(affectedProjectDimension);
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
return ProjectFinder.isSameOrParent(parentProject, item.getProject());
}
});
}
final Boolean paused = locator.getSingleDimensionValueAsBoolean(PAUSED);
if (paused != null) {
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate 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).myEntries;
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()));
}
}
// experimental
final String compatibleAagentLocator = locator.getSingleDimensionValue(COMPATIBLE_AGENT);
if (compatibleAagentLocator != null) {
final List<SBuildAgent> agents = myAgentFinder.getItems(compatibleAagentLocator).myEntries;
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
if (item.getBuildType() == null)
return false;
for (SBuildAgent agent : agents) {
if (AgentFinder.canActuallyRun(agent, item.getBuildType()))
return true;
}
return false;
}
});
}
// experimental
final Long compatibleAgentsCount = locator.getSingleDimensionValueAsLong(COMPATIBLE_AGENTS_COUNT);
if (compatibleAgentsCount != null) {
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
if (item.getBuildType() == null)
return false;
long count = 0;
for (SBuildAgent agent : myAgentFinder.getItems(null).myEntries) {
// 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(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate 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(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
return 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)));
}
// experimental
final String filterBuilds = locator.getSingleDimensionValue(FILTER_BUILDS);
if (filterBuilds != null) {
BuildPromotionFinder promotionFinder = myServiceLocator.getSingletonService(BuildPromotionFinder.class);
FinderSearchMatcher<BuildPromotion> matcher = new FinderSearchMatcher<>(filterBuilds, promotionFinder);
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
@Override
public boolean isIncluded(@NotNull final BuildTypeOrTemplate 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<BuildTypeOrTemplate>(this, new SnapshotDepsTraverser(myPermissionChecker));
final List<BuildTypeOrTemplate> boundingList = graphFinder.getItems(snapshotDependencies).myEntries;
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
return boundingList.contains(item);
}
});
}
final String artifactDependencies = locator.getSingleDimensionValue(ARTIFACT_DEPENDENCY);
if (artifactDependencies != null) {
final GraphFinder<BuildTypeOrTemplate> graphFinder = new GraphFinder<BuildTypeOrTemplate>(this, new ArtifactDepsTraverser(myPermissionChecker));
final List<BuildTypeOrTemplate> boundingList = graphFinder.getItems(artifactDependencies).myEntries;
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
return boundingList.contains(item);
}
});
}
final String templateLocator = locator.getSingleDimensionValue(TEMPLATE_DIMENSION_NAME);
if (templateLocator != null) {
try {
// only this can throw exceptions caught later
final BuildTypeTemplate buildTemplate = getBuildTemplate(null, templateLocator, true);
final List<BuildTypeOrTemplate> boundingList = BuildTypes.fromBuildTypes(buildTemplate.getUsages());
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
return 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(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
return 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<SVcsRoot>(myServiceLocator.getSingletonService(VcsRootFinder.class).getItems(vcsRoot).myEntries);
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate 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<jetbrains.buildServer.vcs.VcsRootInstance>(myServiceLocator.getSingletonService(VcsRootInstanceFinder.class).getItems(vcsRootInstance).myEntries);
result.add(new FilterConditionChecker<BuildTypeOrTemplate>() {
public boolean isIncluded(@NotNull final BuildTypeOrTemplate item) {
for (VcsRootInstanceEntry vcsRootInstanceEntry : item.getVcsRootInstanceEntries()) {
if (vcsRootInstances.contains(vcsRootInstanceEntry.getVcsRoot()))
return true;
}
return false;
}
});
}
}
return result;
}
Aggregations