Search in sources :

Example 11 with SVcsModification

use of jetbrains.buildServer.vcs.SVcsModification in project teamcity-rest by JetBrains.

the class ChangeRequest method getChangeProblemsTree.

/**
 *  Experimental, subject to change
 *  @since 2021.2
 */
@GET
@Path("/{changeLocator}/problemsTree")
@Produces({ "application/xml", "application/json" })
@ApiOperation(value = "Get problems tree for the matching change.", nickname = "getChangeProblemsTree", hidden = true)
public ProblemOccurrencesTree getChangeProblemsTree(@ApiParam(format = LocatorName.CHANGE) @PathParam("changeLocator") String changeLocator, // todo: remove after ui migration
@QueryParam(ProblemOccurrencesTreeCollector.SUB_TREE_ROOT_ID) String subTreeRootId, @QueryParam("treeLocator") String treeLocatorText, @QueryParam("fields") String fields) {
    final SVcsModification change = myChangeFinder.getItem(changeLocator).getSVcsModification();
    ChangeStatusProvider myStatusProvider = myServiceLocator.getSingletonService(ChangeStatusProvider.class);
    ChangeStatus changeStatus = myStatusProvider.getMergedChangeStatus(change);
    Stream<BuildPromotion> firstBuildsPromotions = changeStatus.getBuildTypesStatusMap().values().stream().filter(Objects::nonNull);
    Locator treeLocator = Locator.createPotentiallyEmptyLocator(treeLocatorText);
    if (subTreeRootId != null) {
        treeLocator.setDimension(ProblemOccurrencesTreeCollector.SUB_TREE_ROOT_ID, subTreeRootId);
    }
    return new ProblemOccurrencesTree(myProblemOccurrencesTreeCollector.getTreeFromBuildPromotions(firstBuildsPromotions, treeLocator), new Fields(fields), myBeanContext);
}
Also used : SVcsModification(jetbrains.buildServer.vcs.SVcsModification) ChangeStatus(jetbrains.buildServer.vcs.ChangeStatus) BuildPromotion(jetbrains.buildServer.serverSide.BuildPromotion) ServiceLocator(jetbrains.buildServer.ServiceLocator) Locator(jetbrains.buildServer.server.rest.data.Locator) Fields(jetbrains.buildServer.server.rest.model.Fields) Objects(java.util.Objects) ProblemOccurrencesTree(jetbrains.buildServer.server.rest.model.problem.scope.ProblemOccurrencesTree) ChangeStatusProvider(jetbrains.buildServer.vcs.ChangeStatusProvider) ApiOperation(io.swagger.annotations.ApiOperation)

Example 12 with SVcsModification

use of jetbrains.buildServer.vcs.SVcsModification 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 13 with SVcsModification

use of jetbrains.buildServer.vcs.SVcsModification in project teamcity-rest by JetBrains.

the class Build method getLastChanges.

/**
 * Lists last change(s) included into the build so that this can be used in the build start request. The changes correspond to the revisions used by the build.
 * The set of the changes included can vary in the future TeamCity versions. In TeamCity 8.1 this is the last usual change and also a personal change (for personal build only)
 *
 * @return
 */
@XmlElement(name = "lastChanges")
public Changes getLastChanges() {
    if (!myFields.isIncluded("lastChanges", false, true)) {
        return null;
    }
    return ValueWithDefault.decideDefault(myFields.isIncluded("lastChanges", false), () -> {
        final Changes result = Changes.fromSVcsModificationsSupplier(() -> {
            final List<SVcsModification> modifications = new ArrayList<SVcsModification>();
            final Long lastModificationId = myBuildPromotion.getLastModificationId();
            if (lastModificationId != null && lastModificationId != -1) {
                try {
                    SVcsModification modification = myBeanContext.getSingletonService(VcsModificationHistory.class).findChangeById(lastModificationId);
                    if (modification != null && modification.getRelatedConfigurations().contains(myBuildPromotion.getParentBuildType())) {
                        modifications.add(modification);
                    }
                } catch (AccessDeniedException e) {
                // ignore: the associated modification id probably does not belong to the build configuration (related to TW-35390)
                }
            }
            modifications.addAll(myBuildPromotion.getPersonalChanges());
            return modifications;
        }, null, myFields.getNestedField("lastChanges", Fields.NONE, Fields.LONG), myBeanContext);
        return result.isDefault() ? null : result;
    });
}
Also used : Changes(jetbrains.buildServer.server.rest.model.change.Changes) BuildChanges(jetbrains.buildServer.server.rest.model.change.BuildChanges) SVcsModification(jetbrains.buildServer.vcs.SVcsModification) AccessDeniedException(jetbrains.buildServer.serverSide.auth.AccessDeniedException) VcsModificationHistory(jetbrains.buildServer.vcs.VcsModificationHistory) XmlElement(javax.xml.bind.annotation.XmlElement)

Example 14 with SVcsModification

use of jetbrains.buildServer.vcs.SVcsModification in project teamcity-rest by JetBrains.

the class ChangeStatusTest method testFailedFinished.

public void testFailedFinished() {
    final BuildTypeImpl buildConf = registerBuildType("buildConf1", "project");
    VcsRootInstance root1 = prepareSingleVscRoot(buildConf);
    SVcsModification m20 = myFixture.addModification(modification().in(root1).version("20").parentVersions("10"));
    RunningBuildEx runningBuildEx = build().in(buildConf).onModifications(m20).run();
    myFixture.finishBuild(runningBuildEx, true);
    ChangeStatus status = new ChangeStatus(myFixture.getChangeStatusProvider().getMergedChangeStatus(m20), Fields.ALL_NESTED, getBeanContext(myFixture));
    assertEquals(1, (int) status.getFinished());
    assertEquals(0, (int) status.getSuccessful());
    assertEquals(1, (int) status.getFailed());
    assertEquals(0, (int) status.getRunning());
}
Also used : SVcsModification(jetbrains.buildServer.vcs.SVcsModification) RunningBuildEx(jetbrains.buildServer.serverSide.RunningBuildEx) ChangeStatus(jetbrains.buildServer.server.rest.model.change.ChangeStatus) BuildTypeImpl(jetbrains.buildServer.serverSide.impl.BuildTypeImpl) VcsRootInstance(jetbrains.buildServer.vcs.VcsRootInstance)

Example 15 with SVcsModification

use of jetbrains.buildServer.vcs.SVcsModification in project teamcity-rest by JetBrains.

the class ChangeStatusTest method testCancelled.

@Test(enabled = false, description = "Cancelled builds are not counted at all, as MergedChangeStatus does not contain them for some reason.")
public void testCancelled() {
    final BuildTypeImpl buildConf = registerBuildType("buildConf1", "project");
    VcsRootInstance root1 = prepareSingleVscRoot(buildConf);
    SVcsModification m20 = myFixture.addModification(modification().in(root1).version("20").parentVersions("10"));
    SUser user = createUser("testuser");
    RunningBuildEx runningBuildEx = build().in(buildConf).onModifications(m20).withFailedTests("failedTestName").run();
    runningBuildEx.stop(user, "Stopped manually");
    finishBuild(runningBuildEx, false);
    ChangeStatus status = new ChangeStatus(myFixture.getChangeStatusProvider().getMergedChangeStatus(m20), Fields.ALL_NESTED, getBeanContext(myFixture));
    assertEquals(0, (int) status.getFinished());
    assertEquals(0, (int) status.getSuccessful());
    assertEquals(0, (int) status.getFailed());
    assertEquals(1, (int) status.getCancelled());
    assertEquals(1, (int) status.getPendingBuildTypes());
}
Also used : SVcsModification(jetbrains.buildServer.vcs.SVcsModification) RunningBuildEx(jetbrains.buildServer.serverSide.RunningBuildEx) ChangeStatus(jetbrains.buildServer.server.rest.model.change.ChangeStatus) SUser(jetbrains.buildServer.users.SUser) BuildTypeImpl(jetbrains.buildServer.serverSide.impl.BuildTypeImpl) VcsRootInstance(jetbrains.buildServer.vcs.VcsRootInstance) BaseFinderTest(jetbrains.buildServer.server.rest.data.BaseFinderTest) Test(org.testng.annotations.Test)

Aggregations

SVcsModification (jetbrains.buildServer.vcs.SVcsModification)24 VcsRootInstance (jetbrains.buildServer.vcs.VcsRootInstance)14 ChangeStatus (jetbrains.buildServer.server.rest.model.change.ChangeStatus)12 BuildTypeImpl (jetbrains.buildServer.serverSide.impl.BuildTypeImpl)12 ApiOperation (io.swagger.annotations.ApiOperation)7 Fields (jetbrains.buildServer.server.rest.model.Fields)7 ChangeStatus (jetbrains.buildServer.vcs.ChangeStatus)7 ChangeStatusProvider (jetbrains.buildServer.vcs.ChangeStatusProvider)7 RunningBuildEx (jetbrains.buildServer.serverSide.RunningBuildEx)5 Test (org.testng.annotations.Test)5 BuildPromotion (jetbrains.buildServer.serverSide.BuildPromotion)4 ServiceLocator (jetbrains.buildServer.ServiceLocator)3 BaseFinderTest (jetbrains.buildServer.server.rest.data.BaseFinderTest)3 Locator (jetbrains.buildServer.server.rest.data.Locator)3 Objects (java.util.Objects)2 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)2 BuildTypes (jetbrains.buildServer.server.rest.model.buildType.BuildTypes)2 SVcsRootImpl (jetbrains.buildServer.vcs.impl.SVcsRootImpl)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1