use of jetbrains.buildServer.ServiceLocator in project teamcity-rest by JetBrains.
the class PermissionAssignmentFinder method getPermissions.
@NotNull
private FinderDataBinding.ItemHolder<PermissionAssignmentData> getPermissions(@NotNull final TypedFinderBuilder.DimensionObjects dimensions, @NotNull final AuthorityHolder authorityHolder, @NotNull final ServiceLocator serviceLocator) {
/* The rest of the code in this method is mostly performance optimization producing the same results (with possibly changed sorting).
if (true) {
List<Permission> globalPermissions = authorityHolder.getGlobalPermissions().toList();
Set<Permission> globalPermissionsSet = new HashSet<>(globalPermissions); //TeamCity API issue: this set is used to exclude global permissions from project-level ones
return FinderDataBinding.getItemHolder(Stream.concat(
globalPermissions.stream().map(p -> new PermissionAssignmentData(p)),
authorityHolder.getProjectsPermissions().entrySet().stream().flatMap(
entry -> entry.getValue().toList().stream().filter(p -> !globalPermissionsSet.contains(p)).map(p -> new PermissionAssignmentData(p, entry.getKey())))));
}
*/
List<Permission> permissions_raw = dimensions.get(PERMISSION);
List<List<SProject>> projects_raw = dimensions.get(PROJECT);
if (projects_raw != null && !projects_raw.isEmpty() && projects_raw.size() > 1) {
throw new BadRequestException("Multiple projects dimensions are not supported");
}
@Nullable List<SProject> projects = projects_raw == null || projects_raw.isEmpty() ? null : projects_raw.get(0);
if (permissions_raw != null && !permissions_raw.isEmpty() && permissions_raw.size() > 1) {
throw new BadRequestException("Multiple permissions dimensions are not supported");
}
// permissions_raw is ANDed, permissions is ORed, but so far it is not supported: todo implement
List<Permission> permissions = permissions_raw;
Stream<PermissionAssignmentData> result = Stream.empty();
List<Boolean> global_raw = dimensions.get(GLOBAL);
if (global_raw != null && !global_raw.isEmpty() && global_raw.size() > 1) {
throw new BadRequestException("Multiple global dimensions are not supported");
}
Boolean global = global_raw == null ? null : global_raw.get(0);
if ((permissions == null || permissions.isEmpty())) {
if (projects == null) {
if (global == null || global) {
result = Stream.concat(result, authorityHolder.getGlobalPermissions().toList().stream().map(p -> new PermissionAssignmentData(p)));
}
if (global == null || !global) {
result = Stream.concat(result, authorityHolder.getProjectsPermissions().entrySet().stream().flatMap(entry -> entry.getValue().toList().stream().filter(p -> p.isProjectAssociationSupported()).map(p -> new PermissionAssignmentData(p, entry.getKey()))));
}
return FinderDataBinding.getItemHolder(result);
}
if (global == null || global) {
result = Stream.concat(result, authorityHolder.getGlobalPermissions().toList().stream().filter(p -> p.isProjectAssociationSupported()).map(p -> new PermissionAssignmentData(p)));
}
if (global == null || !global) {
result = Stream.concat(result, projects.stream().flatMap(project -> {
Permissions projectPermissions = authorityHolder.getProjectsPermissions().get(project.getProjectId());
return projectPermissions == null ? Stream.empty() : projectPermissions.toList().stream().filter(p -> p.isProjectAssociationSupported()).map(p -> new PermissionAssignmentData(p, project.getProjectId()));
}));
}
return FinderDataBinding.getItemHolder(result);
}
if (projects == null) {
if (global == null || global) {
result = Stream.concat(result, permissions.stream().filter(p -> authorityHolder.isPermissionGrantedGlobally(p)).map(p -> new PermissionAssignmentData(p)));
}
if (global == null || !global) {
List<SProject> allProjects = serviceLocator.getSingletonService(ProjectManager.class).getProjects();
result = Stream.concat(result, permissions.stream().filter(p -> p.isProjectAssociationSupported()).flatMap(p -> allProjects.stream().filter(project -> {
Permissions projectPermissions = authorityHolder.getProjectsPermissions().get(project.getProjectId());
return projectPermissions != null && projectPermissions.contains(p);
}).map(project -> new PermissionAssignmentData(p, project.getProjectId()))));
}
return FinderDataBinding.getItemHolder(result);
}
if (global == null || global) {
result = Stream.concat(result, permissions.stream().filter(p -> p.isProjectAssociationSupported()).filter(p -> authorityHolder.isPermissionGrantedGlobally(p)).map(p -> new PermissionAssignmentData(p)));
}
if (global == null || !global) {
result = Stream.concat(result, projects.stream().flatMap(project -> permissions.stream().filter(p -> p.isProjectAssociationSupported()).filter(p -> {
Permissions projectPermissions = authorityHolder.getProjectsPermissions().get(project.getProjectId());
return projectPermissions != null && projectPermissions.contains(p);
}).map(p -> new PermissionAssignmentData(p, project.getProjectId()))));
}
return FinderDataBinding.getItemHolder(result);
}
use of jetbrains.buildServer.ServiceLocator in project teamcity-rest by JetBrains.
the class Group method setGroupParents.
public static void setGroupParents(@NotNull final SUserGroup group, @NotNull final Set<SUserGroup> newParents, final boolean revertOnError, @NotNull final ServiceLocator serviceLocator) {
// workaround for TW-52253
serviceLocator.getSingletonService(PermissionChecker.class).getServerActionChecker().checkCanEditUserGroup(group);
Set<SUserGroup> currentParents = group.getParentGroups().stream().map(userGroup -> (SUserGroup) userGroup).collect(Collectors.toSet());
currentParents.stream().filter(userGroup -> !newParents.contains(userGroup)).forEach(userGroup -> userGroup.removeSubgroup(group));
try {
newParents.stream().filter(userGroup -> !currentParents.contains(userGroup)).forEach(userGroup -> userGroup.addSubgroup(group));
} catch (CycleDetectedException e) {
if (revertOnError)
setGroupParents(group, currentParents, false, serviceLocator);
throw new BadRequestException("Error encountered while trying to set new parent groups", e);
} catch (AccessDeniedException e) {
if (revertOnError)
setGroupParents(group, currentParents, false, serviceLocator);
throw e;
} catch (Exception e) {
if (revertOnError)
setGroupParents(group, currentParents, false, serviceLocator);
throw new OperationException("Error encountered while trying to set new parent groups", e);
}
}
use of jetbrains.buildServer.ServiceLocator in project teamcity-rest by JetBrains.
the class Build method getArtifactDependencyChanges.
@NotNull
private static List<BuildChangeData> getArtifactDependencyChanges(@NotNull final BuildPromotion build, @NotNull final ServiceLocator serviceLocator) {
// see also jetbrains.buildServer.server.rest.data.ChangeFinder.getBuildChanges
ArtifactDependencyChangesProvider changesProvider = new ArtifactDependencyChangesProvider(build, ChangeFinder.getBuildChangesPolicy(), serviceLocator.getSingletonService(BuildsManager.class), serviceLocator.getSingletonService(DownloadedArtifactsLoggerImpl.class));
List<ChangeDescriptor> detectedChanges = changesProvider.getDetectedChanges();
if (detectedChanges.isEmpty()) {
return Collections.emptyList();
}
if (detectedChanges.size() > 1) {
throw new OperationException("Unexpected state: more than one (" + detectedChanges.size() + ") artifact changes found");
}
ChangeDescriptor changeDescriptor = detectedChanges.get(0);
if (!ChangeDescriptorConstants.ARTIFACT_DEPENDENCY_CHANGE.equals(changeDescriptor.getType())) {
throw new OperationException("Unexpected state: unknown type of artifact dependency change: '" + changeDescriptor.getType() + "'");
}
try {
Object o = changeDescriptor.getAssociatedData().get(ArtifactDependencyChangesProvider.CHANGED_DEPENDENCIES_ATTR);
// Actually result is List<ArtifactDependencyChangesProvider.ArtifactsChangeDescriptor>
if (o == null) {
return Collections.emptyList();
} else {
// noinspection unchecked
return ((List<ChangeDescriptor>) o).stream().map(descr -> {
Map<String, Object> associatedData = descr.getAssociatedData();
SBuild prevBuild = (SBuild) associatedData.get(ArtifactDependencyChangesProvider.OLD_BUILD_ATTR);
SBuild nextBuild = (SBuild) associatedData.get(ArtifactDependencyChangesProvider.NEW_BUILD_ATTR);
if (prevBuild == null && nextBuild == null)
return null;
return new BuildChangeData(Util.resolveNull(prevBuild, (b) -> b.getBuildPromotion()), Util.resolveNull(nextBuild, (b) -> b.getBuildPromotion()));
}).filter(Objects::nonNull).collect(Collectors.toList());
}
} catch (Exception e) {
throw new OperationException("Unexpected state while getting artifact dependency details: " + e.toString(), e);
}
}
use of jetbrains.buildServer.ServiceLocator in project teamcity-rest by JetBrains.
the class Investigation method getFromPostedAndApply.
@NotNull
public List<InvestigationWrapper> getFromPostedAndApply(@NotNull final ServiceLocator serviceLocator, final boolean allowMultipleResult) {
checkIsValid();
if (target == null) {
throw new BadRequestException("Invalid 'investigation' entity: 'target' should be specified");
}
ProblemTarget.ProblemTargetData targetData;
try {
targetData = target.getFromPosted(serviceLocator);
} catch (BadRequestException e) {
throw new BadRequestException("Invalid 'investigation' entity: " + e.getMessage());
}
ResponsibilityEntry entry = new ResponsibilityEntryEx(TypedFinderBuilder.getEnumValue(state, ResponsibilityEntry.State.class), assignee.getFromPosted(serviceLocator.getSingletonService(UserFinder.class)), serviceLocator.getSingletonService(UserFinder.class).getCurrentUser(), new Date(), assignment == null || assignment.getTextFromPosted() == null ? "" : assignment.getTextFromPosted(), resolution.getFromPostedForInvestigation(serviceLocator));
ResponsibilityFacadeEx responsibilityFacade = serviceLocator.getSingletonService(ResponsibilityFacadeEx.class);
InvestigationFinder investigationFinder = serviceLocator.findSingletonService(InvestigationFinder.class);
assert investigationFinder != null;
List<InvestigationWrapper> resultEntries = new ArrayList<>(1);
if (targetData.isAnyProblem()) {
List<BuildType> buildTypesFromPosted = scope.getBuildTypesFromPosted(serviceLocator);
if (!allowMultipleResult && buildTypesFromPosted.size() > 1) {
throw new OnlySingleEntitySupportedException("Invalid 'scope' entity: for this request only single buildType is supported within 'buildTypes' entity");
}
for (BuildType buildType : buildTypesFromPosted) {
responsibilityFacade.setBuildTypeResponsibility(buildType, entry);
resultEntries.add(investigationFinder.getItem(InvestigationFinder.getLocator((SBuildType) buildType)));
}
} else {
if (scope.buildTypes != null) {
throw new BadRequestException("Invalid 'investigation' entity: Invalid 'scope' entity: 'buildTypes' should not be specified for not buildType-level investigation");
}
SProject project = scope.getProjectFromPosted(serviceLocator);
List<STest> tests = targetData.getTests();
if (!tests.isEmpty()) {
if (!allowMultipleResult && tests.size() > 1) {
throw new OnlySingleEntitySupportedException("Invalid 'target' entity: for this request only single test is supported within 'tests' entity");
}
responsibilityFacade.setTestNameResponsibility(tests.stream().map(sTest -> sTest.getName()).distinct().collect(Collectors.toList()), project.getProjectId(), entry);
tests.stream().map(test -> // only one item should be found in the project
investigationFinder.getItem(InvestigationFinder.getLocatorForTest(test.getTestNameId(), project))).distinct().forEachOrdered(resultEntries::add);
}
List<Long> problems = targetData.getProblemIds();
if (!problems.isEmpty()) {
if (!allowMultipleResult && problems.size() > 1) {
throw new OnlySingleEntitySupportedException("Invalid 'target' entity: for this request only single problem is supported within 'problems' entity");
}
responsibilityFacade.setBuildProblemResponsibility(// seems like only id is used inside
problems.stream().distinct().map(problemId -> ProblemWrapper.getBuildProblemInfo(problemId.intValue(), project.getProjectId())).collect(Collectors.toList()), project.getProjectId(), entry);
problems.stream().distinct().map(problemId -> // only one item should be found in the project
investigationFinder.getItem(InvestigationFinder.getLocatorForProblem(problemId.intValue(), project))).forEachOrdered(resultEntries::add);
}
}
if (!allowMultipleResult && resultEntries.size() != 1) {
throw new BadRequestException("Invalid 'investigation' entity: Invalid 'target' entity: found " + resultEntries.size() + " result entities, while exactly one is required");
}
return resultEntries;
}
use of jetbrains.buildServer.ServiceLocator in project teamcity-rest by JetBrains.
the class TagFinder method getFilterOptions.
@Nullable
public static FilterOptions getFilterOptions(@NotNull final List<String> tagLocators, @NotNull final ServiceLocator serviceLocator) {
// so far supporting only single tag filter
if (tagLocators.size() != 1)
return null;
String tagLocator = tagLocators.get(0);
TagFinder tagFinder = new TagFinder(serviceLocator.getSingletonService(UserFinder.class), null, true);
// the locator is not checked later with checkFullyProcessed as the result of the method is partial and the locator should still be processed later
Locator locator = tagFinder.createLocator(tagLocator, getDefaultLocator());
// no effective API to filter by tag name in the case-insensitive way as it is supported by the main filter
if (locator.getSingleDimensionValue(NAME) != null)
return null;
String tagName = locator.getSingleValue();
final String condition = locator.getSingleDimensionValue(CONDITION);
if (condition != null) {
final ValueCondition valueCondition = ParameterCondition.createValueCondition(condition);
tagName = valueCondition.getConstantValueIfSimpleEqualsCondition();
}
// no case sensitive tag name is set
if (tagName == null)
return null;
// this is set to false by locator defaults
final Boolean privateDimension = locator.isSingleValue() ? Boolean.FALSE : locator.getSingleDimensionValueAsBoolean(PRIVATE);
final String ownerLocator = locator.getSingleDimensionValue(OWNER);
if (ownerLocator != null && privateDimension != null && privateDimension) {
SUser user = tagFinder.myUserFinder.getItem(ownerLocator);
return new FilterOptions(tagName, user);
}
if (ownerLocator == null && privateDimension != null && !privateDimension) {
return new FilterOptions(tagName, null);
}
return null;
}
Aggregations