HasUnresolvedSteps() public method

public HasUnresolvedSteps ( ) : bool
return bool
Esempio n. 1
0
        protected override void Execute()
        {
            if (string.IsNullOrWhiteSpace(ProjectName))
            {
                throw new CommandException("Please specify a project name using the parameter: --project=XYZ");
            }

            Log.Debug("Finding project: " + ProjectName);
            var project = Repository.Projects.FindByName(ProjectName);

            if (project == null)
            {
                throw new CouldNotFindException("a project named", ProjectName);
            }

            var channel = default(ChannelResource);

            if (!string.IsNullOrWhiteSpace(ChannelName))
            {
                Log.Debug("Finding channel: " + ChannelName);
                var channels = Repository.Projects.GetChannels(project).Items;
                channel = channels.SingleOrDefault(c => string.Equals(c.Name, ChannelName, StringComparison.OrdinalIgnoreCase));
                if (channel == null)
                {
                    throw new CouldNotFindException("a channel named", ChannelName);
                }
            }

            Log.Debug("Finding deployment process for project: " + ProjectName);
            var deploymentProcess = Repository.DeploymentProcesses.Get(project.DeploymentProcessId);

            Log.Debug("Finding release template...");
            var releaseTemplate = Repository.DeploymentProcesses.GetTemplate(deploymentProcess, channel);

            var plan = new ReleasePlan(releaseTemplate, versionResolver);

            if (plan.UnresolvedSteps.Count > 0)
            {
                Log.Debug("Resolving NuGet package versions...");
                foreach (var unresolved in plan.UnresolvedSteps)
                {
                    if (!unresolved.IsResolveable)
                    {
                        Log.ErrorFormat("The version number for step '{0}' cannot be automatically resolved because the feed or package ID is dynamic.", unresolved.StepName);
                        continue;
                    }

                    if (!string.IsNullOrEmpty(VersionPrerelease))
                    {
                        Log.DebugFormat("Finding latest NuGet package with pre-release '{1}' for step: {0}", unresolved.StepName, VersionPrerelease);
                    }
                    else
                    {
                        Log.DebugFormat("Finding latest NuGet package for step: {0}", unresolved.StepName);
                    }

                    var feed = Repository.Feeds.Get(unresolved.NuGetFeedId);
                    if (feed == null)
                    {
                        throw new CommandException(string.Format("Could not find a feed with ID {0}, which is used by step: " + unresolved.StepName, unresolved.NuGetFeedId));
                    }

                    var filters = GetChannelVersionFilters(unresolved.StepName, channel);
                    filters["packageId"] = unresolved.PackageId;
                    if (!string.IsNullOrWhiteSpace(VersionPrerelease))
                    {
                        filters["preReleaseTag"] = VersionPrerelease;
                    }

                    var packages = Repository.Client.Get <List <PackageResource> >(feed.Link("SearchTemplate"), filters);
                    var version  = packages.FirstOrDefault();

                    if (version == null)
                    {
                        Log.ErrorFormat("Could not find any packages with ID '{0}' in the feed '{1}'", unresolved.PackageId, feed.FeedUri);
                    }
                    else
                    {
                        unresolved.SetVersionFromLatest(version.Version);
                    }
                }
            }

            string versionNumber;

            if (!string.IsNullOrWhiteSpace(VersionNumber))
            {
                Log.Debug("Using version number provided on command-line.");
                versionNumber = VersionNumber;
            }
            else if (!string.IsNullOrWhiteSpace(releaseTemplate.NextVersionIncrement))
            {
                Log.Debug("Using version number from release template.");
                versionNumber = releaseTemplate.NextVersionIncrement;
            }
            else if (!string.IsNullOrWhiteSpace(releaseTemplate.VersioningPackageStepName))
            {
                Log.Debug("Using version number from package step.");
                versionNumber = plan.GetActionVersionNumber(releaseTemplate.VersioningPackageStepName);
            }
            else
            {
                throw new CommandException("A version number was not specified and could not be automatically selected.");
            }

            if (plan.Steps.Count > 0)
            {
                Log.Info("Release plan for release:    " + versionNumber);
                Log.Info("Steps: ");
                Log.Info(plan.FormatAsTable());
            }

            if (plan.HasUnresolvedSteps())
            {
                throw new CommandException("Package versions could not be resolved for one or more of the package steps in this release. See the errors above for details. Either ensure the latest version of the package can be automatically resolved, or set the version to use specifically by using the --package argument.");
            }

            Log.Debug("Creating release...");

            if (IgnoreIfAlreadyExists)
            {
                try
                {
                    var found = Repository.Projects.GetReleaseByVersion(project, versionNumber);
                    if (found != null)
                    {
                        Log.Info("A release with the number " + versionNumber + " already exists.");
                        return;
                    }
                }
                catch (OctopusResourceNotFoundException)
                {
                    // Expected
                }
            }

            var release = Repository.Releases.Create(new ReleaseResource(versionNumber, project.Id, channel?.Id)
            {
                ReleaseNotes     = ReleaseNotes,
                SelectedPackages = plan.GetSelections()
            }, Force);

            Log.Info("Release " + release.Version + " created successfully!");
            Log.ServiceMessage("setParameter", new { name = "octo.releaseNumber", value = release.Version });
            Log.TfsServiceMessage(ServerBaseUrl, project, release);

            DeployRelease(project, release, DeployToEnvironmentNames);
        }
        protected override void Execute()
        {
            if (string.IsNullOrWhiteSpace(ProjectName)) throw new CommandException("Please specify a project name using the parameter: --project=XYZ");

            Log.Debug("Finding project: " + ProjectName);
            var project = Repository.Projects.FindByName(ProjectName);
            if (project == null)
                throw new CouldNotFindException("a project named", ProjectName);

            var channel = default(ChannelResource);
            if (!string.IsNullOrWhiteSpace(ChannelName))
            {
                Log.Debug("Finding channel: " + ChannelName);
                var channels = Repository.Projects.GetChannels(project).Items;
                channel = channels.SingleOrDefault(c => string.Equals(c.Name, ChannelName, StringComparison.OrdinalIgnoreCase));
                if (channel == null)
                    throw new CouldNotFindException("a channel named", ChannelName);
            }

            Log.Debug("Finding deployment process for project: " + ProjectName);
            var deploymentProcess = Repository.DeploymentProcesses.Get(project.DeploymentProcessId);

            Log.Debug("Finding release template...");
            var releaseTemplate = Repository.DeploymentProcesses.GetTemplate(deploymentProcess, channel);

            var plan = new ReleasePlan(releaseTemplate, versionResolver);

            if (plan.UnresolvedSteps.Count > 0)
            {
                Log.Debug("Resolving package versions...");
                foreach (var unresolved in plan.UnresolvedSteps)
                {
                    if (!unresolved.IsResolveable)
                    {
                        Log.ErrorFormat("The version number for step '{0}' cannot be automatically resolved because the feed or package ID is dynamic.", unresolved.StepName);
                        continue;
                    }

                    if (!string.IsNullOrEmpty(VersionPrerelease))
                        Log.DebugFormat("Finding latest package with pre-release '{1}' for step: {0}", unresolved.StepName, VersionPrerelease);
                    else
                        Log.DebugFormat("Finding latest package for step: {0}", unresolved.StepName);

                    var feed = Repository.Feeds.Get(unresolved.NuGetFeedId);
                    if (feed == null)
                        throw new CommandException(string.Format("Could not find a feed with ID {0}, which is used by step: " + unresolved.StepName, unresolved.NuGetFeedId));

                    var filters = GetChannelVersionFilters(unresolved.StepName, channel);
                    filters["packageId"] = unresolved.PackageId;
                    if (!string.IsNullOrWhiteSpace(VersionPrerelease))
                        filters["preReleaseTag"] = VersionPrerelease;

                    var packages = Repository.Client.Get<List<PackageResource>>(feed.Link("SearchTemplate"), filters);
                    var version = packages.FirstOrDefault();

                    if (version == null)
                    {
                        Log.ErrorFormat("Could not find any packages with ID '{0}' in the feed '{1}'", unresolved.PackageId, feed.FeedUri);
                    }
                    else
                    {
                        Log.DebugFormat("Selected version for package with ID '{0}' determined to be '{1}'", unresolved.PackageId, version.Version);
                        unresolved.SetVersionFromLatest(version.Version);
                    }
                }
            }

            string versionNumber;
            if (!string.IsNullOrWhiteSpace(VersionNumber))
            {
                Log.Debug("Using version number provided on command-line.");
                versionNumber = VersionNumber;
            }
            else if (!string.IsNullOrWhiteSpace(releaseTemplate.NextVersionIncrement))
            {
                Log.Debug("Using version number from release template.");
                versionNumber = releaseTemplate.NextVersionIncrement;
            }
            else if (!string.IsNullOrWhiteSpace(releaseTemplate.VersioningPackageStepName))
            {
                Log.Debug("Using version number from package step.");
                versionNumber = plan.GetActionVersionNumber(releaseTemplate.VersioningPackageStepName);
            }
            else
            {
                throw new CommandException("A version number was not specified and could not be automatically selected.");
            }

            if (plan.Steps.Count > 0)
            {
                Log.Info("Release plan for release:    " + versionNumber);
                Log.Info("Steps: ");
                Log.Info(plan.FormatAsTable());
            }

            if (plan.HasUnresolvedSteps())
            {
                throw new CommandException("Package versions could not be resolved for one or more of the package steps in this release. See the errors above for details. Either ensure the latest version of the package can be automatically resolved, or set the version to use specifically by using the --package argument.");
            }

            Log.Debug("Creating release...");

            if (IgnoreIfAlreadyExists)
            {
                try
                {
                    var found = Repository.Projects.GetReleaseByVersion(project, versionNumber);
                    if (found != null)
                    {
                        Log.Info("A release with the number " + versionNumber + " already exists.");
                        return;
                    }
                }
                catch (OctopusResourceNotFoundException)
                {
                    // Expected
                }
            }

            var release = Repository.Releases.Create(new ReleaseResource(versionNumber, project.Id, channel?.Id)
            {
                ReleaseNotes = ReleaseNotes,
                SelectedPackages = plan.GetSelections()
            }, Force);
            Log.Info("Release " + release.Version + " created successfully!");
            Log.ServiceMessage("setParameter", new {name = "octo.releaseNumber", value = release.Version});
            Log.TfsServiceMessage(ServerBaseUrl, project, release);

            DeployRelease(project, release, DeployToEnvironmentNames);
        }