Search in sources :

Example 6 with SArtifactDependency

use of jetbrains.buildServer.serverSide.artifacts.SArtifactDependency 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 7 with SArtifactDependency

use of jetbrains.buildServer.serverSide.artifacts.SArtifactDependency in project teamcity-rest by JetBrains.

the class PropEntitiesArtifactDep method setToBuildType.

/**
 * @return true if buildTypeSettings is modified
 */
public boolean setToBuildType(@NotNull final BuildTypeSettingsEx buildTypeSettings, @NotNull final ServiceLocator serviceLocator) {
    PropEntitiesArtifactDep.Storage original = new PropEntitiesArtifactDep.Storage(buildTypeSettings);
    try {
        List<SArtifactDependency> deps = new ArrayList<>();
        Map<String, Boolean> enabledData = new HashMap<>();
        if (propEntities != null) {
            for (PropEntityArtifactDep entity : propEntities) {
                SArtifactDependency newDep = entity.addToInternalMain(buildTypeSettings, serviceLocator);
                deps.add(newDep);
                enabledData.put(newDep.getId(), entity.disabled == null || !entity.disabled);
            }
        }
        buildTypeSettings.setArtifactDependencies(deps);
        for (Map.Entry<String, Boolean> entry : enabledData.entrySet()) {
            buildTypeSettings.setEnabled(entry.getKey(), entry.getValue());
        }
        // cannot actually determine if modified or not
        return true;
    } catch (Exception e) {
        // restore previous state
        original.apply(buildTypeSettings);
        throw new BadRequestException("Error setting artifact dependencies", e);
    }
}
Also used : SArtifactDependency(jetbrains.buildServer.serverSide.artifacts.SArtifactDependency) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException)

Example 8 with SArtifactDependency

use of jetbrains.buildServer.serverSide.artifacts.SArtifactDependency in project teamcity-rest by JetBrains.

the class PropEntityArtifactDep method replaceIn.

@NotNull
@Override
public SArtifactDependency replaceIn(@NotNull final BuildTypeSettingsEx buildType, @NotNull final SArtifactDependency originalDep, @NotNull final ServiceLocator serviceLocator) {
    final SArtifactDependency newDependency = createDependency(null, serviceLocator);
    PropEntitiesArtifactDep.Storage original = new PropEntitiesArtifactDep.Storage(buildType);
    final List<SArtifactDependency> newDependencies = new ArrayList<>(original.deps.size());
    for (SArtifactDependency currentDependency : original.deps) {
        if (currentDependency.equals(originalDep)) {
            newDependencies.add(newDependency);
        } else {
            newDependencies.add(currentDependency);
        }
    }
    try {
        buildType.setArtifactDependencies(newDependencies);
        buildType.setEnabled(newDependency.getId(), disabled == null || !disabled);
    } catch (Exception e) {
        // restore
        original.apply(buildType);
        throw new BadRequestException("Error updating artifact dependencies: " + e.toString(), e);
    }
    return newDependency;
}
Also used : BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) SArtifactDependency(jetbrains.buildServer.serverSide.artifacts.SArtifactDependency) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) AccessDeniedException(jetbrains.buildServer.serverSide.auth.AccessDeniedException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with SArtifactDependency

use of jetbrains.buildServer.serverSide.artifacts.SArtifactDependency in project teamcity-rest by JetBrains.

the class PropEntityArtifactDep method createDependency.

@NotNull
public SArtifactDependency createDependency(@Nullable String forcedId, @NotNull final ServiceLocator serviceLocator) {
    if (!ARTIFACT_DEPENDENCY_TYPE_NAME.equals(type)) {
        throw new BadRequestException("Artifact dependency should have type '" + ARTIFACT_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 = PropEntitySnapshotDep.getBuildTypeExternalIdForDependency(sourceBuildType, buildTypeIdFromProperty, serviceLocator);
    BuildTypeUtil.checkCanUseBuildTypeAsDependency(buildTypeIdDependOn, serviceLocator);
    final String revisionName = propertiesMap.get(NAME_REVISION_NAME);
    if (StringUtil.isEmpty(revisionName)) {
        throw new BadRequestException("Missing or empty artifact dependency property '" + NAME_REVISION_NAME + "'. Should contain one of supported values.");
    }
    String sourcePaths = propertiesMap.get(NAME_PATH_RULES);
    if (sourcePaths == null) {
        throw new BadRequestException("Missing source path property '" + NAME_PATH_RULES + "'. Should be specified.");
    }
    ArtifactDependencyFactory factory = serviceLocator.getSingletonService(ArtifactDependencyFactory.class);
    final SArtifactDependency artifactDependency;
    if (forcedId == null) {
        artifactDependency = factory.createArtifactDependency(buildTypeIdDependOn, sourcePaths, getRevisionRule(revisionName, propertiesMap.get(NAME_REVISION_VALUE), propertiesMap.get(NAME_REVISION_BRANCH)));
    } else {
        artifactDependency = factory.createArtifactDependency(forcedId, buildTypeIdDependOn, sourcePaths, getRevisionRule(revisionName, propertiesMap.get(NAME_REVISION_VALUE), propertiesMap.get(NAME_REVISION_BRANCH)));
    }
    final String cleanDir = propertiesMap.get(NAME_CLEAN_DESTINATION_DIRECTORY);
    if (cleanDir != null) {
        artifactDependency.setCleanDestinationFolder(Boolean.parseBoolean(cleanDir));
    }
    // noinspection SimplifiableConditionalExpression
    return artifactDependency;
}
Also used : BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) SArtifactDependency(jetbrains.buildServer.serverSide.artifacts.SArtifactDependency) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with SArtifactDependency

use of jetbrains.buildServer.serverSide.artifacts.SArtifactDependency in project teamcity-rest by JetBrains.

the class PropEntityArtifactDep method addTo.

@NotNull
@Override
public SArtifactDependency addTo(@NotNull final BuildTypeSettingsEx buildType, @NotNull final ServiceLocator serviceLocator) {
    PropEntitiesArtifactDep.Storage original = new PropEntitiesArtifactDep.Storage(buildType);
    try {
        final List<SArtifactDependency> dependencies = new ArrayList<SArtifactDependency>(buildType.getArtifactDependencies());
        SArtifactDependency result = addToInternal(buildType, serviceLocator);
        dependencies.add(result);
        // todo: use  buildType.addArtifactDependency, see TW-45262
        buildType.setArtifactDependencies(dependencies);
        return result;
    } catch (Exception e) {
        original.apply(buildType);
        throw new BadRequestException("Error adding artifact dependency: " + e.toString(), e);
    }
}
Also used : BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) SArtifactDependency(jetbrains.buildServer.serverSide.artifacts.SArtifactDependency) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) AccessDeniedException(jetbrains.buildServer.serverSide.auth.AccessDeniedException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

SArtifactDependency (jetbrains.buildServer.serverSide.artifacts.SArtifactDependency)19 BuildTypeOrTemplate (jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate)8 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)7 BaseFinderTest (jetbrains.buildServer.server.rest.data.BaseFinderTest)6 Test (org.testng.annotations.Test)6 ApiOperation (io.swagger.annotations.ApiOperation)5 NotNull (org.jetbrains.annotations.NotNull)5 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)4 Build (jetbrains.buildServer.server.rest.model.build.Build)3 BeanContext (jetbrains.buildServer.server.rest.util.BeanContext)3 SUser (jetbrains.buildServer.users.SUser)3 ArrayList (java.util.ArrayList)2 BuildTriggerDescriptor (jetbrains.buildServer.buildTriggers.BuildTriggerDescriptor)2 Builds (jetbrains.buildServer.server.rest.model.build.Builds)2 AccessDeniedException (jetbrains.buildServer.serverSide.auth.AccessDeniedException)2 BuildTypeImpl (jetbrains.buildServer.serverSide.impl.BuildTypeImpl)2 MockVcsSupport (jetbrains.buildServer.serverSide.impl.MockVcsSupport)2 ProjectEx (jetbrains.buildServer.serverSide.impl.ProjectEx)2 CheckoutRules (jetbrains.buildServer.vcs.CheckoutRules)2 SVcsRoot (jetbrains.buildServer.vcs.SVcsRoot)2