Search in sources :

Example 76 with BadRequestException

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

the class Build method getLocatorFromPosted.

@NotNull
public String getLocatorFromPosted(@NotNull final Map<Long, Long> buildPromotionIdQueuedBuildsReplacements) {
    String locatorText;
    if (submittedLocator != null) {
        if (submittedPromotionId != null) {
            throw new BadRequestException("Both 'locator' and '" + BuildPromotionFinder.PROMOTION_ID + "' attributes are specified. Only one should be present.");
        }
        if (submittedId != null) {
            throw new BadRequestException("Both 'locator' and '" + BuildPromotionFinder.DIMENSION_ID + "' attributes are specified. Only one should be present.");
        }
        locatorText = submittedLocator;
    } else {
        final Locator locator = Locator.createEmptyLocator();
        if (submittedPromotionId != null) {
            final Long replacementPromotionId = buildPromotionIdQueuedBuildsReplacements.get(submittedPromotionId);
            if (replacementPromotionId != null) {
                locator.setDimension(BuildPromotionFinder.PROMOTION_ID, String.valueOf(replacementPromotionId));
            } else {
                locator.setDimension(BuildPromotionFinder.PROMOTION_ID, String.valueOf(submittedPromotionId));
            }
        }
        if (submittedId != null) {
            // assuming https://youtrack.jetbrains.com/issue/TW-38777 never takes place
            final Long replacementPromotionId = buildPromotionIdQueuedBuildsReplacements.get(submittedId);
            if (replacementPromotionId != null) {
                locator.setDimension(BuildPromotionFinder.PROMOTION_ID, String.valueOf(replacementPromotionId));
            } else {
                locator.setDimension(BuildPromotionFinder.DIMENSION_ID, String.valueOf(submittedId));
            }
        }
        if (locator.isEmpty()) {
            throw new BadRequestException("No build specified. Either '" + BuildPromotionFinder.DIMENSION_ID + "' or 'locator' attributes should be present.");
        }
        locatorText = locator.getStringRepresentation();
    }
    return locatorText;
}
Also used : ServiceLocator(jetbrains.buildServer.ServiceLocator) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) NotNull(org.jetbrains.annotations.NotNull)

Example 77 with BadRequestException

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

the class AgentPool method setFieldValue.

public static void setFieldValue(@NotNull final jetbrains.buildServer.serverSide.agentPools.AgentPool agentPool, @NotNull final String fieldName, @Nullable final String newValue, @NotNull final BeanContext beanContext) {
    String newName = agentPool.getName();
    int minAgents = agentPool.getMinAgents();
    int maxAgents = agentPool.getMaxAgents();
    boolean modificationsFound = false;
    if ("name".equals(fieldName)) {
        if (StringUtil.isEmpty(newValue)) {
            throw new BadRequestException("Agent pool name cannot be empty");
        } else {
            newName = newValue;
        }
        modificationsFound = true;
    } else if ("maxAgents".equals(fieldName)) {
        if (StringUtil.isEmpty(newValue)) {
            maxAgents = AgentPoolLimits.DEFAULT.getMaxAgents();
        } else {
            maxAgents = Integer.valueOf(newValue);
        }
        modificationsFound = true;
    }
    if (!modificationsFound) {
        throw new BadRequestException("Setting field '" + fieldName + "' is not supported. Supported are: name, maxAgents.");
    }
    try {
        beanContext.getSingletonService(AgentPoolManager.class).updateAgentPool(agentPool.getAgentPoolId(), newName, new AgentPoolLimitsImpl(minAgents, maxAgents));
    } catch (NoSuchAgentPoolException e) {
        throw new BadRequestException(e.getMessage());
    } catch (AgentPoolCannotBeRenamedException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException)

Example 78 with BadRequestException

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

the class Property method getFromPosted.

@NotNull
public Parameter getFromPosted(@NotNull final ServiceLocator serviceLocator) {
    isValid();
    final ParameterFactory parameterFactory = serviceLocator.getSingletonService(ParameterFactory.class);
    if (type == null || type.rawValue == null) {
        return parameterFactory.createSimpleParameter(name, value);
    }
    // workaround for https://youtrack.jetbrains.com/issue/TW-41041
    final ParameterDescriptionFactory parameterDescriptionFactory = serviceLocator.getSingletonService(jetbrains.buildServer.serverSide.parameters.ParameterDescriptionFactory.class);
    try {
        parameterDescriptionFactory.parseDescription(type.rawValue);
    } catch (ParseException e) {
        throw new BadRequestException("Wrong parameter type \"" + type.rawValue + "\" provided: " + e.getMessage());
    }
    return parameterFactory.createTypedParameter(name, value, type.rawValue);
}
Also used : ParameterFactory(jetbrains.buildServer.serverSide.parameters.ParameterFactory) ParameterDescriptionFactory(jetbrains.buildServer.serverSide.parameters.ParameterDescriptionFactory) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) ParseException(java.text.ParseException) NotNull(org.jetbrains.annotations.NotNull)

Example 79 with BadRequestException

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

the class Property method addTo.

@NotNull
public Parameter addTo(@NotNull final EntityWithModifiableParameters entity, @NotNull final ServiceLocator serviceLocator) {
    if (StringUtil.isEmpty(name)) {
        throw new BadRequestException("Parameter name cannot be empty.");
    }
    Parameter originalParameter = entity.getParameter(name);
    Parameter originalOwnParameter = entity.getOwnParameter(name);
    if (inherited != null && inherited) {
        if (originalParameter != null && isSimilar(originalParameter, serviceLocator))
            return originalParameter;
    }
    Parameter fromPosted = getFromPosted(serviceLocator);
    try {
        entity.addParameter(fromPosted);
        return fromPosted;
    } catch (Exception e) {
        // restore
        if (originalOwnParameter != null) {
            entity.addParameter(originalOwnParameter);
        } else if (entity.getParameter(name) != null) {
            entity.removeParameter(name);
        }
        throw new BadRequestException("Cannot set parameter '" + name + "' to value '" + fromPosted.getValue() + "': " + e.toString(), e);
    }
}
Also used : BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) Parameter(jetbrains.buildServer.serverSide.Parameter) LocatorProcessException(jetbrains.buildServer.server.rest.errors.LocatorProcessException) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) ParseException(java.text.ParseException) InvalidStateException(jetbrains.buildServer.server.rest.errors.InvalidStateException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) NotNull(org.jetbrains.annotations.NotNull)

Example 80 with BadRequestException

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

the class TestFinder method findSingleItem.

@Override
@Nullable
public STest findSingleItem(@NotNull final Locator locator) {
    if (locator.isSingleValue()) {
        // no dimensions found, assume it's id
        final Long parsedId = locator.getSingleValueAsLong();
        if (parsedId == null) {
            throw new BadRequestException("Expecting id, found empty value.");
        }
        STest item = findTest(parsedId);
        if (item == null) {
            throw new NotFoundException("No test can be found by id '" + parsedId + "' on the entire server.");
        }
        return item;
    }
    // dimension-specific item search
    Long id = locator.getSingleDimensionValueAsLong(DIMENSION_ID);
    if (id != null) {
        STest item = findTest(id);
        if (item == null) {
            throw new NotFoundException("No test" + " can be found by " + DIMENSION_ID + " '" + id + "' on the entire server.");
        }
        return item;
    }
    String nameDimension = locator.getSingleDimensionValue(NAME);
    if (nameDimension != null) {
        ValueCondition nameCondition = ParameterCondition.createValueConditionFromPlainValueOrCondition(nameDimension);
        String constantName = nameCondition.getConstantValueIfSimpleEqualsCondition();
        if (constantName == null) {
            return null;
        }
        final Long testNameId = myTestName2Index.findTestNameId(new TestName(constantName));
        if (testNameId == null) {
            throw new NotFoundException("No test can be found by " + NAME + " '" + constantName + "' on the entire server.");
        }
        STest test = findTest(testNameId);
        if (test == null) {
            throw new NotFoundException("No test can be found by id corresponding to " + NAME + " '" + constantName + "' on the entire server.");
        }
        return test;
    }
    return null;
}
Also used : TestName(jetbrains.buildServer.tests.TestName) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) Nullable(org.jetbrains.annotations.Nullable)

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