Search in sources :

Example 1 with VcsRoot

use of jetbrains.buildServer.server.rest.model.change.VcsRoot in project teamcity-rest by JetBrains.

the class VcsRootInstanceFinder method getPrefilteredItems.

@NotNull
@Override
public ItemHolder<VcsRootInstance> getPrefilteredItems(@NotNull Locator locator) {
    setLocatorDefaults(locator);
    // should check it not in Filter as it considers current scope
    Boolean versionedSettingsUsagesOnly = locator.getSingleDimensionValueAsBoolean(HAS_VERSIONED_SETTINGS_ONLY);
    final String build = locator.getSingleDimensionValue(BUILD);
    if (build != null) {
        Stream<VcsRootInstance> vcsRootInstancesByBuilds = getVcsRootInstancesByBuilds(build);
        if (BooleanUtils.isTrue(versionedSettingsUsagesOnly)) {
            vcsRootInstancesByBuilds = vcsRootInstancesByBuilds.filter(vcsRootInstance -> vcsRootInstance.equals(myVersionedSettingsManager.getVersionedSettingsVcsRootInstance(vcsRootInstance.getParent().getProject())));
        } else if (BooleanUtils.isFalse(versionedSettingsUsagesOnly)) {
            vcsRootInstancesByBuilds = vcsRootInstancesByBuilds.filter(vcsRootInstance -> !vcsRootInstance.equals(myVersionedSettingsManager.getVersionedSettingsVcsRootInstance(vcsRootInstance.getParent().getProject())));
        }
        return FinderDataBinding.getItemHolder(vcsRootInstancesByBuilds);
    }
    final String vcsRootLocator = locator.getSingleDimensionValue(VCS_ROOT_DIMENSION);
    if (vcsRootLocator != null) {
        final List<SVcsRoot> vcsRoots = myVcsRootFinder.getItemsNotEmpty(vcsRootLocator).myEntries;
        final Set<VcsRootInstance> result = new TreeSet<>(VCS_ROOT_INSTANCE_COMPARATOR);
        final String buildTypesLocator = locator.getSingleDimensionValue(BUILD_TYPE);
        Predicate<SBuildType> filter;
        Set<SProject> projects;
        if (buildTypesLocator != null) {
            if (versionedSettingsUsagesOnly == null || !versionedSettingsUsagesOnly) {
                // is used below in the same condition
                ItemFilter<BuildTypeOrTemplate> buildTypeFilter = myBuildTypeFinder.getFilter(buildTypesLocator);
                filter = sBuildType -> buildTypeFilter.isIncluded(new BuildTypeOrTemplate(sBuildType));
            } else {
                filter = (a) -> true;
            }
            if (versionedSettingsUsagesOnly == null || versionedSettingsUsagesOnly) {
                // is used below in the same condition
                projects = myBuildTypeFinder.getItemsNotEmpty(buildTypesLocator).myEntries.stream().map(BuildTypeOrTemplate::getProject).collect(Collectors.toSet());
            } else {
                projects = null;
            }
        } else {
            filter = (a) -> true;
            projects = null;
        }
        filterOutUnrelatedWithoutParameterResolution(locator, vcsRoots);
        for (SVcsRoot vcsRoot : vcsRoots) {
            if (versionedSettingsUsagesOnly == null || !versionedSettingsUsagesOnly) {
                vcsRoot.getUsagesInConfigurations().stream().filter(filter).map(buildType -> buildType.getVcsRootInstanceForParent(vcsRoot)).filter(// minor performance optimization not to return roots which will be filtered in the filter
                rootInstance -> ((rootInstance != null) && hasPermission(Permission.VIEW_BUILD_CONFIGURATION_SETTINGS, rootInstance))).forEach(result::add);
            }
            if (versionedSettingsUsagesOnly == null || versionedSettingsUsagesOnly) {
                Set<SProject> projectsBySettingsRoot = myVersionedSettingsManager.getProjectsBySettingsRoot(vcsRoot);
                result.addAll(getSettingsRootInstances(projects == null ? projectsBySettingsRoot : CollectionsUtil.intersect(projectsBySettingsRoot, projects)));
            }
        }
        return getItemHolder(result);
    }
    final String buildTypesLocator = locator.getSingleDimensionValue(BUILD_TYPE);
    if (buildTypesLocator != null) {
        return getItemHolder(getInstances(buildTypesLocator, versionedSettingsUsagesOnly));
    }
    // todo: support multiple here for "from all not archived projects" case
    final String projectLocator = locator.getSingleDimensionValue(AFFECTED_PROJECT);
    if (projectLocator != null) {
        return getItemHolder(getVcsRootInstancesUnderProject(myProjectFinder.getItem(projectLocator), versionedSettingsUsagesOnly));
    }
    // todo: (TeamCity) open API is there a better way to do this?
    // if reworked, can use checkPermission(Permission.VIEW_BUILD_CONFIGURATION_SETTINGS, item);
    // when implemented, can also add to jetbrains.buildServer.usageStatistics.impl.providers.StaticServerUsageStatisticsProvider.publishNumberOfVcsRoots()
    final Set<VcsRootInstance> result = new TreeSet<>(VCS_ROOT_INSTANCE_COMPARATOR);
    if (versionedSettingsUsagesOnly == null || !versionedSettingsUsagesOnly) {
        for (SBuildType buildType : myProjectManager.getAllBuildTypes()) {
            if (myPermissionChecker.isPermissionGranted(Permission.VIEW_BUILD_CONFIGURATION_SETTINGS, buildType.getProjectId())) {
                result.addAll(buildType.getVcsRootInstances());
            }
        }
    }
    if (versionedSettingsUsagesOnly == null || versionedSettingsUsagesOnly) {
        result.addAll(getSettingsRootInstances(myProjectManager.getProjects()));
    }
    return getItemHolder(result);
}
Also used : java.util(java.util) AuthorizationFailedException(jetbrains.buildServer.server.rest.errors.AuthorizationFailedException) LocatorName(jetbrains.buildServer.server.rest.swagger.constants.LocatorName) LocatorResource(jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource) BuildTypeOrTemplate(jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate) BooleanUtils(org.apache.commons.lang3.BooleanUtils) VersionedSettingsManager(jetbrains.buildServer.serverSide.versionedSettings.VersionedSettingsManager) ParametersProvider(jetbrains.buildServer.parameters.ParametersProvider) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) LocatorDimensionDataType(jetbrains.buildServer.server.rest.swagger.constants.LocatorDimensionDataType) AbstractMapParametersProvider(jetbrains.buildServer.parameters.impl.AbstractMapParametersProvider) VcsRoot(jetbrains.buildServer.server.rest.model.change.VcsRoot) ProjectManager(jetbrains.buildServer.serverSide.ProjectManager) SBuildType(jetbrains.buildServer.serverSide.SBuildType) Logger(com.intellij.openapi.diagnostic.Logger) CollectionsUtil(jetbrains.buildServer.util.CollectionsUtil) PagerData(jetbrains.buildServer.server.rest.model.PagerData) Constants(jetbrains.buildServer.server.rest.request.Constants) ServiceLocator(jetbrains.buildServer.ServiceLocator) Predicate(java.util.function.Predicate) jetbrains.buildServer.vcs(jetbrains.buildServer.vcs) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) CommonLocatorDimensionsList(jetbrains.buildServer.server.rest.swagger.constants.CommonLocatorDimensionsList) Stream(java.util.stream.Stream) LocatorDimension(jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension) Permission(jetbrains.buildServer.serverSide.auth.Permission) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) SProject(jetbrains.buildServer.serverSide.SProject) NotNull(org.jetbrains.annotations.NotNull) ReferencesResolverUtil(jetbrains.buildServer.parameters.ReferencesResolverUtil) BuildTypeOrTemplate(jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate) SProject(jetbrains.buildServer.serverSide.SProject) SBuildType(jetbrains.buildServer.serverSide.SBuildType) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with VcsRoot

use of jetbrains.buildServer.server.rest.model.change.VcsRoot in project teamcity-rest by JetBrains.

the class BuildTypeRequestTest method testUpdatingVcsRoots.

@Test
public void testUpdatingVcsRoots() {
    BuildTypeImpl buildType1 = registerBuildType("buildType1", "projectName");
    buildType1.addVcsRoot(createVcsRoot("name1", null));
    buildType1.addVcsRoot(createVcsRoot("name2", null));
    buildType1.addVcsRoot(createVcsRoot("name3", null));
    String newRootId = createVcsRoot("name4", null).getExternalId();
    final String btLocator = "id:" + buildType1.getExternalId();
    assertEquals(3, myBuildTypeRequest.getVcsRootEntries(btLocator, "$long,vcs-root-entry($long)").vcsRootAssignments.size());
    {
        VcsRootEntry submitted = new VcsRootEntry();
        submitted.vcsRoot = new VcsRoot();
        submitted.vcsRoot.id = newRootId;
        myBuildTypeRequest.addVcsRootEntry(btLocator, submitted, "$long");
        assertEquals(4, myBuildTypeRequest.getVcsRootEntries(btLocator, "$long,vcs-root-entry($long)").vcsRootAssignments.size());
        myBuildTypeRequest.deleteVcsRootEntry(btLocator, newRootId);
        assertEquals(3, myBuildTypeRequest.getVcsRootEntries(btLocator, "$long,vcs-root-entry($long)").vcsRootAssignments.size());
    }
    buildType1.getSettings().addListener(new BuildTypeSettingsAdapter() {

        private int myTriggerOnCall = 1;

        @Override
        public void afterAddVcsRoot(@NotNull final SVcsRoot vcsRoot) {
            if (--myTriggerOnCall == 0) {
                throw new RuntimeException("I need error here ");
            }
        }
    });
    {
        VcsRootEntries submitted = new VcsRootEntries();
        VcsRootEntry submitted1 = new VcsRootEntry();
        submitted1.vcsRoot = new VcsRoot();
        submitted1.vcsRoot.id = newRootId;
        submitted.vcsRootAssignments = Arrays.asList(submitted1);
        checkException(RuntimeException.class, new Runnable() {

            public void run() {
                myBuildTypeRequest.replaceVcsRootEntries(btLocator, submitted, "$long");
            }
        }, null);
        assertEquals(3, myBuildTypeRequest.getVcsRootEntries(btLocator, "$long,vcs-root-entry($long)").vcsRootAssignments.size());
        assertEquals(3, buildType1.getVcsRootEntries().size());
    }
    buildType1.getSettings().addListener(new BuildTypeSettingsAdapter() {

        private int myTriggerOnCall = 1;

        @Override
        public void afterAddVcsRoot(@NotNull final SVcsRoot vcsRoot) {
            if (--myTriggerOnCall == 0) {
                throw new RuntimeException("I need error here ");
            }
        }
    });
    {
        VcsRootEntry submitted = new VcsRootEntry();
        submitted.vcsRoot = new VcsRoot();
        submitted.vcsRoot.id = newRootId;
        checkException(RuntimeException.class, new Runnable() {

            public void run() {
                myBuildTypeRequest.addVcsRootEntry(btLocator, submitted, "$long");
            }
        }, null);
        assertEquals(3, myBuildTypeRequest.getVcsRootEntries(btLocator, "$long,vcs-root-entry($long)").vcsRootAssignments.size());
        assertEquals(3, buildType1.getVcsRootEntries().size());
    }
    {
        myBuildTypeRequest.replaceVcsRootEntries(btLocator, new VcsRootEntries(), "$long");
        assertEquals(0, buildType1.getVcsRootEntries().size());
    }
}
Also used : SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) BuildTypeImpl(jetbrains.buildServer.serverSide.impl.BuildTypeImpl) VcsRoot(jetbrains.buildServer.server.rest.model.change.VcsRoot) SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) Test(org.testng.annotations.Test) BaseFinderTest(jetbrains.buildServer.server.rest.data.BaseFinderTest)

Example 3 with VcsRoot

use of jetbrains.buildServer.server.rest.model.change.VcsRoot in project teamcity-rest by JetBrains.

the class VcsRootInstanceFinder method filterOutUnrelatedWithoutParameterResolution.

private void filterOutUnrelatedWithoutParameterResolution(@NotNull final Locator locator, @NotNull final List<SVcsRoot> vcsRoots) {
    final List<String> properties = locator.lookupDimensionValue(PROPERTY);
    if (properties.isEmpty())
        return;
    final Matcher<ParametersProvider> parameterCondition = ParameterCondition.create(properties);
    for (Iterator<SVcsRoot> iterator = vcsRoots.iterator(); iterator.hasNext(); ) {
        SVcsRoot vcsRoot = iterator.next();
        Map<String, String> propertiesMap = vcsRoot.getProperties();
        try {
            // this assumes something about matcher: e.g. that it's logic does not change while processing items
            boolean matches = parameterCondition.matches(new ParametersProvider() {

                @Override
                public String get(@NotNull final String key) {
                    String value = propertiesMap.get(key);
                    if (value != null && ReferencesResolverUtil.containsReference(value)) {
                        throw CannedException.INSTANCE;
                    }
                    return value;
                }

                @Override
                public int size() {
                    return propertiesMap.size();
                }

                @Override
                public Map<String, String> getAll() {
                    if (propertiesMap.entrySet().stream().anyMatch(e -> ReferencesResolverUtil.containsReference(e.getValue()))) {
                        throw CannedException.INSTANCE;
                    }
                    return propertiesMap;
                }
            });
            // while filtering, no reference was encountered: remove from the collection if it does not match the condition
            if (!matches)
                iterator.remove();
        } catch (CannedException ignore) {
        // encountered a reference: preserve in the collection
        }
    }
}
Also used : java.util(java.util) AuthorizationFailedException(jetbrains.buildServer.server.rest.errors.AuthorizationFailedException) LocatorName(jetbrains.buildServer.server.rest.swagger.constants.LocatorName) LocatorResource(jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource) BuildTypeOrTemplate(jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate) BooleanUtils(org.apache.commons.lang3.BooleanUtils) VersionedSettingsManager(jetbrains.buildServer.serverSide.versionedSettings.VersionedSettingsManager) ParametersProvider(jetbrains.buildServer.parameters.ParametersProvider) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) LocatorDimensionDataType(jetbrains.buildServer.server.rest.swagger.constants.LocatorDimensionDataType) AbstractMapParametersProvider(jetbrains.buildServer.parameters.impl.AbstractMapParametersProvider) VcsRoot(jetbrains.buildServer.server.rest.model.change.VcsRoot) ProjectManager(jetbrains.buildServer.serverSide.ProjectManager) SBuildType(jetbrains.buildServer.serverSide.SBuildType) Logger(com.intellij.openapi.diagnostic.Logger) CollectionsUtil(jetbrains.buildServer.util.CollectionsUtil) PagerData(jetbrains.buildServer.server.rest.model.PagerData) Constants(jetbrains.buildServer.server.rest.request.Constants) ServiceLocator(jetbrains.buildServer.ServiceLocator) Predicate(java.util.function.Predicate) jetbrains.buildServer.vcs(jetbrains.buildServer.vcs) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) CommonLocatorDimensionsList(jetbrains.buildServer.server.rest.swagger.constants.CommonLocatorDimensionsList) Stream(java.util.stream.Stream) LocatorDimension(jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension) Permission(jetbrains.buildServer.serverSide.auth.Permission) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) SProject(jetbrains.buildServer.serverSide.SProject) NotNull(org.jetbrains.annotations.NotNull) ReferencesResolverUtil(jetbrains.buildServer.parameters.ReferencesResolverUtil) ParametersProvider(jetbrains.buildServer.parameters.ParametersProvider) AbstractMapParametersProvider(jetbrains.buildServer.parameters.impl.AbstractMapParametersProvider)

Example 4 with VcsRoot

use of jetbrains.buildServer.server.rest.model.change.VcsRoot in project teamcity-rest by JetBrains.

the class VcsRootRequest method addRoot.

@POST
@Consumes({ "application/xml", "application/json" })
@Produces({ "application/xml", "application/json" })
@ApiOperation(value = "Add a new VCS root.", nickname = "addVcsRoot")
public VcsRoot addRoot(VcsRoot vcsRootDescription, @QueryParam("fields") String fields) {
    checkVcsRootDescription(vcsRootDescription);
    BeanContext ctx = new BeanContext(myDataProvider.getBeanFactory(), myServiceLocator, myApiUrlBuilder);
    // todo: TeamCity openAPI: not consistent methods for creating VCS root with/without id
    final SVcsRoot newVcsRoot = getVcsRootProject(vcsRootDescription, ctx).createVcsRoot(vcsRootDescription.vcsName, vcsRootDescription.name, vcsRootDescription.properties.getMap());
    if (vcsRootDescription.id != null) {
        newVcsRoot.setExternalId(vcsRootDescription.id);
    }
    if (vcsRootDescription.modificationCheckInterval != null) {
        newVcsRoot.setModificationCheckInterval(vcsRootDescription.modificationCheckInterval);
    }
    newVcsRoot.schedulePersisting("A new VCS root created");
    return new VcsRoot(newVcsRoot, new Fields(fields), myBeanContext);
}
Also used : BeanContext(jetbrains.buildServer.server.rest.util.BeanContext) SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) Fields(jetbrains.buildServer.server.rest.model.Fields) VcsRoot(jetbrains.buildServer.server.rest.model.change.VcsRoot) SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

VcsRoot (jetbrains.buildServer.server.rest.model.change.VcsRoot)4 Logger (com.intellij.openapi.diagnostic.Logger)2 java.util (java.util)2 Predicate (java.util.function.Predicate)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 ServiceLocator (jetbrains.buildServer.ServiceLocator)2 ParametersProvider (jetbrains.buildServer.parameters.ParametersProvider)2 ReferencesResolverUtil (jetbrains.buildServer.parameters.ReferencesResolverUtil)2 AbstractMapParametersProvider (jetbrains.buildServer.parameters.impl.AbstractMapParametersProvider)2 AuthorizationFailedException (jetbrains.buildServer.server.rest.errors.AuthorizationFailedException)2 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)2 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)2 PagerData (jetbrains.buildServer.server.rest.model.PagerData)2 Constants (jetbrains.buildServer.server.rest.request.Constants)2 LocatorDimension (jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension)2 LocatorResource (jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource)2 CommonLocatorDimensionsList (jetbrains.buildServer.server.rest.swagger.constants.CommonLocatorDimensionsList)2 LocatorDimensionDataType (jetbrains.buildServer.server.rest.swagger.constants.LocatorDimensionDataType)2 LocatorName (jetbrains.buildServer.server.rest.swagger.constants.LocatorName)2