Search in sources :

Example 1 with BuildsFilter

use of jetbrains.buildServer.server.rest.data.build.BuildsFilter in project teamcity-rest by JetBrains.

the class BuildFinder method getBuildPromotion.

/**
 * Supported build locators:
 *  213 - build with id=213
 *  213 when buildType is specified - build in the specified buildType with build number 213
 *  id:213 - build with id=213
 *  buildType:bt37 - specify Build Configuration by internal id. If specified, other locator parts should select the build
 *  number:213 when buildType is specified - build in the specified buildType with build number 213
 *  status:SUCCESS when buildType is specified - last build with the specified status in the specified buildType
 */
@NotNull
public BuildPromotion getBuildPromotion(@Nullable SBuildType buildType, @Nullable final String buildLocator) {
    if (!TeamCityProperties.getBoolean(LEGACY_BUILDS_FILTERING_FORCED)) {
        return getBuildPromotionInternal(buildType, buildLocator);
    }
    if (StringUtil.isEmpty(buildLocator)) {
        throw new BadRequestException("Empty single build locator is not supported.");
    }
    Locator locator = null;
    try {
        locator = new Locator(buildLocator);
    } catch (LocatorProcessException e) {
    // unparsable locator - proceed to report a due error later with all the supported locators
    }
    if (locator == null || useByPromotionFiltering(locator)) {
        return getBuildPromotionInternal(buildType, buildLocator);
    }
    if (locator.isSingleValue()) {
        if (buildType == null) {
            // no dimensions found and no build type, assume it's build id
            @SuppressWarnings("ConstantConditions") SBuild build = // todo: report non-number more user-friendly
            myServiceLocator.getSingletonService(BuildsManager.class).findBuildInstanceById(locator.getSingleValueAsLong());
            if (build == null) {
                throw new NotFoundException("Cannot find build by id '" + locator.getSingleValue() + "'.");
            }
            return build.getBuildPromotion();
        }
        // no dimensions found and build type is specified, assume it's build number
        @SuppressWarnings("ConstantConditions") SBuild build = myServiceLocator.getSingletonService(BuildsManager.class).findBuildInstanceByBuildNumber(buildType.getBuildTypeId(), buildLocator);
        if (build == null) {
            throw new NotFoundException("No build can be found by number '" + buildLocator + "' in build configuration " + buildType + ".");
        }
        return build.getBuildPromotion();
    }
    String buildTypeLocator = locator.getSingleDimensionValue("buildType");
    buildType = myBuildTypeFinder.deriveBuildTypeFromLocator(buildType, buildTypeLocator);
    Long id = locator.getSingleDimensionValueAsLong(DIMENSION_ID);
    if (id != null) {
        SBuild build = myServiceLocator.getSingletonService(BuildsManager.class).findBuildInstanceById(id);
        if (build == null) {
            throw new NotFoundException("No build can be found by id '" + id + "'.");
        }
        if (buildType != null && !buildType.getBuildTypeId().equals(build.getBuildTypeId())) {
            throw new NotFoundException("No build can be found by id '" + locator.getSingleDimensionValue(DIMENSION_ID) + "' in build type '" + buildType + "'.");
        }
        if (locator.getDimensionsCount() > 1) {
            LOG.info("Build locator '" + buildLocator + "' has '" + DIMENSION_ID + "' dimension and others. Others are ignored.");
        }
        return build.getBuildPromotion();
    }
    String number = locator.getSingleDimensionValue("number");
    if (number != null && buildType != null) {
        SBuild build = myServiceLocator.getSingletonService(BuildsManager.class).findBuildInstanceByBuildNumber(buildType.getBuildTypeId(), number);
        if (build == null) {
            throw new NotFoundException("No build can be found by number '" + number + "' in build configuration " + buildType + ".");
        }
        if (locator.getDimensionsCount() > 1) {
            LOG.info("Build locator '" + buildLocator + "' has 'number' dimension and others. Others are ignored.");
        }
        return build.getBuildPromotion();
    }
    Long promotionId = locator.getSingleDimensionValueAsLong(PROMOTION_ID);
    if (promotionId == null) {
        // support TeamCity 8.0 dimension
        promotionId = locator.getSingleDimensionValueAsLong("promotionId");
    }
    if (promotionId != null) {
        BuildPromotion build = getBuildByPromotionId(promotionId);
        if (buildType != null && !buildType.getBuildTypeId().equals(build.getBuildTypeId())) {
            throw new NotFoundException("No build can be found by " + PROMOTION_ID + " '" + promotionId + "' in build type '" + buildType + "'.");
        }
        if (locator.getDimensionsCount() > 1) {
            LOG.info("Build locator '" + buildLocator + "' has '" + PROMOTION_ID + "' dimension and others. Others are ignored.");
        }
        return build;
    }
    final BuildsFilter buildsFilter = getBuildsFilter(locator, buildType);
    buildsFilter.setCount(1);
    locator.checkLocatorFullyProcessed();
    final List<SBuild> filteredBuilds = getBuilds(buildsFilter);
    if (filteredBuilds.size() == 0) {
        throw new NotFoundException("No build found by filter: " + buildsFilter.toString() + ".");
    }
    if (filteredBuilds.size() == 1) {
        return filteredBuilds.get(0).getBuildPromotion();
    }
    throw new BadRequestException("Build locator '" + buildLocator + "' is not supported (" + filteredBuilds.size() + " builds found)");
}
Also used : ServiceLocator(jetbrains.buildServer.ServiceLocator) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) LocatorProcessException(jetbrains.buildServer.server.rest.errors.LocatorProcessException) GenericBuildsFilter(jetbrains.buildServer.server.rest.data.build.GenericBuildsFilter) BuildsFilter(jetbrains.buildServer.server.rest.data.build.BuildsFilter) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with BuildsFilter

use of jetbrains.buildServer.server.rest.data.build.BuildsFilter in project teamcity-rest by JetBrains.

the class BuildFinder method getBuilds.

@NotNull
public PagedSearchResult<BuildPromotion> getBuilds(@Nullable final SBuildType buildType, @Nullable final String locatorText) {
    if (!TeamCityProperties.getBoolean(LEGACY_BUILDS_FILTERING_FORCED)) {
        return myBuildPromotionFinder.getBuildPromotions(buildType, locatorText);
    }
    Locator locator = null;
    try {
        locator = locatorText != null ? new Locator(locatorText) : Locator.createEmptyLocator();
    } catch (LocatorProcessException e) {
        // error creating locator - some special dimensions used? process by default
        return myBuildPromotionFinder.getBuildPromotions(buildType, locatorText);
    }
    if (useByPromotionFiltering(locator)) {
        return myBuildPromotionFinder.getBuildPromotions(buildType, locatorText);
    }
    BuildsFilter buildsFilter = getBuildsFilter(locator, buildType);
    locator.checkLocatorFullyProcessed();
    final Integer c = buildsFilter.getCount();
    if (c != null) {
        buildsFilter.setCount(c != -1 ? c : null);
    } else {
        buildsFilter.setCount(jetbrains.buildServer.server.rest.request.Constants.getDefaultPageItemsCount());
    }
    return new PagedSearchResult<BuildPromotion>(toBuildPromotions(getBuilds(buildsFilter)), buildsFilter.getStart(), buildsFilter.getCount());
}
Also used : ServiceLocator(jetbrains.buildServer.ServiceLocator) LocatorProcessException(jetbrains.buildServer.server.rest.errors.LocatorProcessException) GenericBuildsFilter(jetbrains.buildServer.server.rest.data.build.GenericBuildsFilter) BuildsFilter(jetbrains.buildServer.server.rest.data.build.BuildsFilter) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with BuildsFilter

use of jetbrains.buildServer.server.rest.data.build.BuildsFilter in project teamcity-rest by JetBrains.

the class BuildFinder method getBuildsFilter.

@NotNull
public BuildsFilter getBuildsFilter(@Nullable final SBuildType buildType, final String locatorText) {
    Locator locator = new Locator(locatorText);
    BuildsFilter buildsFilter = getBuildsFilter(locator, buildType);
    locator.checkLocatorFullyProcessed();
    final Integer c = buildsFilter.getCount();
    if (c != null) {
        buildsFilter.setCount(c != -1 ? c : null);
    } else {
        buildsFilter.setCount(jetbrains.buildServer.server.rest.request.Constants.getDefaultPageItemsCount());
    }
    return buildsFilter;
}
Also used : ServiceLocator(jetbrains.buildServer.ServiceLocator) GenericBuildsFilter(jetbrains.buildServer.server.rest.data.build.GenericBuildsFilter) BuildsFilter(jetbrains.buildServer.server.rest.data.build.BuildsFilter) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with BuildsFilter

use of jetbrains.buildServer.server.rest.data.build.BuildsFilter in project teamcity-rest by JetBrains.

the class BuildFinder method getBuilds.

/**
 * Finds builds by the specified criteria within specified range
 * This is slow!
 *
 * @param buildsFilter the filter for the builds to find
 * @return the builds found
 */
private List<SBuild> getBuilds(@NotNull final BuildsFilter buildsFilter) {
    final ArrayList<SBuild> result = new ArrayList<SBuild>();
    // todo: sort and ensure there are no duplicates
    result.addAll(BuildsFilterProcessor.getMatchingRunningBuilds(buildsFilter, myServiceLocator.getSingletonService(BuildsManager.class)));
    final Integer originalCount = buildsFilter.getCount();
    if (originalCount == null || result.size() < originalCount) {
        final BuildsFilter patchedBuildsFilter = new BuildsFilterWithBuildExcludes(buildsFilter, result);
        if (originalCount != null) {
            patchedBuildsFilter.setCount(originalCount - result.size());
        }
        result.addAll(BuildsFilterProcessor.getMatchingFinishedBuilds(patchedBuildsFilter, myServiceLocator.getSingletonService(BuildHistory.class)));
    }
    return result;
}
Also used : BuildsFilterWithBuildExcludes(jetbrains.buildServer.server.rest.data.build.BuildsFilterWithBuildExcludes) GenericBuildsFilter(jetbrains.buildServer.server.rest.data.build.GenericBuildsFilter) BuildsFilter(jetbrains.buildServer.server.rest.data.build.BuildsFilter)

Aggregations

BuildsFilter (jetbrains.buildServer.server.rest.data.build.BuildsFilter)4 GenericBuildsFilter (jetbrains.buildServer.server.rest.data.build.GenericBuildsFilter)4 ServiceLocator (jetbrains.buildServer.ServiceLocator)3 NotNull (org.jetbrains.annotations.NotNull)3 LocatorProcessException (jetbrains.buildServer.server.rest.errors.LocatorProcessException)2 BuildsFilterWithBuildExcludes (jetbrains.buildServer.server.rest.data.build.BuildsFilterWithBuildExcludes)1 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)1 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)1