use of jetbrains.buildServer.serverSide.metadata.BuildMetadataEntry in project teamcity-rest by JetBrains.
the class Build method getMetadata.
@XmlElement
public NamedDatas getMetadata() {
if (myBuild == null) {
return null;
} else {
return ValueWithDefault.decideDefaultIgnoringAccessDenied(myFields.isIncluded("metadata", false, false), () -> {
checkCanViewRuntimeData();
HashMap<String, Map<String, String>> result = new HashMap<>();
MetadataStorageEx metadataStorage = myServiceLocator.getSingletonService(MetadataStorageEx.class);
for (String providerId : metadataStorage.getProviderIds()) {
Iterator<BuildMetadataEntry> metadataEntryIterator = metadataStorage.getBuildEntry(myBuild.getBuildId(), providerId);
while (metadataEntryIterator.hasNext()) {
BuildMetadataEntry metadataEntry = metadataEntryIterator.next();
HashMap<String, String> properties = new HashMap<>(metadataEntry.getMetadata());
if (properties.get(".providerId") == null) {
properties.put(".providerId", providerId);
} else {
// assume clash here does not happen
properties.put(".teamcity.rest.providerId", providerId);
}
if (properties.get(".key") == null) {
properties.put(".key", metadataEntry.getKey());
} else {
// assume clash here does not happen
properties.put(".teamcity.rest.key", metadataEntry.getKey());
}
result.put(providerId + "_" + metadataEntry.getKey(), properties);
}
}
return new NamedDatas(result, myFields.getNestedField("metadata"));
});
}
}
use of jetbrains.buildServer.serverSide.metadata.BuildMetadataEntry in project teamcity-rest by JetBrains.
the class BuildPromotionFinder method getFilter.
@NotNull
@Override
public ItemFilter<BuildPromotion> getFilter(@NotNull final Locator locator) {
final MultiCheckerFilter<BuildPromotion> result = new MultiCheckerFilter<BuildPromotion>();
// checking permissions to view - workaround for TW-45544
result.add(item -> {
try {
ensureCanView(item);
return true;
} catch (AccessDeniedException e) {
// excluding from the lists as secure wrappers usually do
return false;
}
});
if (locator.isSingleValue()) {
try {
long foundPromotionId = getBuildPromotionById(locator.getSingleValueAsLong(), myBuildPromotionManager, myBuildsManager).getId();
result.add(item -> foundPromotionId == item.getId());
} catch (NotFoundException e) {
result.add(item -> false);
}
}
if (locator.isUnused(DEFAULT_FILTERING)) {
// basically, mark as used if it is not yet processed, but is unset or is set to false
final Boolean defaultFiltering = locator.getSingleDimensionValueAsBoolean(DEFAULT_FILTERING);
if (defaultFiltering != null && defaultFiltering) {
locator.markUnused(DEFAULT_FILTERING);
}
}
final Long id = locator.getSingleDimensionValueAsLong(DIMENSION_ID);
if (id != null) {
try {
long foundPromotionId = getBuildPromotionById(id, myBuildPromotionManager, myBuildsManager).getId();
result.add(item -> foundPromotionId == item.getId());
} catch (NotFoundException e) {
result.add(item -> false);
}
}
final Long promotionId = locator.getSingleDimensionValueAsLong(PROMOTION_ID);
if (promotionId != null) {
try {
long foundPromotionId = BuildFinder.getBuildPromotion(promotionId, myBuildPromotionManager).getId();
result.add(item -> foundPromotionId == item.getId());
} catch (NotFoundException e) {
result.add(item -> false);
}
}
final Long promotionIdAlias = locator.getSingleDimensionValueAsLong(PROMOTION_ID_ALIAS);
if (promotionIdAlias != null) {
try {
long foundPromotionId = BuildFinder.getBuildPromotion(promotionIdAlias, myBuildPromotionManager).getId();
result.add(item -> foundPromotionId == item.getId());
} catch (NotFoundException e) {
result.add(item -> false);
}
}
final Long buildId = locator.getSingleDimensionValueAsLong(BUILD_ID);
if (buildId != null) {
result.add(item -> buildId.equals(item.getAssociatedBuildId()));
}
Locator stateLocator = getStateLocator(locator);
if (!isStateIncluded(stateLocator, STATE_QUEUED)) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return item.getQueuedBuild() == null;
}
});
}
if (!isStateIncluded(stateLocator, STATE_RUNNING)) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
final SBuild associatedBuild = item.getAssociatedBuild();
return associatedBuild == null || associatedBuild.isFinished();
}
});
}
if (!isStateIncluded(stateLocator, STATE_FINISHED)) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
final SBuild associatedBuild = item.getAssociatedBuild();
return associatedBuild == null || !associatedBuild.isFinished();
}
});
}
final Boolean composite = locator.getSingleDimensionValueAsBoolean(COMPOSITE);
if (composite != null) {
result.add(item -> FilterUtil.isIncludedByBooleanFilter(composite, item.isCompositeBuild()));
}
if (locator.isUnused(PROJECT)) {
SProject project = getProjectFromDimension(locator, PROJECT);
if (project != null) {
result.add(item -> {
final SBuildType buildType = item.getBuildType();
return buildType != null && project.equals(buildType.getProject());
});
}
}
if (locator.isUnused(AFFECTED_PROJECT)) {
SProject affectedProject = getProjectFromDimension(locator, AFFECTED_PROJECT);
if (affectedProject != null && !affectedProject.isRootProject()) {
result.add(item -> {
final SBuildType buildType = item.getBuildType();
return buildType != null && ProjectFinder.isSameOrParent(affectedProject, buildType.getProject());
});
}
}
if (locator.isUnused(BUILD_TYPE)) {
final String buildTypeLocator = locator.getSingleDimensionValue(BUILD_TYPE);
if (buildTypeLocator != null) {
final Set<SBuildType> buildTypes = new HashSet<>(myBuildTypeFinder.getBuildTypes(getProjectFromDimension(locator, PROJECT), buildTypeLocator));
if (buildTypes.isEmpty()) {
throw new NotFoundException("No build types found for locator '" + buildTypeLocator + "'");
}
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return buildTypes.contains(item.getParentBuildType());
}
});
}
}
final String branchLocatorValue = locator.getSingleDimensionValue(BRANCH);
if (branchLocatorValue != null) {
PagedSearchResult<? extends Branch> branches = null;
BadRequestException exceptinoOnFullSearch = null;
try {
branches = myBranchFinder.getItemsIfValidBranchListLocator(locator.lookupSingleDimensionValue(BUILD_TYPE), branchLocatorValue);
} catch (BadRequestException e) {
// not a valid branches listing locator
exceptinoOnFullSearch = e;
}
if (branches != null) {
// branches found - use them
Set<String> branchNames = getBranchNamesSet(branches.myEntries);
Set<String> branchDisplayNames = getBranchDisplayNamesSet(branches.myEntries);
boolean defaultBranchIncluded = branches.myEntries.stream().anyMatch(Branch::isDefaultBranch);
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
final Branch buildBranch = BranchData.fromBuild(item);
return (defaultBranchIncluded && buildBranch.isDefaultBranch()) || branchNames.contains(buildBranch.getName()) || branchDisplayNames.contains(buildBranch.getDisplayName());
}
});
} else {
// branches not found by locator - try to use filter
BranchFinder.BranchFilterDetails branchFilterDetails;
try {
branchFilterDetails = myBranchFinder.getBranchFilterDetails(branchLocatorValue);
} catch (LocatorProcessException locatorException) {
throw new BadRequestException("Invalid sub-locator '" + BRANCH + "': Cannot" + (exceptinoOnFullSearch != null ? " find branches: " + exceptinoOnFullSearch.getMessage() + "; and cannot " : "") + " create filter: " + locatorException.getMessage(), locatorException);
}
if (!branchFilterDetails.isAnyBranch()) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return branchFilterDetails.isIncluded(item);
}
});
}
}
}
final Boolean branched = locator.getSingleDimensionValueAsBoolean(BRANCHED);
if (branched != null) {
result.add(item -> FilterUtil.isIncludedByBooleanFilter(branched, item.getBranch() != null));
}
if (locator.isUnused(AGENT)) {
final String agentLocator = locator.getSingleDimensionValue(AGENT);
if (agentLocator != null) {
Set<Integer> agentIds = myAgentFinder.getItemsNotEmpty(agentLocator).myEntries.stream().map(agent -> agent.getId()).filter(i -> i != Agent.UNKNOWN_AGENT_ID).collect(Collectors.toSet());
result.add(item -> {
// for queued build using compatible agents
final SQueuedBuild queuedBuild = item.getQueuedBuild();
if (queuedBuild != null) {
return queuedBuild.getCanRunOnAgents().stream().anyMatch(agent -> agentIds.contains(agent.getId()));
}
final SBuild build = item.getAssociatedBuild();
if (build != null) {
return agentIds.contains(build.getAgent().getId());
}
return false;
});
}
}
// compatibility support
final String tags = locator.getSingleDimensionValue(TAGS);
if (tags != null) {
final List<String> tagsList = Arrays.asList(tags.split(","));
if (tagsList.size() > 0) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return item.getTags().containsAll(tagsList);
}
});
}
}
if (locator.isUnused(TAG)) {
result.add(getFilterByTag(locator.getDimensionValue(TAG)));
}
final String compatibleAgentLocator = locator.getSingleDimensionValue(COMPATIBLE_AGENT);
if (compatibleAgentLocator != null) {
List<SBuildAgent> agents = myAgentFinder.getItems(compatibleAgentLocator).myEntries;
result.add(build -> agents.stream().anyMatch(agent -> myAgentFinder.canActuallyRun(agent, build)));
}
// experimental, only for queued builds
final Long compatibleAgentsCount = locator.getSingleDimensionValueAsLong(COMPATIBLE_AGENTS_COUNT);
if (compatibleAgentsCount != null) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
long count = 0;
for (SBuildAgent agent : myAgentFinder.getItems(null).myEntries) {
// or should process unauthorized as well?
if (myAgentFinder.canActuallyRun(agent, item))
count++;
if (count > compatibleAgentsCount)
return false;
}
return count == compatibleAgentsCount;
}
});
}
final Boolean personal = locator.getSingleDimensionValueAsBoolean(PERSONAL);
if (personal != null) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return FilterUtil.isIncludedByBooleanFilter(personal, item.isPersonal());
}
});
}
final Boolean history = locator.getSingleDimensionValueAsBoolean(HISTORY);
if (history != null) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return FilterUtil.isIncludedByBooleanFilter(history, item.isOutOfChangesSequence());
}
});
}
final String userDimension = locator.getSingleDimensionValue(USER);
if (userDimension != null) {
final SUser user = myUserFinder.getItem(userDimension);
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
SUser owner = item.getOwner();
if (owner != null) {
// if owner is present, consider only it: this is consistent with the builds search in the prefiltering
return user.getId() == owner.getId();
}
SUser actualUser = Util.resolveNull(getTriggeredBy(item), TriggeredBy::getUser);
return actualUser != null && user.getId() == actualUser.getId();
}
});
}
final String triggeredDimension = locator.getSingleDimensionValue(TRIGGERED);
if (triggeredDimension != null) {
final ItemFilter<TriggeredBy> filter = myTriggerByFinder.getFilter(triggeredDimension);
result.add(item -> Util.resolveNull(getTriggeredBy(item), filter::isIncluded, false));
}
final List<String> properties = locator.getDimensionValue(PROPERTY);
if (!properties.isEmpty()) {
final Matcher<ParametersProvider> parameterCondition = ParameterCondition.create(properties);
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
if (!Build.canViewRuntimeData(myPermissionChecker, item))
return false;
// TeamCity open API issue
return parameterCondition.matches(Build.getBuildResultingParameters(item, myServiceLocator));
}
});
}
if (locator.getUnusedDimensions().contains(SNAPSHOT_DEP)) {
// performance optimization: do not filter if already processed
final String snapshotDepDimension = locator.getSingleDimensionValue(SNAPSHOT_DEP);
if (snapshotDepDimension != null) {
final Set<BuildPromotion> snapshotRelatedBuilds = new HashSet<>(getSnapshotRelatedBuilds(snapshotDepDimension));
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return snapshotRelatedBuilds.contains(item);
}
});
}
}
if (locator.getUnusedDimensions().contains(ARTIFACT_DEP)) {
// performance optimization: do not filter if already processed
final String artifactDepDimension = locator.getSingleDimensionValue(ARTIFACT_DEP);
if (artifactDepDimension != null) {
final Set<BuildPromotion> artifactRelatedBuilds = new HashSet<>(getArtifactRelatedBuilds(artifactDepDimension, locator));
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return artifactRelatedBuilds.contains(item);
}
});
}
}
if (locator.isUnused(SNAPSHOT_PROBLEM)) {
final String snapshotDepProblem = locator.getSingleDimensionValue(SNAPSHOT_PROBLEM);
if (snapshotDepProblem != null) {
final Set<BuildPromotion> snapshotDepProblemBuilds = new HashSet<>(getSnapshotDepProblemBuilds(snapshotDepProblem));
result.add(item -> snapshotDepProblemBuilds.contains(item));
}
}
if (locator.getUnusedDimensions().contains(EQUIVALENT)) {
// performance optimization: do not filter if already processed
final String equivalent = locator.getSingleDimensionValue(EQUIVALENT);
if (equivalent != null) {
final Set<BuildPromotion> filter = new HashSet<BuildPromotion>(((BuildPromotionEx) getItem(equivalent)).getStartedEquivalentPromotions(-1));
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return filter.contains(item);
}
});
}
}
if (locator.getUnusedDimensions().contains(METADATA)) {
// performance optimization: do not filter if already processed
final String metadata = locator.getSingleDimensionValue(METADATA);
if (metadata != null) {
final Iterator<BuildMetadataEntry> metadataEntries = getBuildMetadataEntryIterator(metadata);
final Set<Long> buildIds = new HashSet<Long>();
while (metadataEntries.hasNext()) {
BuildMetadataEntry metadataEntry = metadataEntries.next();
buildIds.add(metadataEntry.getBuildId());
}
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
if (!Build.canViewRuntimeData(myPermissionChecker, item))
return false;
return buildIds.contains(item.getAssociatedBuildId());
}
});
}
}
if (locator.getUnusedDimensions().contains(ORDERED)) {
// performance optimization: do not filter if already processed
final String graphLocator = locator.getSingleDimensionValue(ORDERED);
if (graphLocator != null) {
final GraphFinder<BuildPromotion> graphFinder = new BuildPromotionOrderedFinder(BuildPromotionFinder.this);
final Set<BuildPromotion> filter = new HashSet<BuildPromotion>(graphFinder.getItems(graphLocator).myEntries);
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return filter.contains(item);
}
});
}
}
final String sinceBuildDimension = locator.getSingleDimensionValue(SINCE_BUILD);
BuildPromotion sinceBuildPromotion = null;
Long sinceBuildId = null;
if (sinceBuildDimension != null) {
try {
sinceBuildPromotion = getItem(sinceBuildDimension);
final SQueuedBuild queuedBuild = sinceBuildPromotion.getQueuedBuild();
if (queuedBuild != null) {
// compare queued builds by id (triggering sequence)
final long buildPromotionId = getBuildId(sinceBuildPromotion);
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return buildPromotionId < getBuildId(item);
}
});
} else {
// for started build, compare by start time
final SBuild limitingBuild = sinceBuildPromotion.getAssociatedBuild();
if (limitingBuild != null) {
final Date startDate = limitingBuild.getStartDate();
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
final SBuild build = item.getAssociatedBuild();
if (build == null)
return true;
if (startDate.equals(build.getStartDate()) && limitingBuild.getBuildId() != build.getBuildId())
return true;
return startDate.before(build.getStartDate());
}
});
}
}
} catch (NotFoundException e) {
// build not found by sinceBuild locator, extract id ad filter using it
sinceBuildId = getBuildId(sinceBuildDimension);
final long sinceBuildIdFinal = sinceBuildId;
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return sinceBuildIdFinal < getBuildId(item);
}
});
}
}
final String untilBuild = locator.getSingleDimensionValue(UNTIL_BUILD);
if (untilBuild != null) {
try {
final BuildPromotion untilBuildPromotion = getItem(untilBuild);
final SQueuedBuild queuedBuild = untilBuildPromotion.getQueuedBuild();
if (queuedBuild != null) {
// compare queued builds by id (triggering sequence)
final long buildPromotionId = getBuildId(untilBuildPromotion);
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return !(buildPromotionId < getBuildId(item));
}
});
} else {
// for started build, compare by start time
final SBuild limitingBuild = untilBuildPromotion.getAssociatedBuild();
if (limitingBuild != null) {
final Date startDate = limitingBuild.getStartDate();
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
final SBuild build = item.getAssociatedBuild();
return build == null || !startDate.before(build.getStartDate());
}
});
}
}
} catch (NotFoundException e) {
// build not found by sinceBuild locator, extract id ad filter using it
final long untilBuildId = getBuildId(untilBuild);
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
return !(untilBuildId < getBuildId(item));
}
});
}
}
TimeCondition.FilterAndLimitingDate<BuildPromotion> queuedFiltering = myTimeCondition.processTimeConditions(QUEUED_TIME, locator, TimeCondition.QUEUED_BUILD_TIME, TimeCondition.QUEUED_BUILD_TIME);
if (queuedFiltering != null)
result.add(queuedFiltering.getFilter());
TimeCondition.FilterAndLimitingDate<BuildPromotion> startedFiltering = myTimeCondition.processTimeConditions(STARTED_TIME, locator, TimeCondition.STARTED_BUILD_TIME, TimeCondition.STARTED_BUILD_TIME);
@Nullable Date sinceStartDate = null;
if (startedFiltering != null) {
result.add(startedFiltering.getFilter());
sinceStartDate = startedFiltering.getLimitingDate();
}
// todo: add processing cut of based on assumption of max build time (say, a week); for other times as well
TimeCondition.FilterAndLimitingDate<BuildPromotion> finishFiltering = myTimeCondition.processTimeConditions(FINISHED_TIME, locator, TimeCondition.FINISHED_BUILD_TIME, TimeCondition.FINISHED_BUILD_TIME);
if (finishFiltering != null)
result.add(finishFiltering.getFilter());
final String revisionLocatorText = locator.getSingleDimensionValue(REVISION);
if (revisionLocatorText != null) {
final Locator revisionLocator = new Locator(revisionLocatorText, "version", "internalVersion", "vcsRoot", Locator.LOCATOR_SINGLE_VALUE_UNUSED_NAME);
final String revision = revisionLocator.getSingleValue();
if (revision != null) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
// use getAllrevisions map??
final List<BuildRevision> buildRevisions = item.getRevisions();
for (BuildRevision rev : buildRevisions) {
if (revision.equals(rev.getRevisionDisplayName())) {
return true;
}
}
return false;
}
});
} else {
final String vcsRootLocator = revisionLocator.getSingleDimensionValue("vcsRoot");
final SVcsRoot vcsRoot = vcsRootLocator == null ? null : myVcsRootFinder.getItem(vcsRootLocator);
final ValueCondition versionCondition = ParameterCondition.createValueCondition(revisionLocator.getSingleDimensionValue("version"));
final ValueCondition internalVersionCondition = ParameterCondition.createValueCondition(revisionLocator.getSingleDimensionValue("internalVersion"));
revisionLocator.checkLocatorFullyProcessed();
if (vcsRoot != null || versionCondition != null || internalVersionCondition != null) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
// use getAllrevisions map??
final List<BuildRevision> revisions = item.getRevisions();
for (BuildRevision rev : revisions) {
if ((vcsRoot == null || vcsRoot.getId() == rev.getRoot().getParent().getId()) && (versionCondition == null || versionCondition.matches(rev.getRevisionDisplayName())) && (internalVersionCondition == null || internalVersionCondition.matches(rev.getRevision()))) {
return true;
}
}
return false;
}
});
}
}
}
final MultiCheckerFilter<SBuild> buildFilter = getBuildFilter(locator);
if (buildFilter.getSubFiltersCount() > 0) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
final SBuild build = item.getAssociatedBuild();
if (build == null) {
return false;
}
return buildFilter.isIncluded(build);
}
});
}
// see also filtering in getBuildFilter
sinceStartDate = TimeCondition.maxDate(sinceStartDate, DataProvider.parseDate(locator.getSingleDimensionValue(SINCE_DATE)));
final Boolean canceled = locator.getSingleDimensionValueAsBoolean(CANCELED);
if (canceled != null) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
final SBuild build = item.getAssociatedBuild();
return FilterUtil.isIncludedByBooleanFilter(canceled, build != null && build.getCanceledInfo() != null);
}
});
}
final Boolean failedToStart = locator.getSingleDimensionValueAsBoolean(FAILED_TO_START);
if (failedToStart != null) {
result.add(new FilterConditionChecker<BuildPromotion>() {
public boolean isIncluded(@NotNull final BuildPromotion item) {
final SBuild build = item.getAssociatedBuild();
return FilterUtil.isIncludedByBooleanFilter(failedToStart, build != null && build.isInternalError());
}
});
}
return getFilterWithProcessingCutOff(result, locator.getSingleDimensionValueAsLong(SINCE_BUILD_ID_LOOK_AHEAD_COUNT), sinceBuildPromotion, sinceBuildId, sinceStartDate);
}
use of jetbrains.buildServer.serverSide.metadata.BuildMetadataEntry in project teamcity-rest by JetBrains.
the class BuildPromotionFinder method getPrefilteredItems.
@NotNull
@Override
public ItemHolder<BuildPromotion> getPrefilteredItems(@NotNull Locator locator) {
if (!myPermissionChecker.hasPermissionInAnyProject(Permission.VIEW_PROJECT)) {
// TeamCity issue: should be handled in core. Do not spend any resources on user without permissions
return getItemHolder(Collections.emptyList());
}
final Boolean byPromotion = locator.getSingleDimensionValueAsBoolean(BY_PROMOTION);
if (byPromotion != null && !byPromotion) {
throw new BadRequestException("Found '" + BY_PROMOTION + "' locator set to 'false' which is not supported");
}
final String strob = locator.getSingleDimensionValue(STROB);
if (strob != null) {
final Locator strobLocator = new Locator(strob, BUILD_TYPE, BRANCH, STROB_BUILD_LOCATOR);
List<Locator> partialLocators = Collections.singletonList(Locator.createEmptyLocator());
partialLocators = getPartialLocatorsForStrobDimension(partialLocators, strobLocator, BUILD_TYPE, myBuildTypeFinder);
partialLocators = getPartialLocatorsForStrobDimension(partialLocators, strobLocator, BRANCH, myBranchFinder);
final String strobBuildLocator = strobLocator.getSingleDimensionValue(STROB_BUILD_LOCATOR);
final AggregatingItemHolder<BuildPromotion> strobResult = new AggregatingItemHolder<>();
for (Locator partialLocator : partialLocators) {
// limit to single item per strob item by default
partialLocator.setDimensionIfNotPresent(PagerData.COUNT, "1");
final String finalBuildLocator = Locator.createLocator(strobBuildLocator, partialLocator, new String[] {}).getStringRepresentation();
strobResult.add(getItemHolder(getItems(finalBuildLocator).myEntries));
}
strobLocator.checkLocatorFullyProcessed();
return strobResult;
}
setLocatorDefaults(locator);
final String equivalent = locator.getSingleDimensionValue(EQUIVALENT);
if (equivalent != null) {
final BuildPromotionEx build = (BuildPromotionEx) getItem(equivalent);
final List<BuildPromotionEx> result = build.getStartedEquivalentPromotions(-1);
final Set<BuildPromotion> convertedResult = new TreeSet<BuildPromotion>(BUILD_PROMOTIONS_COMPARATOR);
for (BuildPromotionEx item : result) {
convertedResult.add(item);
}
return getItemHolder(convertedResult);
}
final String metadata = locator.getSingleDimensionValue(METADATA);
if (metadata != null) {
final Iterator<BuildMetadataEntry> metadataEntries = getBuildMetadataEntryIterator(metadata);
return new ItemHolder<BuildPromotion>() {
@Override
public void process(@NotNull final ItemProcessor<BuildPromotion> processor) {
while (metadataEntries.hasNext()) {
BuildMetadataEntry metadataEntry = metadataEntries.next();
SBuild build = myBuildsManager.findBuildInstanceById(metadataEntry.getBuildId());
if (build != null && Build.canViewRuntimeData(myPermissionChecker, build.getBuildPromotion())) {
processor.processItem(build.getBuildPromotion());
}
}
}
};
}
final String graphLocator = locator.getSingleDimensionValue(ORDERED);
if (graphLocator != null) {
final GraphFinder<BuildPromotion> graphFinder = new BuildPromotionOrderedFinder(this);
// consider performance optimization by converting id to build only on actual retrieve (use GraphFinder<Int/buildId>)
return getItemHolder(graphFinder.getItems(graphLocator).myEntries);
}
final String snapshotDepDimension = locator.getSingleDimensionValue(SNAPSHOT_DEP);
if (snapshotDepDimension != null) {
return getItemHolder(getSnapshotRelatedBuilds(snapshotDepDimension));
}
final String artifactDepDimension = locator.getSingleDimensionValue(ARTIFACT_DEP);
if (artifactDepDimension != null) {
return getItemHolder(getArtifactRelatedBuilds(artifactDepDimension, locator));
}
final String snapshotDepProblem = locator.getSingleDimensionValue(SNAPSHOT_PROBLEM);
if (snapshotDepProblem != null) {
return getItemHolder(getSnapshotDepProblemBuilds(snapshotDepProblem));
}
final String number = locator.getSingleDimensionValue(NUMBER);
if (number != null) {
final String buildTypeLocator = locator.getSingleDimensionValue(BUILD_TYPE);
if (buildTypeLocator != null) {
final List<SBuildType> buildTypes = myBuildTypeFinder.getBuildTypes(null, buildTypeLocator);
final Set<BuildPromotion> builds = new TreeSet<BuildPromotion>(BUILD_PROMOTIONS_COMPARATOR);
for (SBuildType buildType : buildTypes) {
// todo: ensure due builds sorting
builds.addAll(BuildFinder.toBuildPromotions(myBuildsManager.findBuildInstancesByBuildNumber(buildType.getBuildTypeId(), number)));
}
return getItemHolder(builds);
} else {
// if build type is not specified, search by scanning (performance impact)
locator.markUnused(NUMBER, BUILD_TYPE);
}
}
if (locator.isAnyPresent(TAG) && TeamCityProperties.getBooleanOrTrue("rest.request.builds.prefilterByTag")) {
// using locator copy so that no dimensions are marked as used
Locator stateLocator = getStateLocator(new Locator(locator));
if (isStateIncluded(stateLocator, STATE_FINISHED)) {
// no sense in going further here if no finished builds are requested
// not marking as used to enforce filter processing later
final List<String> tagLocators = locator.lookupDimensionValue(TAG);
Stream<BuildPromotion> finishedBuilds = TagFinder.getPrefilteredFinishedBuildPromotions(tagLocators, myServiceLocator);
if (finishedBuilds != null) {
FilterConditionChecker<BuildPromotion> tagsFilter = getFilterByTag(tagLocators);
// After this point no other builds will be added
locator.markUsed(Collections.singleton(TAG));
Stream<BuildPromotion> queuedBuilds = Stream.empty();
if (isStateIncluded(stateLocator, STATE_QUEUED)) {
queuedBuilds = myBuildQueue.getItems().stream().map(SQueuedBuild::getBuildPromotion);
}
Stream<BuildPromotion> runningBuilds = Stream.empty();
if (isStateIncluded(stateLocator, STATE_RUNNING)) {
// BuildsManager.processBuilds could be used here instead of getting finished and running builds separately, but BuildQueryOptions support
// only exact tag name match, so we can't satisfy all possible TAG queriy conditions with it. To counter that we use
// TagFinder.getPrefilteredFinishedBuildPromotions to obtain a subset of required finished builds and BuildsManager.getRunningBuilds()
// to get all runing builds and later filter everything properly.
runningBuilds = myBuildsManager.getRunningBuilds().stream().map(SRunningBuild::getBuildPromotion);
}
// None of the concatenated streams were properly filtered, so let's filter them now.
// This hopefully allows for avoiding of hitting a lookupLimit because of a large queue.
Stream<BuildPromotion> allBuildsFilteredByTag = Stream.concat(Stream.concat(queuedBuilds, runningBuilds), finishedBuilds).filter(tagsFilter::isIncluded);
return processor -> allBuildsFilteredByTag.forEach(processor::processItem);
}
}
}
final String testOccurrence = locator.getSingleDimensionValue(TEST_OCCURRENCE);
if (testOccurrence != null) {
TestOccurrenceFinder testOccurrenceFinder = myServiceLocator.getSingletonService(TestOccurrenceFinder.class);
return FinderDataBinding.getItemHolder(testOccurrenceFinder.getItems(testOccurrence).myEntries.stream().map(sTestRun -> sTestRun.getBuild().getBuildPromotion()));
}
SBuildAgent agent;
final String agentLocator = locator.getSingleDimensionValue(AGENT);
if (agentLocator != null) {
List<SBuildAgent> agents = myAgentFinder.getItemsNotEmpty(agentLocator).myEntries;
if (agents.size() == 1) {
agent = agents.get(0);
} else {
// only if builds processor cannot handle this
Stream<BuildPromotion> result = Stream.empty();
Locator stateLocator = getStateLocator(locator);
if (isStateIncluded(stateLocator, STATE_QUEUED)) {
// todo: should sort backwards as currently the order does not seem right...
result = Stream.concat(result, myBuildQueue.getItems().stream().filter(build -> !CollectionsUtil.intersect(build.getCanRunOnAgents(), agents).isEmpty()).map(build -> build.getBuildPromotion()));
}
if (isStateIncluded(stateLocator, STATE_RUNNING)) {
// todo: address an issue when a build can appear twice in the output
// agent instance can be different when disconnecting, so need to check id
Set<Integer> agentIds = agents.stream().map(a -> a.getId()).collect(Collectors.toSet());
Set<String> agentNames = agents.stream().map(a -> a.getName()).collect(Collectors.toSet());
result = Stream.concat(result, myBuildsManager.getRunningBuilds().stream().filter(build -> {
SBuildAgent buildAgent = build.getAgent();
int agentId = buildAgent.getId();
return agentId > 0 ? agentIds.contains(agentId) : agentNames.contains(buildAgent.getName());
}).map(build -> build.getBuildPromotion()));
}
if (isStateIncluded(stateLocator, STATE_FINISHED)) {
// todo: optimize for user and canceled
Stream<BuildPromotion> finishedBuilds = StreamUtil.merge(agents.stream().map(a -> a.getBuildHistory(null, true).stream().map(b -> b.getBuildPromotion())), BUILD_PROMOTIONS_COMPARATOR);
result = Stream.concat(result, finishedBuilds);
}
return FinderDataBinding.getItemHolder(result);
}
} else {
agent = null;
}
// process by build states
final List<BuildPromotion> result = new ArrayList<BuildPromotion>();
Set<Long> includedPromotionIds = new HashSet<>();
@Nullable Set<SBuildType> buildTypes = getBuildTypes(locator);
String agentName;
String agentNameCondition = locator.lookupSingleDimensionValue(AGENT_NAME);
if (agentNameCondition != null) {
agentName = ParameterCondition.createValueCondition(agentNameCondition).getConstantValueIfSimpleEqualsCondition();
if (agentName != null)
locator.markUsed(Collections.singleton(AGENT_NAME));
} else {
agentName = null;
}
Long agentTypeId = locator.getSingleDimensionValueAsLong(AGENT_TYPE_ID);
Locator stateLocator = getStateLocator(locator);
if (isStateIncluded(stateLocator, STATE_QUEUED)) {
// todo: should sort backwards as currently the order does not seem right...
Stream<SQueuedBuild> builds = myBuildQueue.getItems().stream();
if (buildTypes != null) {
// make sure buildTypes retrieved from the locator are used
builds = builds.filter(qb -> buildTypes.contains(qb.getBuildPromotion().getParentBuildType()));
}
if (agentTypeId != null) {
builds = builds.filter(build -> build.getCanRunOnAgents().stream().anyMatch(a -> a.getAgentTypeId() == agentTypeId.intValue()));
}
if (agent != null) {
// todo: check
builds = builds.filter(build -> build.getCanRunOnAgents().stream().anyMatch(a -> a.equals(agent)));
}
if (agentName != null) {
builds = builds.filter(build -> build.getCanRunOnAgents().stream().anyMatch(a -> a.getName().equals(agentName)));
}
builds.map(b -> b.getBuildPromotion()).forEach(p -> {
if (includedPromotionIds.add(p.getId())) {
result.add(p);
}
});
}
if (isStateIncluded(stateLocator, STATE_RUNNING)) {
Stream<SRunningBuild> builds = myBuildsManager.getRunningBuilds().stream();
if (buildTypes != null) {
// make sure buildTypes retrieved from the locator are used
builds = builds.filter(b -> buildTypes.contains(b.getBuildPromotion().getParentBuildType()));
}
if (agentTypeId != null) {
builds = builds.filter(build -> build.getAgent().getAgentTypeId() == agentTypeId.intValue());
}
if (agent != null) {
builds = builds.filter(build -> {
SBuildAgent buildAgent = build.getAgent();
int agentId = buildAgent.getId();
return agentId > 0 ? agent.getId() == agentId : agent.getName().equals(buildAgent.getName());
});
}
if (agentName != null) {
builds = builds.filter(build -> build.getAgentName().equals(agentName));
}
builds.map(b -> b.getBuildPromotion()).forEach(p -> {
if (includedPromotionIds.add(p.getId())) {
result.add(p);
}
});
}
ItemHolder<BuildPromotion> finishedBuilds = null;
if (isStateIncluded(stateLocator, STATE_FINISHED)) {
final BuildQueryOptions options = new BuildQueryOptions();
if (buildTypes != null) {
options.setBuildTypeIds(buildTypes.stream().map(bt -> bt.getBuildTypeId()).collect(Collectors.toList()));
}
if (agentTypeId != null) {
options.setAgentTypeId(agentTypeId.intValue());
}
if (agent != null) {
options.setAgent(agent);
}
if (agentName != null) {
options.setAgentName(agentName);
}
final Boolean personal = locator.lookupSingleDimensionValueAsBoolean(PERSONAL);
if (personal == null || personal) {
final String userDimension = locator.getSingleDimensionValue(USER);
options.setIncludePersonal(true, userDimension == null ? null : myUserFinder.getItem(userDimension));
} else {
options.setIncludePersonal(false, null);
}
final Boolean failedToStart = locator.lookupSingleDimensionValueAsBoolean(FAILED_TO_START);
final Boolean canceled = locator.lookupSingleDimensionValueAsBoolean(CANCELED);
if (canceled == null || canceled || failedToStart == null || failedToStart) {
// also includes failed to start builds, TW-32060
options.setIncludeCanceled(true);
} else {
options.setIncludeCanceled(false);
}
final Boolean pinned = locator.getSingleDimensionValueAsBoolean(PINNED);
if (pinned != null) {
options.setPinStatus(pinned);
}
TagFinder.FilterOptions tagFilterOptions = TagFinder.getFilterOptions(locator.lookupDimensionValue(TAG), myServiceLocator);
if (tagFilterOptions != null) {
options.setTagName(tagFilterOptions.getTagName(), tagFilterOptions.getTagOwner());
}
// do not mark dimension as used as not all can be used from it
final String branchLocatorValue = locator.lookupSingleDimensionValue(BRANCH);
if (branchLocatorValue != null) {
BranchFinder.BranchFilterDetails branchFilterDetails;
try {
branchFilterDetails = myBranchFinder.getBranchFilterDetailsWithoutLocatorCheck(branchLocatorValue);
// parsed OK, setting options
if (branchFilterDetails.isAnyBranch()) {
options.setMatchAllBranches(true);
} else {
if (branchFilterDetails.isDefaultBranchOrNotBranched()) {
options.setMatchAllBranches(false);
options.setBranch(Branch.DEFAULT_BRANCH_NAME);
}
if (branchFilterDetails.getBranchName() != null) {
// causes a bug in certain cases, see https://youtrack.jetbrains.com/issue/TW-61530 however, no performant fix is possible so far
options.setMatchAllBranches(false);
options.setBranch(branchFilterDetails.getBranchName());
}
}
} catch (LocatorProcessException e) {
// not parsed, cannot extract name or default status
options.setMatchAllBranches(true);
}
} else {
options.setMatchAllBranches(true);
}
// running builds are retrieved separately and appear before finished ones
options.setIncludeRunning(false);
options.setOrderByChanges(false);
Long count = locator.getSingleDimensionValueAsLong(PagerData.COUNT);
if (count != null) {
options.setPageSize(count.intValue());
}
finishedBuilds = new ItemHolder<BuildPromotion>() {
public void process(@NotNull final ItemProcessor<BuildPromotion> processor) {
myBuildsManager.processBuilds(options, new ItemProcessor<SBuild>() {
public boolean processItem(SBuild item) {
// ignore already added builds
if (includedPromotionIds.contains(item.getBuildPromotion().getId()))
return true;
return processor.processItem(item.getBuildPromotion());
}
});
}
};
}
stateLocator.checkLocatorFullyProcessed();
final ItemHolder<BuildPromotion> finishedBuildsFinal = finishedBuilds;
return new ItemHolder<BuildPromotion>() {
public void process(@NotNull final ItemProcessor<BuildPromotion> processor) {
new CollectionItemHolder<BuildPromotion>(result).process(processor);
if (finishedBuildsFinal != null) {
finishedBuildsFinal.process(processor);
}
}
};
}
Aggregations