示例#1
0
        /// <summary>
        /// Create TFS Team if not exists
        /// </summary>
        /// <returns></returns>
        public TeamFoundationTeam CreateTeam()
        {
            // Check team already exists
            var teams        = _tfsTeamService.QueryTeams(_projectInfo.Uri.ToString());
            var existingTeam = teams.FirstOrDefault(t => t.Name == _softwareProjectName);

            if (existingTeam != null)
            {
                Console.WriteLine($"Team '{existingTeam.Name}' already exists.");
                return(existingTeam);
            }

            //Create TFS Team
            TeamFoundationTeam team = _tfsTeamService.CreateTeam(
                _projectInfo.Uri.ToString(), _softwareProjectName, _softwareProjectDescription, null);

            //Set the IterationPaths and BacklogIterationPath for the TFS Team
            var teamConfiguration     = _teamSettingsConfigurationService.GetTeamConfigurations(new[] { team.Identity.TeamFoundationId });
            TeamConfiguration tconfig = teamConfiguration.FirstOrDefault();
            TeamSettings      ts      = tconfig.TeamSettings;

            ts.IterationPaths       = new string[] { $"{_projectInfo.Name}\\{_softwareProjectName}\\Sprint 1" };
            ts.BacklogIterationPath = $"{_projectInfo.Name}\\{_softwareProjectName}";
            TeamFieldValue tfv = new TeamFieldValue();

            tfv.IncludeChildren = true;
            tfv.Value           = ts.BacklogIterationPath;
            ts.TeamFieldValues  = new TeamFieldValue[] { tfv };
            _teamSettingsConfigurationService.SetTeamSettings(tconfig.TeamId, ts);

            Console.WriteLine($"Team '{team.Name}' created.");

            return(team);
        }
        internal override void InternalExecute()
        {
            if (_config == null)
            {
                throw new Exception("You must call Configure() first");
            }
            Stopwatch stopwatch = Stopwatch.StartNew();
            //////////////////////////////////////////////////
            WorkItemStoreContext sourceStore = new WorkItemStoreContext(me.Source, WorkItemStoreFlags.BypassRules, Telemetry);
            TfsTeamService sourceTS = me.Source.Collection.GetService<TfsTeamService>();
            List<TeamFoundationTeam> sourceTL = sourceTS.QueryTeams(me.Source.Config.Project).ToList();
            Trace.WriteLine(string.Format("Found {0} teams in Source?", sourceTL.Count));
            var sourceTSCS = me.Source.Collection.GetService<TeamSettingsConfigurationService>();
            //////////////////////////////////////////////////
            WorkItemStoreContext targetStore = new WorkItemStoreContext(me.Target, WorkItemStoreFlags.BypassRules, Telemetry);
            Project targetProject = targetStore.GetProject();
            Trace.WriteLine(string.Format("Found target project as {0}", targetProject.Name));
            TfsTeamService targetTS = me.Target.Collection.GetService<TfsTeamService>();
            List<TeamFoundationTeam> targetTL = targetTS.QueryTeams(me.Target.Config.Project).ToList();
            Trace.WriteLine(string.Format("Found {0} teams in Target?", targetTL.Count));
            var targetTSCS = me.Target.Collection.GetService<TeamSettingsConfigurationService>();
            //////////////////////////////////////////////////
            int current = sourceTL.Count;
            int count = 0;
            long elapsedms = 0;

            /// Create teams
            /// 
            foreach (TeamFoundationTeam sourceTeam in sourceTL)
            {
                Stopwatch witstopwatch = Stopwatch.StartNew();
                var foundTargetTeam = (from x in targetTL where x.Name == sourceTeam.Name select x).SingleOrDefault();
                if (foundTargetTeam == null)
                {
                    Trace.WriteLine(string.Format("Processing team '{0}':", sourceTeam.Name));
                    TeamFoundationTeam newTeam = targetTS.CreateTeam(targetProject.Uri.ToString(), sourceTeam.Name, sourceTeam.Description, null);
                    Trace.WriteLine(string.Format("-> Team '{0}' created", sourceTeam.Name));

                    if (_config.EnableTeamSettingsMigration)
                    {
                        /// Duplicate settings
                        Trace.WriteLine(string.Format("-> Processing team '{0}' settings:", sourceTeam.Name));
                        var sourceConfigurations = sourceTSCS.GetTeamConfigurations(new List<Guid> { sourceTeam.Identity.TeamFoundationId });
                        var targetConfigurations = targetTSCS.GetTeamConfigurations(new List<Guid> { newTeam.Identity.TeamFoundationId });

                        foreach (var sourceConfig in sourceConfigurations)
                        {
                            var targetConfig = targetConfigurations.FirstOrDefault(t => t.TeamName == sourceConfig.TeamName);
                            if (targetConfig == null)
                            {
                                Trace.WriteLine(string.Format("-> Settings for team '{0}'.. not found", sourceTeam.Name));
                                continue;
                            }

                            Trace.WriteLine(string.Format("-> Settings found for team '{0}'..", sourceTeam.Name));
                            if (_config.PrefixProjectToNodes)
                            {
                                targetConfig.TeamSettings.BacklogIterationPath = 
                                    string.Format("{0}\\{1}", me.Target.Config.Project, sourceConfig.TeamSettings.BacklogIterationPath);
                                targetConfig.TeamSettings.IterationPaths = sourceConfig.TeamSettings.IterationPaths
                                    .Select(path => string.Format("{0}\\{1}", me.Target.Config.Project, path))
                                    .ToArray();
                                targetConfig.TeamSettings.TeamFieldValues = sourceConfig.TeamSettings.TeamFieldValues
                                    .Select(field => new TeamFieldValue
                                    {
                                        IncludeChildren = field.IncludeChildren,
                                        Value = string.Format("{0}\\{1}", me.Target.Config.Project, field.Value)
                                    })
                                    .ToArray();
                            }
                            else
                            {
                                targetConfig.TeamSettings.BacklogIterationPath = sourceConfig.TeamSettings.BacklogIterationPath;
                                targetConfig.TeamSettings.IterationPaths = sourceConfig.TeamSettings.IterationPaths;
                                targetConfig.TeamSettings.TeamFieldValues = sourceConfig.TeamSettings.TeamFieldValues;
                            }

                            targetTSCS.SetTeamSettings(targetConfig.TeamId, targetConfig.TeamSettings);
                            Trace.WriteLine(string.Format("-> Team '{0}' settings... applied", targetConfig.TeamName));
                        }
                    }
                }
                else
                {
                    Trace.WriteLine(string.Format("Team '{0}' found.. skipping", sourceTeam.Name));
                }

                witstopwatch.Stop();
                elapsedms = elapsedms + witstopwatch.ElapsedMilliseconds;
                current--;
                count++;
                TimeSpan average = new TimeSpan(0, 0, 0, 0, (int)(elapsedms / count));
                TimeSpan remaining = new TimeSpan(0, 0, 0, 0, (int)(average.TotalMilliseconds * current));
                Trace.WriteLine("");
                //Trace.WriteLine(string.Format("Average time of {0} per work item and {1} estimated to completion", string.Format(@"{0:s\:fff} seconds", average), string.Format(@"{0:%h} hours {0:%m} minutes {0:s\:fff} seconds", remaining)));
            }
            // Set Team Settings
            //foreach (TeamFoundationTeam sourceTeam in sourceTL)
            //{
            //    Stopwatch witstopwatch = new Stopwatch();
            //    witstopwatch.Start();
            //    var foundTargetTeam = (from x in targetTL where x.Name == sourceTeam.Name select x).SingleOrDefault();
            //    if (foundTargetTeam == null)
            //    {
            //        Trace.WriteLine(string.Format("Processing team {0}", sourceTeam.Name));
            //        var sourceTCfU = sourceTSCS.GetTeamConfigurations((new[] { sourceTeam.Identity.TeamFoundationId })).SingleOrDefault();
            //        TeamSettings newTeamSettings = CreateTargetTeamSettings(sourceTCfU);
            //        TeamFoundationTeam newTeam = targetTS.CreateTeam(targetProject.Uri.ToString(), sourceTeam.Name, sourceTeam.Description, null);
            //        targetTSCS.SetTeamSettings(newTeam.Identity.TeamFoundationId, newTeamSettings);
            //    }
            //    else
            //    {
            //        Trace.WriteLine(string.Format("Team found.. skipping"));
            //    }

            //    witstopwatch.Stop();
            //    elapsedms = elapsedms + witstopwatch.ElapsedMilliseconds;
            //    current--;
            //    count++;
            //    TimeSpan average = new TimeSpan(0, 0, 0, 0, (int)(elapsedms / count));
            //    TimeSpan remaining = new TimeSpan(0, 0, 0, 0, (int)(average.TotalMilliseconds * current));
            //    Trace.WriteLine("");
            //    //Trace.WriteLine(string.Format("Average time of {0} per work item and {1} estimated to completion", string.Format(@"{0:s\:fff} seconds", average), string.Format(@"{0:%h} hours {0:%m} minutes {0:s\:fff} seconds", remaining)));

            //}
            //////////////////////////////////////////////////
            stopwatch.Stop();
            Console.WriteLine(@"DONE in {0:%h} hours {0:%m} minutes {0:s\:fff} seconds", stopwatch.Elapsed);
        }
        internal override void InternalExecute()
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            //////////////////////////////////////////////////
            WorkItemStoreContext      sourceStore = new WorkItemStoreContext(me.Source, WorkItemStoreFlags.BypassRules);
            TfsTeamService            sourceTS    = me.Source.Collection.GetService <TfsTeamService>();
            List <TeamFoundationTeam> sourceTL    = sourceTS.QueryTeams(me.Source.Name).ToList();

            Trace.WriteLine(string.Format("Found {0} teams in Source?", sourceTL.Count));
            var sourceTSCS = me.Source.Collection.GetService <TeamSettingsConfigurationService>();
            //////////////////////////////////////////////////
            WorkItemStoreContext targetStore = new WorkItemStoreContext(me.Target, WorkItemStoreFlags.BypassRules);
            Project targetProject            = targetStore.GetProject();

            Trace.WriteLine(string.Format("Found target project as {0}", targetProject.Name));
            TfsTeamService            targetTS = me.Target.Collection.GetService <TfsTeamService>();
            List <TeamFoundationTeam> targetTL = targetTS.QueryTeams(me.Target.Name).ToList();

            Trace.WriteLine(string.Format("Found {0} teams in Target?", targetTL.Count));
            var targetTSCS = me.Target.Collection.GetService <TeamSettingsConfigurationService>();
            //////////////////////////////////////////////////
            int  current   = sourceTL.Count;
            int  count     = 0;
            long elapsedms = 0;

            /// Create teams
            ///
            foreach (TeamFoundationTeam sourceTeam in sourceTL)
            {
                Stopwatch witstopwatch    = Stopwatch.StartNew();
                var       foundTargetTeam = (from x in targetTL where x.Name == sourceTeam.Name select x).SingleOrDefault();
                if (foundTargetTeam == null)
                {
                    Trace.WriteLine(string.Format("Processing team {0}", sourceTeam.Name));
                    TeamFoundationTeam newTeam = targetTS.CreateTeam(targetProject.Uri.ToString(), sourceTeam.Name, sourceTeam.Description, null);
                }
                else
                {
                    Trace.WriteLine(string.Format("Team found.. skipping"));
                }

                witstopwatch.Stop();
                elapsedms = elapsedms + witstopwatch.ElapsedMilliseconds;
                current--;
                count++;
                TimeSpan average   = new TimeSpan(0, 0, 0, 0, (int)(elapsedms / count));
                TimeSpan remaining = new TimeSpan(0, 0, 0, 0, (int)(average.TotalMilliseconds * current));
                Trace.WriteLine("");
                //Trace.WriteLine(string.Format("Average time of {0} per work item and {1} estimated to completion", string.Format(@"{0:s\:fff} seconds", average), string.Format(@"{0:%h} hours {0:%m} minutes {0:s\:fff} seconds", remaining)));
            }
            // Set Team Settings
            //foreach (TeamFoundationTeam sourceTeam in sourceTL)
            //{
            //    Stopwatch witstopwatch = new Stopwatch();
            //    witstopwatch.Start();
            //    var foundTargetTeam = (from x in targetTL where x.Name == sourceTeam.Name select x).SingleOrDefault();
            //    if (foundTargetTeam == null)
            //    {
            //        Trace.WriteLine(string.Format("Processing team {0}", sourceTeam.Name));
            //        var sourceTCfU = sourceTSCS.GetTeamConfigurations((new[] { sourceTeam.Identity.TeamFoundationId })).SingleOrDefault();
            //        TeamSettings newTeamSettings = CreateTargetTeamSettings(sourceTCfU);
            //        TeamFoundationTeam newTeam = targetTS.CreateTeam(targetProject.Uri.ToString(), sourceTeam.Name, sourceTeam.Description, null);
            //        targetTSCS.SetTeamSettings(newTeam.Identity.TeamFoundationId, newTeamSettings);
            //    }
            //    else
            //    {
            //        Trace.WriteLine(string.Format("Team found.. skipping"));
            //    }

            //    witstopwatch.Stop();
            //    elapsedms = elapsedms + witstopwatch.ElapsedMilliseconds;
            //    current--;
            //    count++;
            //    TimeSpan average = new TimeSpan(0, 0, 0, 0, (int)(elapsedms / count));
            //    TimeSpan remaining = new TimeSpan(0, 0, 0, 0, (int)(average.TotalMilliseconds * current));
            //    Trace.WriteLine("");
            //    //Trace.WriteLine(string.Format("Average time of {0} per work item and {1} estimated to completion", string.Format(@"{0:s\:fff} seconds", average), string.Format(@"{0:%h} hours {0:%m} minutes {0:s\:fff} seconds", remaining)));

            //}
            //////////////////////////////////////////////////
            stopwatch.Stop();
            Console.WriteLine(@"DONE in {0:%h} hours {0:%m} minutes {0:s\:fff} seconds", stopwatch.Elapsed);
        }