use of jetbrains.buildServer.util.ItemProcessor in project teamcity-rest by JetBrains.
the class BranchFinder method itemsExist.
@Override
public boolean itemsExist(@NotNull Locator locator) {
ItemHolder<BranchData> prefilteredItems = getPrefilteredItemsInternal(locator, true);
ItemFilter<BranchData> filter = getFilter(locator);
AtomicBoolean result = new AtomicBoolean(false);
ItemProcessor<BranchData> existenceChecker = branchData -> {
if (!filter.isIncluded(branchData))
return true;
result.compareAndSet(false, true);
return false;
};
prefilteredItems.process(existenceChecker);
return result.get();
}
use of jetbrains.buildServer.util.ItemProcessor in project teamcity-rest by JetBrains.
the class ProblemOccurrenceFinder method getProblemOccurrences.
@NotNull
private static ItemHolder<BuildProblem> getProblemOccurrences(@NotNull final Long problemId, @NotNull final ServiceLocator serviceLocator, @NotNull final BuildFinder buildFinder) {
// todo: TeamCity API (VB): how to do this?
final ArrayList<Long> buildIds = new ArrayList<Long>();
try {
// final SQLRunner sqlRunner = myServiceLocator.getSingletonService(SQLRunner.class);
// workaround for http://youtrack.jetbrains.com/issue/TW-25260
final SQLRunnerEx sqlRunner = serviceLocator.getSingletonService(BuildServerEx.class).getSQLRunner();
sqlRunner.withDB(new DBActionNoResults() {
public void run(final DBFunctions dbf) throws DBException {
dbf.queryForTuples(new Object() {
public void getBuildProblem(String build_state_id) throws IOException {
try {
// do nothing within database connection
buildIds.add(Long.valueOf(build_state_id));
} catch (NumberFormatException e) {
LOG.infoAndDebugDetails("Non-number build promotion id " + build_state_id + " retrieved from the database for problemId: " + problemId + ", ignoring.", e);
}
}
}, "getBuildProblem", "select build_state_id from build_problem where problem_id = ? order by build_state_id desc", problemId);
}
});
} catch (Exception e) {
throw new OperationException("Error performing database query: " + e.toString(), e);
}
return new ItemHolder<BuildProblem>() {
@Override
public void process(@NotNull final ItemProcessor<BuildProblem> processor) {
for (Long buildId : buildIds) {
try {
final BuildPromotion buildByPromotionId = buildFinder.getBuildByPromotionId(Long.valueOf(buildId));
if (buildByPromotionId.getBuildType() == null) {
// missing build type, skip. Workaround for http://youtrack.jetbrains.com/issue/TW-34733
} else {
final BuildProblem problem = findProblem(buildByPromotionId, problemId);
if (problem != null) {
processor.processItem(problem);
}
}
} catch (RuntimeException e) {
// addressing TW-41636
LOG.infoAndDebugDetails("Error getting problems for build promotion with id " + buildId + ", problemId: " + problemId + ", ignoring. Cause", e);
}
}
}
};
}
use of jetbrains.buildServer.util.ItemProcessor 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