Search in sources :

Example 6 with BadRequestException

use of jetbrains.buildServer.server.rest.errors.BadRequestException 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 7 with BadRequestException

use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.

the class BuildFinder method getBuildsFilter.

@NotNull
private BuildsFilter getBuildsFilter(@NotNull final Locator buildLocator, @Nullable final SBuildType buildType) {
    // todo: report unknown locator dimensions
    final SBuildType actualBuildType = myBuildTypeFinder.deriveBuildTypeFromLocator(buildType, buildLocator.getSingleDimensionValue("buildType"));
    final String projectFromLocator = buildLocator.getSingleDimensionValue("project");
    final SProject project = StringUtil.isEmpty(projectFromLocator) ? null : myProjectFinder.getItem(projectFromLocator);
    final String userLocator = buildLocator.getSingleDimensionValue("user");
    final String tagsString = buildLocator.getSingleDimensionValue("tags");
    final String singleTagString = buildLocator.getSingleDimensionValue("tag");
    if (tagsString != null && singleTagString != null) {
        throw new BadRequestException("Both 'tags' and 'tag' dimensions specified. Only one can be present.");
    }
    List<String> tagsList = null;
    if (singleTagString != null) {
        tagsList = Collections.singletonList(singleTagString);
    } else if (tagsString != null) {
        tagsList = Arrays.asList(tagsString.split(","));
    }
    final Long count = buildLocator.getSingleDimensionValueAsLong(PagerData.COUNT);
    BranchMatcher branchMatcher;
    final String branchLocatorValue = buildLocator.getSingleDimensionValue("branch");
    try {
        branchMatcher = new BranchMatcher(branchLocatorValue);
    } catch (LocatorProcessException e) {
        throw new LocatorProcessException("Invalid sub-locator 'branch': " + e.getMessage());
    }
    Collection<SBuildAgent> agents = null;
    final String agentLocator = buildLocator.getSingleDimensionValue("agent");
    if (agentLocator != null) {
        agents = myAgentFinder.getItems(agentLocator).myEntries;
    }
    return new GenericBuildsFilter(actualBuildType, project, buildLocator.getSingleDimensionValue("status"), buildLocator.getSingleDimensionValue("number"), userLocator == null ? null : myUserFinder.getItem(userLocator), buildLocator.getSingleDimensionValueAsBoolean("personal", false), buildLocator.getSingleDimensionValueAsBoolean("canceled", false), buildLocator.getSingleDimensionValueAsBoolean("running", false), buildLocator.getSingleDimensionValueAsBoolean("pinned"), tagsList, branchMatcher, // deprecated, use "agent" instead
    buildLocator.getSingleDimensionValue("agentName"), agents, ParameterCondition.create(buildLocator.getSingleDimensionValue("property")), getRangeLimit(actualBuildType, buildLocator.getSingleDimensionValue("sinceBuild"), DataProvider.parseDate(buildLocator.getSingleDimensionValue("sinceDate"))), getRangeLimit(actualBuildType, buildLocator.getSingleDimensionValue("untilBuild"), DataProvider.parseDate(buildLocator.getSingleDimensionValue("untilDate"))), buildLocator.getSingleDimensionValueAsLong(PagerData.START), count == null ? null : count.intValue(), buildLocator.getSingleDimensionValueAsLong("lookupLimit"), myServiceLocator);
}
Also used : GenericBuildsFilter(jetbrains.buildServer.server.rest.data.build.GenericBuildsFilter) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) LocatorProcessException(jetbrains.buildServer.server.rest.errors.LocatorProcessException) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with BadRequestException

use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.

the class TimeCondition method processTimeConditions.

/**
 * @return Date is included if it can be used for cutting processing. 'null' if no dimension is defined
 */
@Nullable
<T> FilterAndLimitingDate<T> processTimeConditions(@NotNull final String locatorDimension, @NotNull final Locator locator, @NotNull final ValueExtractor<T, Date> valueExtractor, @Nullable final ValueExtractor<BuildPromotion, Date> buildValueExtractor) {
    final List<String> timeLocators = locator.getDimensionValue(locatorDimension);
    if (timeLocators.isEmpty())
        return null;
    MultiCheckerFilter<T> resultFilter = new MultiCheckerFilter<>();
    Date resultDate = null;
    for (String timeLocator : timeLocators) {
        try {
            FilterAndLimitingDate<T> filterAndLimitingDate = processTimeCondition(timeLocator, valueExtractor, buildValueExtractor);
            resultFilter.add(filterAndLimitingDate.getFilter());
            resultDate = maxDate(resultDate, filterAndLimitingDate.getLimitingDate());
        } catch (BadRequestException e) {
            throw new BadRequestException("Error processing '" + locatorDimension + "' locator '" + timeLocator + "': " + e.getMessage(), e);
        }
    }
    return new FilterAndLimitingDate<T>(resultFilter, resultDate);
}
Also used : BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with BadRequestException

use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.

the class GenericBuildsFilter method isTagsMatchLocator.

private boolean isTagsMatchLocator(final List<String> buildTags, final Locator tagsLocator) {
    if (!"extended".equals(tagsLocator.getSingleDimensionValue("format"))) {
        throw new BadRequestException("Only 'extended' value is supported for 'format' dimension of 'tag' dimension");
    }
    final Boolean present = tagsLocator.getSingleDimensionValueAsBoolean("present", true);
    final String patternString = tagsLocator.getSingleDimensionValue("regexp");
    if (present == null) {
        return true;
    }
    Boolean tagsMatchPattern = null;
    if (patternString != null) {
        if (StringUtil.isEmpty(patternString)) {
            throw new BadRequestException("'regexp' sub-dimension should not be empty for 'tag' dimension");
        }
        try {
            tagsMatchPattern = tagsMatchPattern(buildTags, patternString);
        } catch (PatternSyntaxException e) {
            throw new BadRequestException("Bad syntax for Java regular expression in 'regexp' sub-dimension of 'tag' dimension: " + e.getMessage(), e);
        }
    }
    if (tagsMatchPattern == null) {
        if ((present && buildTags.size() != 0) || (!present && (buildTags.size() == 0))) {
            return true;
        }
    } else {
        if (present && tagsMatchPattern) {
            return true;
        } else if (!present && !tagsMatchPattern) {
            return true;
        }
    }
    return false;
}
Also used : BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 10 with BadRequestException

use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.

the class InvestigationFinder method getFilter.

@NotNull
@Override
public ItemFilter<InvestigationWrapper> getFilter(@NotNull final Locator locator) {
    final MultiCheckerFilter<InvestigationWrapper> result = new MultiCheckerFilter<InvestigationWrapper>();
    final String investigatorDimension = locator.getSingleDimensionValue(ASSIGNEE);
    if (investigatorDimension != null) {
        @NotNull final User user = myUserFinder.getItem(investigatorDimension);
        result.add(new FilterConditionChecker<InvestigationWrapper>() {

            public boolean isIncluded(@NotNull final InvestigationWrapper item) {
                return user.equals(item.getResponsibleUser());
            }
        });
    }
    final String reporterDimension = locator.getSingleDimensionValue(REPORTER);
    if (reporterDimension != null) {
        @NotNull final User user = myUserFinder.getItem(reporterDimension);
        result.add(new FilterConditionChecker<InvestigationWrapper>() {

            public boolean isIncluded(@NotNull final InvestigationWrapper item) {
                return user.equals(item.getReporterUser());
            }
        });
    }
    final String typeDimension = locator.getSingleDimensionValue(TYPE);
    if (typeDimension != null) {
        if (ProblemTarget.getKnownTypesForInvestigation().stream().noneMatch(s -> typeDimension.equalsIgnoreCase(s))) {
            throw new BadRequestException("Error in dimension '" + TYPE + "': unknown value '" + typeDimension + "'. Known values: " + StringUtil.join(ProblemTarget.getKnownTypesForInvestigation(), ", "));
        }
        result.add(new FilterConditionChecker<InvestigationWrapper>() {

            public boolean isIncluded(@NotNull final InvestigationWrapper item) {
                return typeDimension.equalsIgnoreCase(ProblemTarget.getType(item));
            }
        });
    }
    final String stateDimension = locator.getSingleDimensionValue(STATE);
    if (stateDimension != null) {
        if (!InvestigationWrapper.getKnownStates().contains(stateDimension.toLowerCase())) {
            throw new BadRequestException("Error in dimension '" + STATE + "': unknown value '" + stateDimension + "'. Known values: " + StringUtil.join(InvestigationWrapper.getKnownStates(), ", "));
        }
        result.add(new FilterConditionChecker<InvestigationWrapper>() {

            public boolean isIncluded(@NotNull final InvestigationWrapper item) {
                return stateDimension.equalsIgnoreCase(item.getState().name());
            }
        });
    }
    final String resolutionDimension = locator.getSingleDimensionValue(RESOLUTION);
    if (resolutionDimension != null) {
        ResponsibilityEntry.RemoveMethod removeMethod = Resolution.getRemoveMethodForInvestigation(resolutionDimension);
        result.add(item -> removeMethod.equals(item.getRemoveMethod()));
    }
    final String assignmentProjectDimension = locator.getSingleDimensionValue(ASSIGNMENT_PROJECT);
    if (assignmentProjectDimension != null) {
        @NotNull final SProject project = myProjectFinder.getItem(assignmentProjectDimension);
        result.add(new FilterConditionChecker<InvestigationWrapper>() {

            public boolean isIncluded(@NotNull final InvestigationWrapper item) {
                final BuildProject assignmentProject = item.getAssignmentProject();
                return assignmentProject != null && project.getProjectId().equals(assignmentProject.getProjectId());
            }
        });
    }
    final String affectedProjectDimension = locator.getSingleDimensionValue(AFFECTED_PROJECT);
    if (affectedProjectDimension != null) {
        @NotNull final SProject project = myProjectFinder.getItem(affectedProjectDimension);
        result.add(new FilterConditionChecker<InvestigationWrapper>() {

            public boolean isIncluded(@NotNull final InvestigationWrapper item) {
                final BuildProject assignmentProject = item.getAssignmentProject();
                final BuildType assignmentBuildType = item.getAssignmentBuildType();
                final BuildProject buildTypeProject = assignmentBuildType != null ? myProjectFinder.findProjectByInternalId(assignmentBuildType.getProjectId()) : null;
                return (assignmentProject != null && ProjectFinder.isSameOrParent(project, assignmentProject)) || (buildTypeProject != null && ProjectFinder.isSameOrParent(project, buildTypeProject));
            }
        });
    }
    final String sinceDateDimension = locator.getSingleDimensionValue(SINCE_DATE);
    if (sinceDateDimension != null) {
        final Date date = DataProvider.getDate(sinceDateDimension);
        result.add(new FilterConditionChecker<InvestigationWrapper>() {

            public boolean isIncluded(@NotNull final InvestigationWrapper item) {
                return date.before(item.getTimestamp());
            }
        });
    }
    // todo: add assignmentBuildType
    return result;
}
Also used : User(jetbrains.buildServer.users.User) BuildProject(jetbrains.buildServer.BuildProject) SProject(jetbrains.buildServer.serverSide.SProject) NotNull(org.jetbrains.annotations.NotNull) Date(java.util.Date) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) SBuildType(jetbrains.buildServer.serverSide.SBuildType) BuildType(jetbrains.buildServer.BuildType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)128 NotNull (org.jetbrains.annotations.NotNull)52 ApiOperation (io.swagger.annotations.ApiOperation)39 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)35 ServiceLocator (jetbrains.buildServer.ServiceLocator)28 BuildTypeOrTemplate (jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate)22 Nullable (org.jetbrains.annotations.Nullable)22 SUser (jetbrains.buildServer.users.SUser)19 Fields (jetbrains.buildServer.server.rest.model.Fields)17 LocatorProcessException (jetbrains.buildServer.server.rest.errors.LocatorProcessException)16 Collectors (java.util.stream.Collectors)10 SProject (jetbrains.buildServer.serverSide.SProject)9 StringUtil (jetbrains.buildServer.util.StringUtil)9 Date (java.util.Date)8 jetbrains.buildServer.serverSide (jetbrains.buildServer.serverSide)8 java.util (java.util)7 Locator (jetbrains.buildServer.server.rest.data.Locator)7 OperationException (jetbrains.buildServer.server.rest.errors.OperationException)7 PagerData (jetbrains.buildServer.server.rest.model.PagerData)7 Permission (jetbrains.buildServer.serverSide.auth.Permission)7