Search in sources :

Example 1 with NotFoundException

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

the class BuildFinder method getBuildPromotion.

/**
 * Supported build locators:
 *  213 - build with id=213
 *  213 when buildType is specified - build in the specified buildType with build number 213
 *  id:213 - build with id=213
 *  buildType:bt37 - specify Build Configuration by internal id. If specified, other locator parts should select the build
 *  number:213 when buildType is specified - build in the specified buildType with build number 213
 *  status:SUCCESS when buildType is specified - last build with the specified status in the specified buildType
 */
@NotNull
public BuildPromotion getBuildPromotion(@Nullable SBuildType buildType, @Nullable final String buildLocator) {
    if (!TeamCityProperties.getBoolean(LEGACY_BUILDS_FILTERING_FORCED)) {
        return getBuildPromotionInternal(buildType, buildLocator);
    }
    if (StringUtil.isEmpty(buildLocator)) {
        throw new BadRequestException("Empty single build locator is not supported.");
    }
    Locator locator = null;
    try {
        locator = new Locator(buildLocator);
    } catch (LocatorProcessException e) {
    // unparsable locator - proceed to report a due error later with all the supported locators
    }
    if (locator == null || useByPromotionFiltering(locator)) {
        return getBuildPromotionInternal(buildType, buildLocator);
    }
    if (locator.isSingleValue()) {
        if (buildType == null) {
            // no dimensions found and no build type, assume it's build id
            @SuppressWarnings("ConstantConditions") SBuild build = // todo: report non-number more user-friendly
            myServiceLocator.getSingletonService(BuildsManager.class).findBuildInstanceById(locator.getSingleValueAsLong());
            if (build == null) {
                throw new NotFoundException("Cannot find build by id '" + locator.getSingleValue() + "'.");
            }
            return build.getBuildPromotion();
        }
        // no dimensions found and build type is specified, assume it's build number
        @SuppressWarnings("ConstantConditions") SBuild build = myServiceLocator.getSingletonService(BuildsManager.class).findBuildInstanceByBuildNumber(buildType.getBuildTypeId(), buildLocator);
        if (build == null) {
            throw new NotFoundException("No build can be found by number '" + buildLocator + "' in build configuration " + buildType + ".");
        }
        return build.getBuildPromotion();
    }
    String buildTypeLocator = locator.getSingleDimensionValue("buildType");
    buildType = myBuildTypeFinder.deriveBuildTypeFromLocator(buildType, buildTypeLocator);
    Long id = locator.getSingleDimensionValueAsLong(DIMENSION_ID);
    if (id != null) {
        SBuild build = myServiceLocator.getSingletonService(BuildsManager.class).findBuildInstanceById(id);
        if (build == null) {
            throw new NotFoundException("No build can be found by id '" + id + "'.");
        }
        if (buildType != null && !buildType.getBuildTypeId().equals(build.getBuildTypeId())) {
            throw new NotFoundException("No build can be found by id '" + locator.getSingleDimensionValue(DIMENSION_ID) + "' in build type '" + buildType + "'.");
        }
        if (locator.getDimensionsCount() > 1) {
            LOG.info("Build locator '" + buildLocator + "' has '" + DIMENSION_ID + "' dimension and others. Others are ignored.");
        }
        return build.getBuildPromotion();
    }
    String number = locator.getSingleDimensionValue("number");
    if (number != null && buildType != null) {
        SBuild build = myServiceLocator.getSingletonService(BuildsManager.class).findBuildInstanceByBuildNumber(buildType.getBuildTypeId(), number);
        if (build == null) {
            throw new NotFoundException("No build can be found by number '" + number + "' in build configuration " + buildType + ".");
        }
        if (locator.getDimensionsCount() > 1) {
            LOG.info("Build locator '" + buildLocator + "' has 'number' dimension and others. Others are ignored.");
        }
        return build.getBuildPromotion();
    }
    Long promotionId = locator.getSingleDimensionValueAsLong(PROMOTION_ID);
    if (promotionId == null) {
        // support TeamCity 8.0 dimension
        promotionId = locator.getSingleDimensionValueAsLong("promotionId");
    }
    if (promotionId != null) {
        BuildPromotion build = getBuildByPromotionId(promotionId);
        if (buildType != null && !buildType.getBuildTypeId().equals(build.getBuildTypeId())) {
            throw new NotFoundException("No build can be found by " + PROMOTION_ID + " '" + promotionId + "' in build type '" + buildType + "'.");
        }
        if (locator.getDimensionsCount() > 1) {
            LOG.info("Build locator '" + buildLocator + "' has '" + PROMOTION_ID + "' dimension and others. Others are ignored.");
        }
        return build;
    }
    final BuildsFilter buildsFilter = getBuildsFilter(locator, buildType);
    buildsFilter.setCount(1);
    locator.checkLocatorFullyProcessed();
    final List<SBuild> filteredBuilds = getBuilds(buildsFilter);
    if (filteredBuilds.size() == 0) {
        throw new NotFoundException("No build found by filter: " + buildsFilter.toString() + ".");
    }
    if (filteredBuilds.size() == 1) {
        return filteredBuilds.get(0).getBuildPromotion();
    }
    throw new BadRequestException("Build locator '" + buildLocator + "' is not supported (" + filteredBuilds.size() + " builds found)");
}
Also used : ServiceLocator(jetbrains.buildServer.ServiceLocator) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) LocatorProcessException(jetbrains.buildServer.server.rest.errors.LocatorProcessException) GenericBuildsFilter(jetbrains.buildServer.server.rest.data.build.GenericBuildsFilter) BuildsFilter(jetbrains.buildServer.server.rest.data.build.BuildsFilter) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with NotFoundException

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

the class VcsRootInstance method setFieldValue.

public static void setFieldValue(final jetbrains.buildServer.vcs.VcsRootInstance rootInstance, final String field, final String newValue, @NotNull final BeanContext beanContext) {
    if (LAST_VERSION_INTERNAL.equals(field) || (LAST_VERSION.equals(field) && StringUtil.isEmpty(newValue))) {
        if (!StringUtil.isEmpty(newValue)) {
            beanContext.getSingletonService(RepositoryStateManager.class).setRepositoryState(rootInstance, new SingleVersionRepositoryStateAdapter(newValue));
            Loggers.VCS.info("Repository state is set to \"" + newValue + "\" via REST API call for " + rootInstance.describe(false) + " by " + beanContext.getSingletonService(PermissionChecker.class).getCurrentUserDescription());
        } else {
            beanContext.getSingletonService(RepositoryStateManager.class).setRepositoryState(rootInstance, new SingleVersionRepositoryStateAdapter((String) null));
            Loggers.VCS.info("Repository state is reset via REST API call for " + rootInstance.describe(false) + " by " + beanContext.getSingletonService(PermissionChecker.class).getCurrentUserDescription());
        }
        return;
    } else if (COMMIT_HOOK_MODE.equals(field)) {
        boolean pollingMode = !Locator.getStrictBooleanOrReportError(newValue);
        ((VcsRootInstanceEx) rootInstance).setPollingMode(pollingMode);
        Loggers.VCS.info("Poling mode is set to \"" + pollingMode + "\" via REST API call for " + rootInstance.describe(false) + " by " + beanContext.getSingletonService(PermissionChecker.class).getCurrentUserDescription());
        return;
    }
    throw new NotFoundException("Setting of field '" + field + "' is not supported. Supported are: " + LAST_VERSION_INTERNAL + ", " + COMMIT_HOOK_MODE);
}
Also used : RepositoryStateManager(jetbrains.buildServer.vcs.impl.RepositoryStateManager) SingleVersionRepositoryStateAdapter(jetbrains.buildServer.vcs.SingleVersionRepositoryStateAdapter) PermissionChecker(jetbrains.buildServer.server.rest.data.PermissionChecker) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException)

Example 3 with NotFoundException

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

the class ProjectRequest method getDefaultTemplate.

@GET
@Path("/{projectLocator}/defaultTemplate")
@Produces({ "application/xml", "application/json" })
@ApiOperation(value = "Get the default template of the matching project.", nickname = "getDefaultTemplate")
public BuildType getDefaultTemplate(@ApiParam(format = LocatorName.PROJECT) @PathParam("projectLocator") String projectLocator, @QueryParam("fields") String fields) {
    SProject project = myProjectFinder.getItem(projectLocator, true);
    BuildType result = Project.getDefaultTemplate(project, new Fields(fields), myBeanContext);
    if (result == null)
        throw new NotFoundException("No default template present");
    return result;
}
Also used : Fields(jetbrains.buildServer.server.rest.model.Fields) BuildType(jetbrains.buildServer.server.rest.model.buildType.BuildType) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) ApiOperation(io.swagger.annotations.ApiOperation)

Example 4 with NotFoundException

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

the class ProjectRequest method removeDefaultTemplate.

@DELETE
@Path("/{projectLocator}/defaultTemplate")
@ApiOperation(value = "Remove the default template from the matching project.", nickname = "removeDefaultTemplate")
public void removeDefaultTemplate(@ApiParam(format = LocatorName.PROJECT) @PathParam("projectLocator") String projectLocator, @QueryParam("fields") String fields) {
    ProjectEx project = (ProjectEx) myProjectFinder.getItem(projectLocator, true);
    if (project.getOwnDefaultTemplate() == null)
        throw new NotFoundException("No own default template present");
    project.setDefaultTemplate(null);
    project.schedulePersisting("Default template removed");
}
Also used : ProjectEx(jetbrains.buildServer.serverSide.impl.ProjectEx) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with NotFoundException

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

the class VcsRootInstanceRequest method scheduleCheckingForChanges.

@POST
@Path("/commitHookNotification")
@Produces({ "text/plain" })
@ApiOperation(value = "Send the commit hook notification.", nickname = "triggerCommitHookNotification")
public Response scheduleCheckingForChanges(@ApiParam(format = LocatorName.VCS_ROOT_INSTANCE) @QueryParam("locator") final String vcsRootInstancesLocator, @QueryParam("okOnNothingFound") final Boolean okOnNothingFound, @Context @NotNull final BeanContext beanContext) {
    if (StringUtil.isEmpty(vcsRootInstancesLocator)) {
        throw new BadRequestException("No 'locator' parameter provided, should be not empty VCS root instances locator");
    }
    Date requestStartTime = new Date();
    PagedSearchResult<jetbrains.buildServer.vcs.VcsRootInstance> vcsRootInstances = null;
    boolean nothingFound;
    try {
        vcsRootInstances = myVcsRootInstanceFinder.getItems(vcsRootInstancesLocator);
        nothingFound = vcsRootInstances.myEntries.isEmpty();
    } catch (NotFoundException e) {
        nothingFound = true;
    }
    if (nothingFound) {
        Response.ResponseBuilder responseBuilder;
        if (okOnNothingFound != null && okOnNothingFound) {
            responseBuilder = Response.status(Response.Status.OK);
        } else {
            responseBuilder = Response.status(Response.Status.NOT_FOUND);
        }
        return responseBuilder.entity("No VCS roots are found for locator '" + vcsRootInstancesLocator + "' with current user " + myBeanContext.getSingletonService(PermissionChecker.class).getCurrentUserDescription() + ". Check locator and permissions using '" + API_VCS_ROOT_INSTANCES_URL + "?locator=" + Locator.HELP_DIMENSION + "' URL.").build();
    }
    myDataProvider.getChangesCheckingService().forceCheckingFor(vcsRootInstances.myEntries, OperationRequestor.COMMIT_HOOK);
    StringBuilder okMessage = new StringBuilder();
    okMessage.append("Scheduled checking for changes for");
    if (vcsRootInstances.isNextPageAvailable()) {
        okMessage.append(" first ").append(vcsRootInstances.myActualCount).append(" VCS roots.");
        if (vcsRootInstances.myCount != null && vcsRootInstances.myActualCount >= vcsRootInstances.myCount) {
            okMessage.append(" You can add '" + PagerData.COUNT + ":X' to cover more roots.");
        }
        if (vcsRootInstances.myLookupLimit != null && vcsRootInstances.myLookupLimitReached) {
            okMessage.append(" You can add '" + FinderImpl.DIMENSION_LOOKUP_LIMIT + ":X' to cover more roots.");
        }
    } else {
        okMessage.append(" ").append(vcsRootInstances.myEntries.size()).append(" VCS roots.");
    }
    // format supported by TimeWithPrecision, can later be used in filtering
    okMessage.append(" (Server time: ").append(ISODateTimeFormat.basicDateTime().print(requestStartTime.getTime())).append(")");
    // can also add "in XX projects"
    return Response.status(Response.Status.ACCEPTED).entity(okMessage.toString()).build();
}
Also used : Response(javax.ws.rs.core.Response) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) Date(java.util.Date) VcsRootInstance(jetbrains.buildServer.server.rest.model.change.VcsRootInstance) ApiOperation(io.swagger.annotations.ApiOperation)

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