use of jetbrains.buildServer.vcs.VcsRootInstanceEntry in project teamcity-rest by JetBrains.
the class BuildRequest method setVcsLabel.
/**
* Adds a label to build VCS roots.
* @param buildLocator specifies build to label.
* @param vcsRootLocator optional, specifies a VCS root to put a label on. If not present, label will be applied to all VCS roots.
* @param fields specifies result representation
* @param labelValue text of the label.
* @return added labels.
*/
@POST
@Path("/{buildLocator}/vcsLabels")
@Consumes("text/plain")
@Produces({ "application/xml", "application/json" })
@ApiOperation(value = "Add a VCS label to the matching build.", nickname = "addBuildVcsLabel")
public VcsLabels setVcsLabel(@ApiParam(format = LocatorName.BUILD) @PathParam("buildLocator") String buildLocator, @ApiParam(format = LocatorName.VCS_ROOT_INSTANCE) @QueryParam("locator") String vcsRootLocator, @QueryParam("fields") String fields, String labelValue) {
if (StringUtil.isEmpty(labelValue)) {
throw new BadRequestException("Label can not empty.");
}
SBuild build = getBuild(myBuildFinder.getBuildPromotion(null, buildLocator));
if (build == null) {
throw new NotFoundException("Cannot find a build using locator: " + buildLocator);
}
VcsLabelManager labelManager = myBeanContext.getSingletonService(VcsLabelManager.class);
List<VcsRootInstance> roots;
if (vcsRootLocator == null) {
roots = build.getVcsRootEntries().stream().map(VcsRootInstanceEntry::getVcsRoot).collect(Collectors.toList());
} else {
VcsRootInstanceFinder rootInstanceFinder = myBeanContext.getSingletonService(VcsRootInstanceFinder.class);
roots = Collections.singletonList(rootInstanceFinder.getItem(vcsRootLocator));
}
try {
labelManager.setLabel(build, labelValue, roots);
} catch (VcsException e) {
LOG.warn("Couldn't set a vcs label.", e);
}
Fields returnFields = new Fields(fields);
return new VcsLabels(labelManager.getLabels(build).stream().filter(l -> l.getLabelText().equals(labelValue)).map(l -> new VcsLabel(l, returnFields, myBeanContext)).collect(Collectors.toList()), returnFields);
}
use of jetbrains.buildServer.vcs.VcsRootInstanceEntry 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