Search in sources :

Example 11 with NotFoundException

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

the class BuildTypeRequest method getTemplateById.

@NotNull
private BuildTypeOrTemplate getTemplateById(@NotNull final SBuildType buildType, @NotNull final String templateExternalId, final boolean onlyOwn) {
    if (onlyOwn) {
        return new BuildTypeOrTemplate(buildType.getOwnTemplates().stream().filter(t -> t.getExternalId().equals(templateExternalId)).findFirst().orElseThrow(() -> new NotFoundException("Build type " + LogUtil.describe(buildType) + " does not have own template with id \"" + templateExternalId + "\"")));
    }
    Set<String> ownTemplatesIds = buildType.getOwnTemplates().stream().map(t -> t.getInternalId()).collect(Collectors.toSet());
    BuildTypeOrTemplate result = new BuildTypeOrTemplate(buildType.getTemplates().stream().filter(t -> t.getExternalId().equals(templateExternalId)).findFirst().orElseThrow(() -> new NotFoundException("Build type " + LogUtil.describe(buildType) + " does not have template with id \"" + templateExternalId + "\"")));
    result.markInherited(!ownTemplatesIds.contains(result.getInternalId()));
    return result;
}
Also used : CheckoutRules(jetbrains.buildServer.vcs.CheckoutRules) AuthorityHolder(jetbrains.buildServer.serverSide.auth.AuthorityHolder) BuildTypeOrTemplate(jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate) ApiParam(io.swagger.annotations.ApiParam) jetbrains.buildServer.server.rest.data(jetbrains.buildServer.server.rest.data) InvestigationFinder(jetbrains.buildServer.server.rest.data.investigations.InvestigationFinder) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) Dependency(jetbrains.buildServer.serverSide.dependency.Dependency) ApiOperation(io.swagger.annotations.ApiOperation) BUILD_TYPE(jetbrains.buildServer.server.rest.data.ProjectFinder.BUILD_TYPE) SArtifactDependency(jetbrains.buildServer.serverSide.artifacts.SArtifactDependency) StringUtil(jetbrains.buildServer.util.StringUtil) Logger(com.intellij.openapi.diagnostic.Logger) CollectionsUtil(jetbrains.buildServer.util.CollectionsUtil) Context(javax.ws.rs.core.Context) ServiceLocator(jetbrains.buildServer.ServiceLocator) Branches(jetbrains.buildServer.server.rest.model.build.Branches) jetbrains.buildServer.serverSide(jetbrains.buildServer.serverSide) ProcessingResult(jetbrains.buildServer.parameters.ProcessingResult) VcsLabelingBuildFeature(jetbrains.buildServer.serverSide.impl.VcsLabelingBuildFeature) Collectors(java.util.stream.Collectors) BeanFactory(jetbrains.buildServer.server.rest.util.BeanFactory) Nullable(org.jetbrains.annotations.Nullable) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) Pattern(java.util.regex.Pattern) UriInfo(javax.ws.rs.core.UriInfo) BeanContext(jetbrains.buildServer.server.rest.util.BeanContext) NotNull(org.jetbrains.annotations.NotNull) LogUtil(jetbrains.buildServer.log.LogUtil) LocatorProcessException(jetbrains.buildServer.server.rest.errors.LocatorProcessException) java.util(java.util) AuthorizationFailedException(jetbrains.buildServer.server.rest.errors.AuthorizationFailedException) LocatorName(jetbrains.buildServer.server.rest.swagger.constants.LocatorName) Converter(jetbrains.buildServer.util.Converter) Tags(jetbrains.buildServer.server.rest.model.build.Tags) jetbrains.buildServer.server.rest.model(jetbrains.buildServer.server.rest.model) Element(jetbrains.buildServer.util.browser.Element) HttpServletRequest(javax.servlet.http.HttpServletRequest) BuildTypeIdentifiersManager(jetbrains.buildServer.serverSide.identifiers.BuildTypeIdentifiersManager) ParametersPersistableEntity(jetbrains.buildServer.server.rest.data.parameters.ParametersPersistableEntity) BuildTriggerDescriptor(jetbrains.buildServer.buildTriggers.BuildTriggerDescriptor) Api(io.swagger.annotations.Api) Build(jetbrains.buildServer.server.rest.model.build.Build) SecurityContext(jetbrains.buildServer.serverSide.auth.SecurityContext) VcsRootInstance(jetbrains.buildServer.vcs.VcsRootInstance) jetbrains.buildServer.server.rest.model.buildType(jetbrains.buildServer.server.rest.model.buildType) SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) Properties(jetbrains.buildServer.server.rest.model.Properties) Builds(jetbrains.buildServer.server.rest.model.build.Builds) CachingValue(jetbrains.buildServer.server.rest.util.CachingValue) Requirement(jetbrains.buildServer.requirements.Requirement) ApiUrlBuilder(jetbrains.buildServer.server.rest.ApiUrlBuilder) Permission(jetbrains.buildServer.serverSide.auth.Permission) SUser(jetbrains.buildServer.users.SUser) BuildTypeOrTemplate(jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with NotFoundException

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

the class BuildTypeRequest method getVcsRootEntryCheckoutRules.

@GET
@Path("/{btLocator}/vcs-root-entries/{vcsRootLocator}/" + VcsRootEntry.CHECKOUT_RULES)
@Produces({ "text/plain" })
@ApiOperation(value = "Get checkout rules of a VCS root of the matching build configuration.", nickname = "getVcsRootCheckoutRules")
public String getVcsRootEntryCheckoutRules(@ApiParam(format = LocatorName.BUILD_TYPE) @PathParam("btLocator") String buildTypeLocator, @ApiParam(format = LocatorName.VCS_ROOT) @PathParam("vcsRootLocator") String vcsRootLocator) {
    final BuildTypeOrTemplate buildType = myBuildTypeFinder.getBuildTypeOrTemplate(null, buildTypeLocator, true);
    final SVcsRoot vcsRoot = myVcsRootFinder.getItem(vcsRootLocator);
    if (!buildType.get().containsVcsRoot(vcsRoot.getId())) {
        throw new NotFoundException("VCS root with id '" + vcsRoot.getExternalId() + "' is not attached to the build type.");
    }
    return buildType.get().getCheckoutRules(vcsRoot).getAsString();
}
Also used : BuildTypeOrTemplate(jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate) SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) ApiOperation(io.swagger.annotations.ApiOperation)

Example 13 with NotFoundException

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

the class Build method getBuildToTrigger.

private BuildPromotion getBuildToTrigger(@Nullable final SUser user, @NotNull final ServiceLocator serviceLocator, @NotNull final Map<Long, Long> buildPromotionIdReplacements) {
    SVcsModification changeToUse = null;
    SVcsModification personalChangeToUse = null;
    if (submittedLastChanges != null) {
        List<SVcsModification> lastChanges = submittedLastChanges.getChangesFromPosted(serviceLocator.getSingletonService(ChangeFinder.class));
        if (lastChanges.size() > 0) {
            boolean changeProcessed = false;
            boolean personalChangeProcessed = false;
            for (SVcsModification change : lastChanges) {
                if (!change.isPersonal()) {
                    if (!changeProcessed) {
                        changeToUse = change;
                        changeProcessed = true;
                    } else {
                        throw new BadRequestException("Several non-personal changes are submitted, only one can be present");
                    }
                } else {
                    if (!personalChangeProcessed) {
                        personalChangeToUse = change;
                        personalChangeProcessed = true;
                    } else {
                        throw new BadRequestException("Several personal changes are submitted, only one can be present");
                    }
                }
            }
        }
    }
    final SBuildType submittedBuildType = getSubmittedBuildType(serviceLocator, personalChangeToUse, user);
    final BuildCustomizer customizer = serviceLocator.getSingletonService(BuildCustomizerFactory.class).createBuildCustomizer(submittedBuildType, user);
    if (changeToUse != null) {
        // might need to rework after comparison to code in jetbrains.buildServer.controllers.RunBuildBean.setupBuildCustomizer: customizer.setNodeRevisions, etc.
        customizer.setChangesUpTo(changeToUse);
    }
    if (submittedComment != null) {
        if (submittedComment.text != null) {
            customizer.setBuildComment(submittedComment.text);
        } else {
            throw new BadRequestException("Submitted comment does not have 'text' set.");
        }
    }
    if (submittedProperties != null) {
        customizer.setParameters(submittedProperties.getMap());
    }
    // this should ideally be used only when defaultBranc flag is not set. If set to false, should use customizer.setDesiredBranchName(submittedBranchName, false)
    if (submittedBranchName != null)
        customizer.setDesiredBranchName(submittedBranchName);
    if (submittedPersonal != null)
        customizer.setPersonal(submittedPersonal);
    if (submittedBuildDependencies != null) {
        try {
            customizer.setSnapshotDependencyNodes(submittedBuildDependencies.getFromPosted(serviceLocator, buildPromotionIdReplacements));
        } catch (IllegalArgumentException e) {
            throw new BadRequestException("Error trying to use specified snapshot dependencies" + getRelatedBuildDescription() + ":" + e.getMessage());
        } catch (NotFoundException e) {
            throw new BadRequestException("Error searching for snapshot dependency" + getRelatedBuildDescription() + ": " + e.getMessage(), e);
        }
    }
    if (submittedTriggeringOptions != null) {
        if (submittedTriggeringOptions.cleanSources != null) {
            customizer.setCleanSources(submittedTriggeringOptions.cleanSources);
        }
        if (submittedTriggeringOptions.cleanSourcesInAllDependencies != null) {
            ((BuildCustomizerEx) customizer).setApplyCleanSourcesToDependencies(submittedTriggeringOptions.cleanSourcesInAllDependencies);
        }
        if (submittedTriggeringOptions.freezeSettings != null) {
            ((BuildCustomizerEx) customizer).setFreezeSettings(submittedTriggeringOptions.freezeSettings);
        }
        if (submittedTriggeringOptions.tagDependencies != null) {
            ((BuildCustomizerEx) customizer).setApplyTagsToDependencies(submittedTriggeringOptions.tagDependencies);
        }
        if (submittedTriggeringOptions.rebuildAllDependencies != null) {
            customizer.setRebuildDependencies(submittedTriggeringOptions.rebuildAllDependencies);
        }
        if (submittedTriggeringOptions.rebuildFailedOrIncompleteDependencies != null && submittedTriggeringOptions.rebuildFailedOrIncompleteDependencies) {
            ((BuildCustomizerEx) customizer).setRebuildDependencies(BuildCustomizerEx.RebuildDependenciesMode.FAILED_OR_INCOMPLETE);
        }
        if (submittedTriggeringOptions.rebuildDependencies != null) {
            customizer.setRebuildDependencies(CollectionsUtil.convertCollection(submittedTriggeringOptions.rebuildDependencies.getFromPosted(serviceLocator.getSingletonService(BuildTypeFinder.class)), new Converter<String, BuildTypeOrTemplate>() {

                public String createFrom(@NotNull final BuildTypeOrTemplate source) {
                    if (source.getBuildType() == null) {
                        // noinspection ConstantConditions
                        throw new BadRequestException("Template is specified instead of a build type. Template id: '" + source.getTemplate().getExternalId() + "'");
                    }
                    return source.getBuildType().getInternalId();
                }
            }));
        }
    }
    List<BuildPromotion> artifactDepsBuildsPosted = null;
    try {
        artifactDepsBuildsPosted = submittedBuildArtifactDependencies == null ? null : submittedBuildArtifactDependencies.getFromPosted(serviceLocator, buildPromotionIdReplacements);
    } catch (NotFoundException e) {
        throw new BadRequestException("Error searching for artifact dependency" + getRelatedBuildDescription() + ": " + e.getMessage(), e);
    }
    if (submittedCustomBuildArtifactDependencies != null) {
        // todo: investigate if OK: here new dependencies are created and set into the build. Custom run build dialog onthe contrary, sets artifact deps with the same IDs into the build
        final List<SArtifactDependency> customDeps = submittedCustomBuildArtifactDependencies.getFromPosted(submittedBuildType.getArtifactDependencies(), serviceLocator);
        if (artifactDepsBuildsPosted == null) {
            setDepsWithNullCheck(customizer, customDeps);
        } else {
            // patch with "artifact-dependencies"
            setDepsWithNullCheck(customizer, getBuildPatchedDeps(customDeps, true, serviceLocator, artifactDepsBuildsPosted));
        }
    } else {
        if (artifactDepsBuildsPosted != null) {
            // use "artifact-dependencies" as the only dependencies as this allows to repeat a build
            setDepsWithNullCheck(customizer, getBuildPatchedDeps(submittedBuildType.getArtifactDependencies(), false, serviceLocator, artifactDepsBuildsPosted));
        } else {
        // no artifact dependencies customizations necessary
        }
    }
    if (submittedTags != null) {
        customizer.setTagDatas(new HashSet<TagData>(submittedTags.getFromPosted(serviceLocator.getSingletonService(UserFinder.class))));
    }
    if (submittedAttributes != null) {
        customizer.setAttributes(submittedAttributes.getMap());
    }
    final BuildPromotion result;
    try {
        result = customizer.createPromotion();
    } catch (IllegalStateException e) {
        // IllegalStateException is thrown e.g. when we try to create a personal build in a build type which does not allow this
        throw new BadRequestException("Cannot trigger build: " + e.getMessage());
    } catch (RevisionsNotFoundException e) {
        throw new BadRequestException("Cannot trigger build, if the changes are specified, they should be visible on the build configuration Change Log under the requested branch. Original error: " + e.getMessage());
    }
    BuildTypeEx modifiedBuildType = getCustomizedSubmittedBuildType(serviceLocator);
    if (modifiedBuildType != null) {
        // it's core's responsibility to check permissions here
        try {
            ((BuildPromotionEx) result).freezeSettings(modifiedBuildType, "rest");
        } catch (IOException e) {
            // include nested erorr or it can expose too much data?
            throw new OperationException("Error while freezing promotion settings", e);
        }
    }
    return result;
}
Also used : RevisionsNotFoundException(jetbrains.buildServer.vcs.impl.RevisionsNotFoundException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) NotNull(org.jetbrains.annotations.NotNull) SVcsModification(jetbrains.buildServer.vcs.SVcsModification) RevisionsNotFoundException(jetbrains.buildServer.vcs.impl.RevisionsNotFoundException) Converter(jetbrains.buildServer.util.Converter) OperationException(jetbrains.buildServer.server.rest.errors.OperationException) BuildTypeOrTemplate(jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate) IOException(java.io.IOException) SArtifactDependency(jetbrains.buildServer.serverSide.artifacts.SArtifactDependency) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException)

Example 14 with NotFoundException

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

the class Build method getBuildStatisticValue.

@NotNull
public static String getBuildStatisticValue(@NotNull final SBuild build, @NotNull final String statisticValueName) {
    Map<String, String> stats = getBuildStatisticsValues(build);
    String val = stats.get(statisticValueName);
    if (val != null) {
        return val;
    }
    // TeamCity API issue: this can actually provide a value which is not returned in the list
    BigDecimal directValue = build.getStatisticValue(statisticValueName);
    if (directValue != null) {
        return directValue.stripTrailingZeros().toPlainString();
    }
    throw new NotFoundException("No statistics data for key: " + statisticValueName + "' in build " + LogUtil.describe(build));
}
Also used : RevisionsNotFoundException(jetbrains.buildServer.vcs.impl.RevisionsNotFoundException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) BigDecimal(java.math.BigDecimal) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with NotFoundException

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

the class BuildTypeUtil method getParameter.

public static String getParameter(@Nullable final String parameterName, @NotNull final EntityWithParameters parametrizedEntity, final boolean checkSecure, final boolean nameItProperty, @NotNull final ServiceLocator serviceLocator) {
    if (StringUtil.isEmpty(parameterName)) {
        throw new BadRequestException(nameItProperty ? "Property" : "Parameter" + " name cannot be empty.");
    }
    Parameter parameter = parametrizedEntity.getParameter(parameterName);
    if (parameter == null) {
        throw new NotFoundException((nameItProperty ? "No property" : "No parameter") + " with name '" + parameterName + "' is found.");
    }
    if (!checkSecure)
        return parameter.getValue();
    String parameterValue = Property.getParameterValue(parameter, serviceLocator);
    if (parameterValue == null)
        throw new BadRequestException("Secure " + (nameItProperty ? "properties" : "parameters") + " cannot be retrieved via remote API by default.");
    return parameterValue;
}
Also used : BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException)

Aggregations

NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)47 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)26 ApiOperation (io.swagger.annotations.ApiOperation)19 NotNull (org.jetbrains.annotations.NotNull)14 BuildTypeOrTemplate (jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate)13 ServiceLocator (jetbrains.buildServer.ServiceLocator)10 SUser (jetbrains.buildServer.users.SUser)10 SVcsRoot (jetbrains.buildServer.vcs.SVcsRoot)10 Nullable (org.jetbrains.annotations.Nullable)8 LocatorProcessException (jetbrains.buildServer.server.rest.errors.LocatorProcessException)7 AuthorizationFailedException (jetbrains.buildServer.server.rest.errors.AuthorizationFailedException)5 Fields (jetbrains.buildServer.server.rest.model.Fields)5 Converter (jetbrains.buildServer.util.Converter)4 RevisionsNotFoundException (jetbrains.buildServer.vcs.impl.RevisionsNotFoundException)4 Logger (com.intellij.openapi.diagnostic.Logger)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 SUserGroup (jetbrains.buildServer.groups.SUserGroup)3 SArtifactDependency (jetbrains.buildServer.serverSide.artifacts.SArtifactDependency)3 Permission (jetbrains.buildServer.serverSide.auth.Permission)3