private static void ScheduleAutoDeployToTest(AspNetDeployEntities entities)
        {
            List <BundleVersion> bundleVersions = entities.BundleVersion
                                                  .Include("Properties")
                                                  .Include("Packages.Publications.Environment")
                                                  .Include("ProjectVersions.SourceControlVersion.Properties")
                                                  .Where(bv => !bv.IsDeleted)
                                                  .ToList();

            List <BundleVersion> bundleVersionsWithAutoDeploy = bundleVersions
                                                                .Where(bv =>
                                                                       bv.GetIntProperty("AutoDeployToEnvironment") > 0 &&
                                                                       bv.Packages.Any() &&
                                                                       bv.ProjectVersions.All(pv => pv.SourceControlVersion.ArchiveState == SourceControlVersionArchiveState.Normal))
                                                                .ToList();

            bundleVersionsWithAutoDeploy.ForEach(bundleVersion =>
            {
                int deployToEnvironmentId = bundleVersion.GetIntProperty("AutoDeployToEnvironment");

                if (bundleVersion.Packages.Any(p => p.Publications.Any(pub => pub.EnvironmentId == deployToEnvironmentId && pub.State == PublicationState.Queued)))
                {
                    return;
                }

                if (TaskRunnerContext.GetBundleVersionState(bundleVersion.Id) != BundleState.Idle)
                {
                    return;
                }

                Package latestPackage = bundleVersion.Packages.OrderByDescending(p => p.CreatedDate).First();
                List <Publication> latestPackagePublications = latestPackage.Publications.OrderByDescending(p => p.CreatedDate).ToList();

                if (latestPackagePublications.Count == 0)
                {
                    Publication publication   = new Publication();
                    publication.CreatedDate   = DateTime.UtcNow;
                    publication.EnvironmentId = deployToEnvironmentId;
                    publication.Package       = latestPackage;
                    publication.State         = PublicationState.Queued;

                    entities.Publication.Add(publication);
                }
            });

            entities.SaveChanges();
        }
 public BundleState GetBundleState(int bundleId)
 {
     return(TaskRunnerContext.GetBundleVersionState(bundleId));
 }