Search in sources :

Example 11 with BadRequestException

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

the class DataUpdater method createUserGroup.

public SUserGroup createUserGroup(@Nullable final Group groupDescription, @NotNull final ServiceLocator serviceLocator) {
    myDataProvider.checkGlobalPermission(jetbrains.buildServer.serverSide.auth.Permission.CREATE_USERGROUP);
    if (groupDescription == null) {
        throw new BadRequestException("Empty payload received while group details are expected.");
    }
    if (StringUtil.isEmpty(groupDescription.key)) {
        throw new BadRequestException("Attribute 'key' must not be empty when creating group.");
    }
    if (StringUtil.isEmpty(groupDescription.name)) {
        throw new BadRequestException("Attribute 'name' must not be empty when creating group.");
    }
    SUserGroup resultingGroup;
    try {
        resultingGroup = myGroupManager.createUserGroup(groupDescription.key, groupDescription.name, groupDescription.description != null ? groupDescription.description : "");
    } catch (DuplicateKeyException e) {
        throw new BadRequestException("Cannot create group as group with key '" + groupDescription.key + "': group with the same key already exists");
    } catch (DuplicateNameException e) {
        throw new BadRequestException("Cannot create group as group with name '" + groupDescription.name + "': group with the same name already exists");
    }
    if (groupDescription.parentGroups != null) {
        try {
            Group.setGroupParents(resultingGroup, new LinkedHashSet<>(groupDescription.parentGroups.getFromPosted(serviceLocator)), false, serviceLocator);
        } catch (Exception e) {
            myGroupManager.deleteUserGroup(resultingGroup);
            throw new BadRequestException("Cannot create group with specified parent groups", e);
        }
    }
    if (groupDescription.childGroups != null) {
        try {
            groupDescription.childGroups.getFromPosted(serviceLocator).forEach(group -> resultingGroup.addSubgroup(group));
        } catch (Exception e) {
            myGroupManager.deleteUserGroup(resultingGroup);
            throw new BadRequestException("Cannot create group with specified children groups", e);
        }
    }
    if (groupDescription.roleAssignments != null) {
        try {
            Group.setRoles(resultingGroup, groupDescription.roleAssignments, serviceLocator);
        } catch (Exception e) {
            myGroupManager.deleteUserGroup(resultingGroup);
            throw new BadRequestException("Cannot create group with specified roles", e);
        }
    }
    if (groupDescription.properties != null) {
        try {
            resultingGroup.setGroupProperties(groupDescription.properties.getMap().entrySet().stream().collect(Collectors.toMap(e -> new SimplePropertyKey(e.getKey()), e -> e.getValue())));
        } catch (Exception e) {
            myGroupManager.deleteUserGroup(resultingGroup);
            throw new BadRequestException("Cannot create group with specified properties", e);
        }
    }
    if (groupDescription.users != null) {
        try {
            groupDescription.users.getFromPosted(serviceLocator.getSingletonService(UserFinder.class)).forEach(user -> resultingGroup.addUser(user));
        } catch (Exception e) {
            myGroupManager.deleteUserGroup(resultingGroup);
            throw new BadRequestException("Cannot create group with specified users", e);
        }
    }
    return resultingGroup;
}
Also used : BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException)

Example 12 with BadRequestException

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

the class Orders method getComparator.

@NotNull
public Comparator<T> getComparator(@NotNull final String orderLocatorText) {
    Locator locator = new Locator(orderLocatorText, getNames());
    if (locator.isSingleValue()) {
        // noinspection ConstantConditions
        return get(locator.getSingleValue());
    }
    Comparator<T> ALL_EQUAL = (o1, o2) -> 0;
    Comparator<T> comparator = ALL_EQUAL;
    for (Map.Entry<String, Comparator<T>> compPair : myComparators.entrySet()) {
        String name = compPair.getKey();
        String dimension = locator.getSingleDimensionValue(name);
        if (dimension != null) {
            if ("asc".equals(dimension) || "".equals(dimension)) {
                comparator = comparator.thenComparing(compPair.getValue());
            } else if ("desc".equals(dimension)) {
                comparator = comparator.thenComparing(compPair.getValue().reversed());
            } else {
                throw new BadRequestException("Dimension \"" + name + "\" has invalid value \"" + dimension + "\". Should be \"asc\" or \"desc\"");
            }
        }
    }
    locator.checkLocatorFullyProcessed();
    if (comparator == ALL_EQUAL) {
        throw new BadRequestException("No order is defined by the supplied ordering locator");
    }
    return comparator;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) Arrays(java.util.Arrays) Map(java.util.Map) Comparator(java.util.Comparator) Locator(jetbrains.buildServer.server.rest.data.Locator) NotNull(org.jetbrains.annotations.NotNull) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) Locator(jetbrains.buildServer.server.rest.data.Locator) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Comparator(java.util.Comparator) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with BadRequestException

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

the class TestScopesCollector method getItems.

public Stream<TestScope> getItems(@NotNull Locator locator) {
    locator.addSupportedDimensions(SCOPE_TYPE, TEST_OCCURRENCES, ORDER_BY, PagerData.START, PagerData.COUNT);
    locator.addSupportedDimensions(TestScopeFilterImpl.SUPPORTED_DIMENSIONS);
    String scopeName = locator.getSingleDimensionValue(SCOPE_TYPE);
    if (scopeName == null || !SUPPORTED_SCOPES.contains(scopeName)) {
        throw new BadRequestException("Invalid scope. Only scopes " + String.join(",", SUPPORTED_SCOPES) + " are supported.");
    }
    TestScopeFilter filter = myTestScopeFilterProducer.createFromLocator(locator);
    Locator testOccurrencesLocator = new Locator(locator.getSingleDimensionValue(TEST_OCCURRENCES));
    if (!StringUtil.isEmpty(filter.getLocatorString()))
        testOccurrencesLocator.setDimension(TestOccurrenceFinder.SCOPE, filter.getLocatorString());
    PagedSearchResult<STestRun> items = myTestOccurrenceFinder.getItems(testOccurrencesLocator.getStringRepresentation());
    Stream<TestScope> scopes = groupByScope(items, filter, scopeName);
    if (locator.isAnyPresent(ORDER_BY)) {
        String orderDimension = locator.getSingleDimensionValue(ORDER_BY);
        // noinspection ConstantConditions
        scopes = scopes.sorted(SUPPORTED_ORDERS.getComparator(orderDimension));
    }
    if (locator.isAnyPresent(SPLIT_BY_BUILD_TYPE)) {
        Boolean split = locator.getSingleDimensionValueAsBoolean(SPLIT_BY_BUILD_TYPE);
        if (BooleanUtils.isTrue(split)) {
            scopes = splitByBuildType(scopes);
        }
    }
    return scopes;
}
Also used : Locator(jetbrains.buildServer.server.rest.data.Locator) STestRun(jetbrains.buildServer.serverSide.STestRun) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException)

Example 14 with BadRequestException

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

the class PropEntitySnapshotDep method addTo.

@NotNull
@Override
public Dependency addTo(@NotNull final BuildTypeSettingsEx buildType, @NotNull final ServiceLocator serviceLocator) {
    if (!SNAPSHOT_DEPENDENCY_TYPE_NAME.equals(type)) {
        throw new BadRequestException("Snapshot dependency should have type '" + SNAPSHOT_DEPENDENCY_TYPE_NAME + "'.");
    }
    final Map<String, String> propertiesMap = properties == null ? Collections.emptyMap() : properties.getMap();
    // compatibility mode with pre-8.0
    final String buildTypeIdFromProperty = propertiesMap.get(NAME_SOURCE_BUILD_TYPE_ID);
    String buildTypeIdDependOn = getBuildTypeExternalIdForDependency(sourceBuildType, buildTypeIdFromProperty, serviceLocator);
    BuildTypeUtil.checkCanUseBuildTypeAsDependency(buildTypeIdDependOn, serviceLocator);
    Dependency similar = getInheritedOrSameIdSimilar(buildType, serviceLocator);
    if (inherited != null && inherited && similar != null) {
        return similar;
    }
    if (similar != null && id != null && id.equals(similar.getDependOnExternalId())) {
        // todo
        return similar;
    }
    // todo: (TeamCity) for some reason API does not report adding dependency with same id. Seems like it just ignores the call
    if (getSnapshotDepOrNull(buildType, buildTypeIdDependOn) != null) {
        throw new BadRequestException("Snapshot dependency on build type with id '" + buildTypeIdDependOn + "' already exists.");
    }
    final Dependency result = serviceLocator.getSingletonService(DependencyFactory.class).createDependency(buildTypeIdDependOn);
    for (Map.Entry<String, String> property : propertiesMap.entrySet()) {
        if (!NAME_SOURCE_BUILD_TYPE_ID.equals(property.getKey())) {
            setDependencyOption(property.getKey(), property.getValue(), result);
        }
    }
    try {
        buildType.addDependency(result);
    } catch (CyclicDependencyFoundException e) {
        throw new BadRequestException("Error adding dependency", e);
    }
    return getSnapshotDep(buildType, result.getDependOnExternalId(), serviceLocator.getSingletonService(BuildTypeFinder.class));
}
Also used : DependencyFactory(jetbrains.buildServer.serverSide.dependency.DependencyFactory) BuildTypeFinder(jetbrains.buildServer.server.rest.data.BuildTypeFinder) CyclicDependencyFoundException(jetbrains.buildServer.serverSide.dependency.CyclicDependencyFoundException) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) Dependency(jetbrains.buildServer.serverSide.dependency.Dependency) HashMap(java.util.HashMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with BadRequestException

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

the class Change method getChangeFromPosted.

@NotNull
public SVcsModification getChangeFromPosted(final ChangeFinder changeFinder) {
    String locatorText;
    if (submittedId != null) {
        if (submittedLocator != null) {
            throw new BadRequestException("Both 'locator' and 'id' attributes are specified. Only one should be present.");
        }
        final Locator locator = Locator.createEmptyLocator().setDimension(ChangeFinder.DIMENSION_ID, String.valueOf(submittedId));
        if (submittedPersonal != null && submittedPersonal) {
            locator.setDimension(ChangeFinder.PERSONAL, "true");
        }
        locatorText = locator.getStringRepresentation();
    } else {
        if (submittedLocator == null) {
            throw new BadRequestException("No change specified. Either 'id' or 'locator' attribute should be present.");
        }
        locatorText = submittedLocator;
    }
    return changeFinder.getItem(locatorText).getSVcsModification();
}
Also used : Locator(jetbrains.buildServer.server.rest.data.Locator) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) 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