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);
}
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());
}
}
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
}
}
}
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);
}
Aggregations