Search in sources :

Example 1 with BuildTypeDescriptor

use of jetbrains.buildServer.BuildTypeDescriptor in project teamcity-rest by JetBrains.

the class ChangeFinder method getFilter.

@NotNull
@Override
public ItemFilter<SVcsModificationOrChangeDescriptor> getFilter(@NotNull final Locator locator) {
    final MultiCheckerFilter<SVcsModification> result = new MultiCheckerFilter<>();
    // myBuildType, myProject and myBranchName are handled on getting initial collection to filter
    final String vcsRootInstanceLocator = locator.getSingleDimensionValue(VCS_ROOT_INSTANCE);
    if (vcsRootInstanceLocator != null) {
        final VcsRootInstance vcsRootInstance = myVcsRootInstanceFinder.getItem(vcsRootInstanceLocator);
        result.add(item -> {
            // todo: check personal change applicability to the root
            return !item.isPersonal() && vcsRootInstance.getId() == item.getVcsRoot().getId();
        });
    }
    final String vcsRootLocator = locator.getSingleDimensionValue(VCS_ROOT);
    if (vcsRootLocator != null) {
        final VcsRoot vcsRoot = myVcsRootFinder.getItem(vcsRootLocator);
        result.add(item -> {
            // todo: check personal change applicability to the root
            return !item.isPersonal() && vcsRoot.getId() == item.getVcsRoot().getParent().getId();
        });
    }
    // todo: deprecate this
    final String sinceChangeLocator = locator.getSingleDimensionValue(SINCE_CHANGE);
    if (sinceChangeLocator != null) {
        final long sinceChangeId = getChangeIdBySinceChangeLocator(sinceChangeLocator);
        result.add(item -> sinceChangeId < item.getId());
    }
    if (locator.isUnused(USERNAME)) {
        final String username = locator.getSingleDimensionValue(USERNAME);
        if (username != null) {
            result.add(item -> {
                // todo: is ignoreCase is right here?
                return username.equalsIgnoreCase(item.getUserName());
            });
        }
    }
    if (locator.getUnusedDimensions().contains(USER)) {
        final String userLocator = locator.getSingleDimensionValue(USER);
        if (userLocator != null) {
            final SUser user = myUserFinder.getItem(userLocator);
            result.add(item -> item.getCommitters().contains(user));
        }
    }
    // TeamCity API: exclude "fake" personal changes created by TeamCity for personal builds without personal changes
    result.add(item -> {
        if (!item.isPersonal())
            return true;
        return item.getChanges().size() > 0;
    });
    final Boolean personal = locator.getSingleDimensionValueAsBoolean(PERSONAL);
    if (personal != null) {
        result.add(item -> FilterUtil.isIncludedByBooleanFilter(personal, item.isPersonal()));
    }
    if (personal != null && personal) {
        // initial collection can contain changes from any buildType/project
        final String buildTypeLocator = locator.getSingleDimensionValue(BUILD_TYPE);
        if (buildTypeLocator != null) {
            final SBuildType buildType = myBuildTypeFinder.getBuildType(null, buildTypeLocator, false);
            result.add(item -> isPersonalChangeMatchesBuildType(item, buildType));
        }
    }
    if (locator.getUnusedDimensions().contains(BUILD)) {
        final String buildLocator = locator.getSingleDimensionValue(BUILD);
        if (buildLocator != null) {
            final Set<Long> buildChanges = getBuildChangeDescriptors(myBuildFinder.getBuildPromotion(null, buildLocator), locator).map(mord -> mord.getRelatedVcsChange().getId()).collect(Collectors.toSet());
            result.add(item -> buildChanges.contains(item.getId()));
        }
    }
    // pre-9.0 dimension compatibility
    if (locator.getUnusedDimensions().contains(PROMOTION)) {
        final Long promotionLocator = locator.getSingleDimensionValueAsLong(PROMOTION);
        if (promotionLocator != null) {
            @SuppressWarnings("ConstantConditions") final Set<Long> buildChanges = getBuildChangeDescriptors(BuildFinder.getBuildPromotion(promotionLocator, myServiceLocator.findSingletonService(BuildPromotionManager.class)), null).map(mord -> mord.getRelatedVcsChange().getId()).collect(Collectors.toSet());
            result.add(item -> buildChanges.contains(item.getId()));
        }
    }
    if (locator.isUnused(BUILD_TYPE)) {
        // todo: support multiple buildTypes here
        final String buildTypeLocator = locator.getSingleDimensionValue(BUILD_TYPE);
        if (buildTypeLocator != null) {
            SBuildType buildType = myBuildTypeFinder.getBuildType(null, buildTypeLocator, false);
            // todo: this does not include "show changes from dependencies", relates to https://youtrack.jetbrains.com/issue/TW-63704
            result.add(item -> item.getRelatedConfigurations().contains(buildType));
        }
    }
    final String projectLocator = locator.getSingleDimensionValue(PROJECT);
    if (projectLocator != null) {
        final SProject project = myProjectFinder.getItem(projectLocator);
        Set<String> btIds = project.getOwnBuildTypes().stream().map(BuildTypeDescriptor::getBuildTypeId).collect(Collectors.toSet());
        result.add(item -> {
            List<String> itemBtIds = ((VcsModificationEx) item).getRelatedConfigurationIds(false);
            for (String itemBtId : itemBtIds) {
                String finalId = RemoteBuildTypeIdUtil.isValidRemoteBuildTypeId(itemBtId) ? RemoteBuildTypeIdUtil.getParentBuildTypeId(itemBtId) : itemBtId;
                if (btIds.contains(finalId)) {
                    return true;
                }
            }
            return false;
        });
    }
    if (locator.isUnused(INTERNAL_VERSION)) {
        final String internalVersion = locator.getSingleDimensionValue(INTERNAL_VERSION);
        if (internalVersion != null) {
            result.add(item -> internalVersion.equals(item.getVersion()));
        }
    }
    if (locator.isUnused(VERSION)) {
        final String displayVersion = locator.getSingleDimensionValue(VERSION);
        ValueCondition condition = ParameterCondition.createValueCondition(displayVersion);
        if (displayVersion != null) {
            result.add(item -> condition.matches(item.getDisplayVersion()));
        }
    }
    final String commentLocatorText = locator.getSingleDimensionValue(COMMENT);
    if (commentLocatorText != null) {
        final String containsText = new Locator(commentLocatorText).getSingleDimensionValue("contains");
        // Preserve legacy behaviour
        if (containsText != null) {
            result.add(item -> item.getDescription().contains(containsText));
        // todo: check unprocessed dimensions
        } else {
            ValueCondition condition = ParameterCondition.createValueCondition(commentLocatorText);
            result.add(item -> condition.matches(item.getDescription()));
        }
    }
    if (locator.getUnusedDimensions().contains(PENDING)) {
        final Boolean pending = locator.getSingleDimensionValueAsBoolean(PENDING);
        if (pending != null) {
            // todo: support multiple buildTypes here
            final String buildTypeLocator = locator.getSingleDimensionValue(BUILD_TYPE);
            final SBuildType buildType = buildTypeLocator == null ? null : myBuildTypeFinder.getBuildType(null, buildTypeLocator, false);
            final Set<SVcsModification> pendingChanges = getPendingChanges(buildType, locator).map(mcd -> mcd.getSVcsModification()).collect(Collectors.toSet());
            result.add(item -> FilterUtil.isIncludedByBooleanFilter(pending, pendingChanges.contains(item)));
        }
    }
    final String fileLocator = locator.getSingleDimensionValue(FILE);
    if (fileLocator != null) {
        // todo: use conditions here
        final String pathLocatorText = new Locator(fileLocator).getSingleDimensionValue("path");
        // todo: check unknown locator dimensions
        if (pathLocatorText != null) {
            final String containsText = new Locator(pathLocatorText).getSingleDimensionValue("contains");
            if (containsText != null) {
                result.add(item -> {
                    for (VcsFileModification vcsFileModification : item.getChanges()) {
                        if (vcsFileModification.getFileName().contains(containsText)) {
                            return true;
                        }
                    }
                    return false;
                });
            // todo: check unknown locator dimensions
            } else {
                ValueCondition condition = ParameterCondition.createValueCondition(pathLocatorText);
                result.add(item -> item.getChanges().stream().map(m -> m.getFileName()).anyMatch(condition::matches));
            }
        }
    }
    if (TeamCityProperties.getBoolean("rest.request.changes.check.enforceChangeViewPermission")) {
        result.add(myPermissionChecker::checkCanView);
    }
    return new UnwrappingFilter<>(result, SVcsModificationOrChangeDescriptor::getSVcsModification);
}
Also used : LocatorProcessException(jetbrains.buildServer.server.rest.errors.LocatorProcessException) WrappingItemHolder(jetbrains.buildServer.server.rest.data.util.WrappingItemHolder) java.util(java.util) SVcsModificationOrChangeDescriptor(jetbrains.buildServer.server.rest.data.change.SVcsModificationOrChangeDescriptor) LocatorName(jetbrains.buildServer.server.rest.swagger.constants.LocatorName) DuplicateChecker(jetbrains.buildServer.server.rest.data.util.DuplicateChecker) LocatorResource(jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource) AuthorityHolder(jetbrains.buildServer.serverSide.auth.AuthorityHolder) UnwrappingFilter(jetbrains.buildServer.server.rest.data.util.UnwrappingFilter) Function(java.util.function.Function) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) LocatorDimensionDataType(jetbrains.buildServer.server.rest.swagger.constants.LocatorDimensionDataType) BuildTypeDescriptor(jetbrains.buildServer.BuildTypeDescriptor) UserChangesFacade(jetbrains.buildServer.serverSide.userChanges.UserChangesFacade) KeyDuplicateChecker(jetbrains.buildServer.server.rest.data.util.KeyDuplicateChecker) StringUtil(jetbrains.buildServer.util.StringUtil) SecurityContext(jetbrains.buildServer.serverSide.auth.SecurityContext) CollectionsUtil(jetbrains.buildServer.util.CollectionsUtil) PagerData(jetbrains.buildServer.server.rest.model.PagerData) Constants(jetbrains.buildServer.server.rest.request.Constants) ServiceLocator(jetbrains.buildServer.ServiceLocator) jetbrains.buildServer.serverSide(jetbrains.buildServer.serverSide) RemoteBuildTypeIdUtil(jetbrains.buildServer.serverSide.impl.RemoteBuildTypeIdUtil) BFSVisitorAdapter(jetbrains.buildServer.util.graph.BFSVisitorAdapter) Predicate(java.util.function.Predicate) VcsModificationEx(jetbrains.buildServer.vcs.impl.VcsModificationEx) DAGIterator(jetbrains.buildServer.util.graph.DAGIterator) jetbrains.buildServer.vcs(jetbrains.buildServer.vcs) Collectors(java.util.stream.Collectors) RemoteBuildType(jetbrains.buildServer.serverSide.impl.RemoteBuildType) Nullable(org.jetbrains.annotations.Nullable) DAG(jetbrains.buildServer.util.graph.DAG) Stream(java.util.stream.Stream) LocatorDimension(jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) AuthUtil(jetbrains.buildServer.serverSide.auth.AuthUtil) NotNull(org.jetbrains.annotations.NotNull) SUser(jetbrains.buildServer.users.SUser) VcsModificationEx(jetbrains.buildServer.vcs.impl.VcsModificationEx) UnwrappingFilter(jetbrains.buildServer.server.rest.data.util.UnwrappingFilter) SUser(jetbrains.buildServer.users.SUser) SVcsModificationOrChangeDescriptor(jetbrains.buildServer.server.rest.data.change.SVcsModificationOrChangeDescriptor) ServiceLocator(jetbrains.buildServer.ServiceLocator) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

java.util (java.util)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 BuildTypeDescriptor (jetbrains.buildServer.BuildTypeDescriptor)1 ServiceLocator (jetbrains.buildServer.ServiceLocator)1 SVcsModificationOrChangeDescriptor (jetbrains.buildServer.server.rest.data.change.SVcsModificationOrChangeDescriptor)1 DuplicateChecker (jetbrains.buildServer.server.rest.data.util.DuplicateChecker)1 KeyDuplicateChecker (jetbrains.buildServer.server.rest.data.util.KeyDuplicateChecker)1 UnwrappingFilter (jetbrains.buildServer.server.rest.data.util.UnwrappingFilter)1 WrappingItemHolder (jetbrains.buildServer.server.rest.data.util.WrappingItemHolder)1 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)1 LocatorProcessException (jetbrains.buildServer.server.rest.errors.LocatorProcessException)1 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)1 PagerData (jetbrains.buildServer.server.rest.model.PagerData)1 Constants (jetbrains.buildServer.server.rest.request.Constants)1 LocatorDimension (jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension)1 LocatorResource (jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource)1 LocatorDimensionDataType (jetbrains.buildServer.server.rest.swagger.constants.LocatorDimensionDataType)1