1+ using  Microsoft . TeamFoundation . Client ; 
2+ using  Microsoft . TeamFoundation . ProcessConfiguration . Client ; 
3+ using  Microsoft . TeamFoundation . WorkItemTracking . Client ; 
4+ using  System ; 
5+ using  System . Collections ; 
6+ using  System . Collections . Generic ; 
7+ using  System . Diagnostics ; 
8+ using  System . Globalization ; 
9+ using  System . Linq ; 
10+ using  System . Text ; 
11+ using  System . Text . RegularExpressions ; 
12+ using  VSTS . DataBulkEditor . Engine . Configuration . Processing ; 
13+ 
14+ namespace  VSTS . DataBulkEditor . Engine 
15+ { 
16+     public  class  TeamMigrationContext  :  MigrationContextBase 
17+     { 
18+ 
19+         TeamMigrationConfig  _config ; 
20+         MigrationEngine  _me ; 
21+ 
22+         public  override  string  Name 
23+         { 
24+             get 
25+             { 
26+                 return  "TeamMigrationContext" ; 
27+             } 
28+         } 
29+ 
30+         public  TeamMigrationContext ( MigrationEngine  me ,  TeamMigrationConfig  config )  :  base ( me ,  config ) 
31+         { 
32+             _me  =  me ; 
33+             _config  =  config ; 
34+         } 
35+ 
36+         internal  override  void  InternalExecute ( ) 
37+         { 
38+             Stopwatch  stopwatch  =  new  Stopwatch ( ) ; 
39+             stopwatch . Start ( ) ; 
40+             ////////////////////////////////////////////////// 
41+             WorkItemStoreContext  sourceStore  =  new  WorkItemStoreContext ( me . Source ,  WorkItemStoreFlags . BypassRules ) ; 
42+             TfsTeamService  sourceTS  =  me . Source . Collection . GetService < TfsTeamService > ( ) ; 
43+             List < TeamFoundationTeam >  sourceTL  =  sourceTS . QueryTeams ( me . Source . Name ) . ToList ( ) ; 
44+             Trace . WriteLine ( string . Format ( "Found {0} teams in Source?" ,  sourceTL . Count ) ) ; 
45+             var  sourceTSCS  =  me . Source . Collection . GetService < TeamSettingsConfigurationService > ( ) ; 
46+             ////////////////////////////////////////////////// 
47+             WorkItemStoreContext  targetStore  =  new  WorkItemStoreContext ( me . Target ,  WorkItemStoreFlags . BypassRules ) ; 
48+             Project  targetProject  =  targetStore . GetProject ( ) ; 
49+             Trace . WriteLine ( string . Format ( "Found target project as {0}" ,  targetProject . Name ) ) ; 
50+             TfsTeamService  targetTS  =  me . Target . Collection . GetService < TfsTeamService > ( ) ; 
51+             List < TeamFoundationTeam >  targetTL  =  targetTS . QueryTeams ( me . Target . Name ) . ToList ( ) ; 
52+             Trace . WriteLine ( string . Format ( "Found {0} teams in Target?" ,  targetTL . Count ) ) ; 
53+             var  targetTSCS  =  me . Target . Collection . GetService < TeamSettingsConfigurationService > ( ) ; 
54+             ////////////////////////////////////////////////// 
55+             int  current  =  sourceTL . Count ; 
56+             int  count  =  0 ; 
57+             long  elapsedms  =  0 ; 
58+ 
59+             /// Create teams 
60+             ///  
61+             foreach  ( TeamFoundationTeam  sourceTeam  in  sourceTL ) 
62+             { 
63+                 Stopwatch  witstopwatch  =  new  Stopwatch ( ) ; 
64+                 witstopwatch . Start ( ) ; 
65+                 var  foundTargetTeam  =  ( from  x  in  targetTL  where  x . Name  ==  sourceTeam . Name  select  x ) . SingleOrDefault ( ) ; 
66+                 if  ( foundTargetTeam  ==  null ) 
67+                 { 
68+                     Trace . WriteLine ( string . Format ( "Processing team {0}" ,  sourceTeam . Name ) ) ; 
69+                     TeamFoundationTeam  newTeam  =  targetTS . CreateTeam ( targetProject . Uri . ToString ( ) ,  sourceTeam . Name ,  sourceTeam . Description ,  null ) ; 
70+                 } 
71+                 else 
72+                 { 
73+                     Trace . WriteLine ( string . Format ( "Team found.. skipping" ) ) ; 
74+                 } 
75+ 
76+                 witstopwatch . Stop ( ) ; 
77+                 elapsedms  =  elapsedms  +  witstopwatch . ElapsedMilliseconds ; 
78+                 current -- ; 
79+                 count ++ ; 
80+                 TimeSpan  average  =  new  TimeSpan ( 0 ,  0 ,  0 ,  0 ,  ( int ) ( elapsedms  /  count ) ) ; 
81+                 TimeSpan  remaining  =  new  TimeSpan ( 0 ,  0 ,  0 ,  0 ,  ( int ) ( average . TotalMilliseconds  *  current ) ) ; 
82+                 Trace . WriteLine ( "" ) ; 
83+                 //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))); 
84+             } 
85+             // Set Team Settings 
86+             //foreach (TeamFoundationTeam sourceTeam in sourceTL) 
87+             //{ 
88+             //    Stopwatch witstopwatch = new Stopwatch(); 
89+             //    witstopwatch.Start(); 
90+             //    var foundTargetTeam = (from x in targetTL where x.Name == sourceTeam.Name select x).SingleOrDefault(); 
91+             //    if (foundTargetTeam == null) 
92+             //    { 
93+             //        Trace.WriteLine(string.Format("Processing team {0}", sourceTeam.Name)); 
94+             //        var sourceTCfU = sourceTSCS.GetTeamConfigurations((new[] { sourceTeam.Identity.TeamFoundationId })).SingleOrDefault(); 
95+             //        TeamSettings newTeamSettings = CreateTargetTeamSettings(sourceTCfU); 
96+             //        TeamFoundationTeam newTeam = targetTS.CreateTeam(targetProject.Uri.ToString(), sourceTeam.Name, sourceTeam.Description, null); 
97+             //        targetTSCS.SetTeamSettings(newTeam.Identity.TeamFoundationId, newTeamSettings); 
98+             //    } 
99+             //    else 
100+             //    { 
101+             //        Trace.WriteLine(string.Format("Team found.. skipping")); 
102+             //    } 
103+ 
104+             //    witstopwatch.Stop(); 
105+             //    elapsedms = elapsedms + witstopwatch.ElapsedMilliseconds; 
106+             //    current--; 
107+             //    count++; 
108+             //    TimeSpan average = new TimeSpan(0, 0, 0, 0, (int)(elapsedms / count)); 
109+             //    TimeSpan remaining = new TimeSpan(0, 0, 0, 0, (int)(average.TotalMilliseconds * current)); 
110+             //    Trace.WriteLine(""); 
111+             //    //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))); 
112+ 
113+             //} 
114+             ////////////////////////////////////////////////// 
115+             stopwatch . Stop ( ) ; 
116+             Console . WriteLine ( @"DONE in {0:%h} hours {0:%m} minutes {0:s\:fff} seconds" ,  stopwatch . Elapsed ) ; 
117+         } 
118+ 
119+ 
120+         private  TeamSettings  CreateTargetTeamSettings ( TeamConfiguration  sourceTCfU ) 
121+         { 
122+             /////////////////////////////////////////////////// 
123+             TeamSettings  newTeamSettings  =  sourceTCfU . TeamSettings ; 
124+             newTeamSettings . BacklogIterationPath  =  newTeamSettings . BacklogIterationPath . Replace ( me . Source . Name ,  me . Target . Name ) ; 
125+             List < string >  newIterationPaths  =  new  List < string > ( ) ; 
126+             foreach  ( var  ip  in  newTeamSettings . IterationPaths ) 
127+             { 
128+                 newIterationPaths . Add ( ip . Replace ( me . Source . Name ,  me . Target . Name ) ) ; 
129+             } 
130+             newTeamSettings . IterationPaths  =  newIterationPaths . ToArray ( ) ; 
131+ 
132+             /////////////////////////////////////////////////// 
133+             return  newTeamSettings ; 
134+         } 
135+     } 
136+ } 
0 commit comments