예제 #1
0
        private static void SetExpectations(BuildTemplate template, ITeamCityApi api, IExternalIssueResolver issueResolver, PackageBuildMappingCache packageCache)
        {
            var startBuild  = string.Format(template.BuildNumberPattern, template.StartBuildNumber);
            var finishBuild = string.Format(template.BuildNumberPattern, template.FinishBuildNumber);

            //BuildType/Builds/ChangeDetails
            A.CallTo(() => api.GetBuildTypeDetailsById(template.BuildId))
            .Returns(new BuildTypeDetails
            {
                Id          = template.BuildId,
                Name        = template.BuildName,
                Description = template.BuildName,
            });

            var changeDetails = SetupBuildTypeAndBuilds(api, template);

            A.CallTo(() => api.GetChangeDetailsByBuildTypeAndBuildNumber(template.BuildId, startBuild, finishBuild, A <IEnumerable <Build> > .Ignored))
            .Returns(changeDetails.Where(c => Convert.ToInt16(c.Id) > template.StartBuildNumber && Convert.ToInt16(c.Id) <= template.FinishBuildNumber).ToList());

            //Issues
            if (template.IssueCount > 0)
            {
                var issues = Enumerable.Range(1, template.IssueCount).Select(i => new Issue {
                    Id = RandomNumber.Next(2000).ToString()
                }).ToList();

                A.CallTo(() => api.GetIssuesByBuildTypeAndBuildRange(template.BuildId, startBuild, finishBuild, A <IEnumerable <Build> > .Ignored))
                .Returns(issues);

                A.CallTo(issueResolver).WithReturnType <IEnumerable <ExternalIssueDetails> >()
                .Returns(CreateExternalIssueDetails(issues, template));
            }

            //NuGetPackages
            SetNugetPackageDependencyExpectations(api, packageCache, template, issueResolver);
            SetNugetPackageDependencyExpectations(api, packageCache, template, issueResolver);
        }
        private static void SetExpectations(BuildTemplate template, ITeamCityApi api, IExternalIssueResolver issueResolver, PackageBuildMappingCache packageCache)
        {
            var startBuild = string.Format(template.BuildNumberPattern, template.StartBuildNumber);
            var finishBuild = string.Format(template.BuildNumberPattern, template.FinishBuildNumber);

            //BuildType/Builds/ChangeDetails
            A.CallTo(() => api.GetBuildTypeDetailsById(template.BuildId))
             .Returns(new BuildTypeDetails
                 {
                     Id = template.BuildId,
                     Name = template.BuildName,
                     Description = template.BuildName,
                 });

            var changeDetails = SetupBuildTypeAndBuilds(api, template);
            A.CallTo(() =>api.GetChangeDetailsByBuildTypeAndBuildNumber(template.BuildId, startBuild, finishBuild, A<IEnumerable<Build>>.Ignored))
             .Returns(changeDetails.Where(c => Convert.ToInt16(c.Id) > template.StartBuildNumber && Convert.ToInt16(c.Id) <= template.FinishBuildNumber).ToList());

            //Issues
            if (template.IssueCount > 0)
            {
                var issues = Enumerable.Range(1, template.IssueCount).Select(i => new Issue {Id = RandomNumber.Next(2000).ToString()}).ToList();

                A.CallTo(() => api.GetIssuesByBuildTypeAndBuildRange(template.BuildId, startBuild, finishBuild, A<IEnumerable<Build>>.Ignored))
                 .Returns(issues);

                A.CallTo(issueResolver).WithReturnType<IEnumerable<ExternalIssueDetails>>()
                 .Returns(CreateExternalIssueDetails(issues, template));
            }

            //NuGetPackages
            SetNugetPackageDependencyExpectations(api, packageCache, template, issueResolver);
            SetNugetPackageDependencyExpectations(api, packageCache, template, issueResolver);
        }
        private ChangeManifest CreateChangeManifest(string buildName, string buildType, string referenceBuild = null, string from = null, string to = null, string projectName = null, bool useBuildSystemIssueResolution = true, bool recurse = false)
        {
            var changeManifest = new ChangeManifest();

            if (recurse && _packageBuildMappingCache == null)
            {
                changeManifest.GenerationLog.Add(new LogEntry(DateTime.Now, Status.Warning, "Recurse option provided with no PackageBuildMappingCache, we will not be honoring the Recurse option."));
                changeManifest.GenerationStatus = Status.Warning;
            }

            buildType = buildType ?? ResolveBuildTypeId(projectName, buildName);

            if (String.IsNullOrEmpty(from))
            {
                changeManifest.GenerationLog.Add(new LogEntry(DateTime.Now, Status.Warning, "Resolving FROM version based on the provided BuildType (FROM was not provided)."));
                from = ResolveFromVersion(buildType);
            }

            if (String.IsNullOrEmpty(to))
            {
                changeManifest.GenerationLog.Add(new LogEntry(DateTime.Now, Status.Warning, "Resolving TO version based on the provided BuildType (TO was not provided)."));
                to = ResolveToVersion(buildType);
            }

            var buildWithCommitData       = referenceBuild ?? buildType;
            var buildTypeDetails          = _api.GetBuildTypeDetailsById(buildType);
            var referenceBuildTypeDetails = !String.IsNullOrEmpty(referenceBuild) ? _api.GetBuildTypeDetailsById(referenceBuild) : null;

            if (!String.IsNullOrEmpty(from) && !String.IsNullOrEmpty(to) && !String.IsNullOrEmpty(buildWithCommitData))
            {
                changeManifest.GenerationLog.Add(new LogEntry(DateTime.Now, Status.Ok, "Getting builds based on BuildType"));
                var builds = _api.GetBuildsByBuildType(buildWithCommitData);
                if (builds != null)
                {
                    var buildList = builds as List <Build> ?? builds.ToList();
                    changeManifest.GenerationLog.Add(new LogEntry(DateTime.Now, Status.Ok, string.Format("Got {0} builds for BuildType {1}.", buildList.Count(), buildType)));
                    var changeDetails       = _api.GetChangeDetailsByBuildTypeAndBuildNumber(buildWithCommitData, @from, to, buildList).ToList();
                    var issueDetailResolver = new IssueDetailResolver(_externalIssueResolvers);

                    //Rather than use TeamCity to resolve the issue to commit details (via TeamCity plugins) use the issue resolvers directly...
                    var issues = useBuildSystemIssueResolution
                                     ? _api.GetIssuesByBuildTypeAndBuildRange(buildWithCommitData, @from, to, buildList).ToList()
                                     : issueDetailResolver.GetAssociatedIssues(changeDetails).ToList();

                    changeManifest.GenerationLog.Add(new LogEntry(DateTime.Now, Status.Ok, string.Format("Got {0} issues for BuildType {1}.", issues.Count(), buildType)));

                    changeManifest.GenerationLog.Add(new LogEntry(DateTime.Now, Status.Ok, "Checking package dependencies."));
                    var buildFrom       = buildList.FirstOrDefault(b => b.Number == @from);
                    var buildTo         = buildList.FirstOrDefault(b => b.Number == to);
                    var initialPackages = new List <TeamCityApi.PackageDetails>();
                    var finalPackages   = new List <TeamCityApi.PackageDetails>();
                    if (buildFrom != null)
                    {
                        initialPackages = _api.GetNuGetDependenciesByBuildTypeAndBuildId(buildType, buildFrom.Id).ToList();
                    }
                    if (buildTo != null)
                    {
                        finalPackages = _api.GetNuGetDependenciesByBuildTypeAndBuildId(buildType, buildTo.Id).ToList();
                    }

                    var packageChanges = _packageChangeComparator.GetPackageChanges(initialPackages, finalPackages).ToList();

                    var issueDetails = issueDetailResolver.GetExternalIssueDetails(issues);

                    changeManifest.NuGetPackageChanges = packageChanges;
                    changeManifest.ChangeDetails.AddRange(changeDetails);
                    changeManifest.IssueDetails.AddRange(issueDetails);
                    changeManifest.Generated                   = DateTime.Now;
                    changeManifest.FromVersion                 = @from;
                    changeManifest.ToVersion                   = to;
                    changeManifest.BuildConfiguration          = buildTypeDetails;
                    changeManifest.ReferenceBuildConfiguration = referenceBuildTypeDetails ?? new BuildTypeDetails();
                }
                else
                {
                    changeManifest.GenerationLog.Add(new LogEntry(DateTime.Now, Status.Warning, string.Format("No builds returned for BuildType {0}.", buildType)));
                }
            }
            //Now we need to see if we need to recurse, and whether we have been given a cache file....
            if (changeManifest.NuGetPackageChanges.Any() && recurse && _packageBuildMappingCache != null)
            {
                foreach (var dependency in changeManifest.NuGetPackageChanges.Where(c => c.Type == NuGetPackageChangeType.Modified))
                {
                    var traversedDependency = _traversedPackageChanges.FirstOrDefault(p => p.NewVersion == dependency.NewVersion && p.OldVersion == dependency.OldVersion && p.PackageId == dependency.PackageId);
                    if (traversedDependency != null)
                    {
                        dependency.ChangeManifest = traversedDependency.ChangeManifest;
                        continue;
                    }
                    var mappings = _packageBuildMappingCache.PackageBuildMappings.Where(m => m.PackageId.Equals(dependency.PackageId, StringComparison.CurrentCultureIgnoreCase)).ToList();
                    PackageBuildMapping build = null;
                    if (mappings.Count == 1)
                    {
                        //We only got one back, this is good...
                        build = mappings.First();
                        changeManifest.GenerationLog.Add(new LogEntry(DateTime.Now, Status.Ok, string.Format("Found singular packages to build mapping {0}.", build.BuildConfigurationName)));
                    }
                    else if (mappings.Any())
                    {
                        //Ok, so multiple builds are outputting this package, so we need to try and constrain on project...
                        build = mappings.FirstOrDefault(m => m.Project.Equals(buildTypeDetails.Project.Name, StringComparison.OrdinalIgnoreCase));
                        if (build != null)
                        {
                            changeManifest.GenerationLog.Add(new LogEntry(DateTime.Now, Status.Warning, string.Format("Found duplicate mappings, using package to build mapping {0}.", build.BuildConfigurationName)));
                        }
                    }

                    if (build != null)
                    {
                        if (build.BuildConfigurationId == buildType)
                        {
                            continue;
                        }
                        var instanceTeamCityApi = _api.TeamCityServer.Equals(build.ServerUrl, StringComparison.OrdinalIgnoreCase)
                                                              ? _api
                                                              : new TeamCityApi(build.ServerUrl);

                        var resolver           = new AggregateBuildDeltaResolver(instanceTeamCityApi, _externalIssueResolvers, _packageChangeComparator, _packageBuildMappingCache, _traversedPackageChanges);
                        var dependencyManifest = resolver.CreateChangeManifest(null, build.BuildConfigurationId, null, dependency.OldVersion, dependency.NewVersion, null, true, true);
                        dependency.ChangeManifest = dependencyManifest;
                    }
                    else
                    {
                        changeManifest.GenerationLog.Add(new LogEntry(DateTime.Now, Status.Warning, string.Format("Did not find a mapping for package: {0}.", dependency.PackageId)));
                    }
                    _traversedPackageChanges.Add(dependency);
                }
            }

            return(changeManifest);
        }