11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using Microsoft . Extensions . DependencyInjection ;
45using Microsoft . Extensions . Logging ;
56using Microsoft . TeamFoundation . Client ;
67using Microsoft . TeamFoundation . Core . WebApi . Types ;
78using Microsoft . TeamFoundation . Framework . Client ;
89using Microsoft . TeamFoundation . Work . WebApi ;
910using Microsoft . VisualStudio . Services . WebApi ;
11+ using MigrationTools . Tools ;
1012
1113namespace MigrationTools . Processors
1214{
@@ -21,9 +23,11 @@ internal static void MigrateCapacities(
2123 TeamFoundationTeam targetTeam ,
2224 Dictionary < string , string > iterationMap ,
2325 Lazy < List < TeamFoundationIdentity > > identityCache ,
26+ bool useUserMapping ,
2427 ITelemetryLogger telemetry ,
2528 ILogger log ,
26- LogLevel exceptionLogLevel )
29+ LogLevel exceptionLogLevel ,
30+ IServiceProvider services )
2731 {
2832 log . LogInformation ( "Migrating team capacities.." ) ;
2933
@@ -48,7 +52,7 @@ internal static void MigrateCapacities(
4852 List < TeamMemberCapacityIdentityRef > sourceCapacities = GetSourceCapacities (
4953 sourceHttpClient , sourceTeamContext , sourceIteration ) ;
5054 List < TeamMemberCapacityIdentityRef > targetCapacities = GetTargetCapacities (
51- sourceCapacities , targetIteration . Path , identityCache , log ) ;
55+ sourceCapacities , targetIteration . Path , useUserMapping , identityCache , log , services ) ;
5256
5357 if ( targetCapacities . Count > 0 )
5458 {
@@ -83,13 +87,22 @@ private static List<TeamMemberCapacityIdentityRef> GetSourceCapacities(
8387 private static List < TeamMemberCapacityIdentityRef > GetTargetCapacities (
8488 List < TeamMemberCapacityIdentityRef > sourceCapacities ,
8589 string targetIteration ,
90+ bool useUserMapping ,
8691 Lazy < List < TeamFoundationIdentity > > identityCache ,
87- ILogger log )
92+ ILogger log ,
93+ IServiceProvider services )
8894 {
89- var targetCapacities = new List < TeamMemberCapacityIdentityRef > ( ) ;
95+ List < TeamMemberCapacityIdentityRef > targetCapacities = [ ] ;
96+ Dictionary < string , string > userMapping = null ;
97+ if ( useUserMapping )
98+ {
99+ TfsCommonTools commonTools = services . GetRequiredService < TfsCommonTools > ( ) ;
100+ userMapping = commonTools . UserMapping . UserMappings . Value ;
101+ }
102+
90103 foreach ( var sourceCapacity in sourceCapacities )
91104 {
92- if ( TryMatchIdentity ( sourceCapacity , identityCache , out TeamFoundationIdentity targetIdentity ) )
105+ if ( TryMatchIdentity ( sourceCapacity , identityCache , userMapping , out TeamFoundationIdentity targetIdentity ) )
93106 {
94107 targetCapacities . Add ( new TeamMemberCapacityIdentityRef
95108 {
@@ -127,51 +140,66 @@ private static void ReplaceTargetCapacities(
127140 private static bool TryMatchIdentity (
128141 TeamMemberCapacityIdentityRef sourceCapacity ,
129142 Lazy < List < TeamFoundationIdentity > > identityCache ,
143+ Dictionary < string , string > userMapping ,
130144 out TeamFoundationIdentity targetIdentity )
131145 {
132- var sourceDisplayName = sourceCapacity . TeamMember . DisplayName ;
133- var index = sourceDisplayName . IndexOf ( "<" ) ;
146+ var sourceName = sourceCapacity . TeamMember . DisplayName ;
147+ var index = sourceName . IndexOf ( "<" ) ;
134148 if ( index > 0 )
135149 {
136- sourceDisplayName = sourceDisplayName . Substring ( 0 , index ) . Trim ( ) ;
150+ sourceName = sourceName . Substring ( 0 , index ) . Trim ( ) ;
137151 }
138152
153+ targetIdentity = MatchIdentity ( sourceName , identityCache ) ;
154+ if ( ( targetIdentity is null ) && ( userMapping is not null ) )
155+ {
156+ if ( userMapping . TryGetValue ( sourceName , out var mappedName ) && ! string . IsNullOrEmpty ( mappedName ) )
157+ {
158+ targetIdentity = MatchIdentity ( mappedName , identityCache ) ;
159+ }
160+ }
161+
162+ // last attempt to match on unique name
163+ // Match: "John Michael Bolden" to Bolden, "John Michael" on "[email protected] " unique name 164+ if ( targetIdentity is null )
165+ {
166+ var sourceUniqueName = sourceCapacity . TeamMember . UniqueName ;
167+ targetIdentity = identityCache . Value . FirstOrDefault ( i => i . UniqueName == sourceUniqueName ) ;
168+ }
169+
170+ return targetIdentity is not null ;
171+ }
172+
173+ private static TeamFoundationIdentity MatchIdentity ( string sourceName , Lazy < List < TeamFoundationIdentity > > identityCache )
174+ {
139175 // Match:
140176 // "Doe, John" to "Doe, John"
141177 // "John Doe" to "John Doe"
142- targetIdentity = identityCache . Value . FirstOrDefault ( i => i . DisplayName == sourceDisplayName ) ;
178+ TeamFoundationIdentity targetIdentity = identityCache . Value . FirstOrDefault ( i => i . DisplayName == sourceName ) ;
143179 if ( targetIdentity == null )
144180 {
145- if ( sourceDisplayName . Contains ( ", " ) )
181+ if ( sourceName . Contains ( ", " ) )
146182 {
147183 // Match:
148184 // "Doe, John" to "John Doe"
149- var splitName = sourceDisplayName . Split ( ',' ) ;
150- sourceDisplayName = $ "{ splitName [ 1 ] . Trim ( ) } { splitName [ 0 ] . Trim ( ) } ";
151- targetIdentity = identityCache . Value . FirstOrDefault ( i => i . DisplayName == sourceDisplayName ) ;
185+ var splitName = sourceName . Split ( ',' ) ;
186+ sourceName = $ "{ splitName [ 1 ] . Trim ( ) } { splitName [ 0 ] . Trim ( ) } ";
187+ targetIdentity = identityCache . Value . FirstOrDefault ( i => i . DisplayName == sourceName ) ;
152188 }
153189 else
154190 {
155- if ( sourceDisplayName . Contains ( ' ' ) )
191+ if ( sourceName . Contains ( ' ' ) )
156192 {
157193 // Match:
158194 // "John Doe" to "Doe, John"
159- var splitName = sourceDisplayName . Split ( ' ' ) ;
160- sourceDisplayName = $ "{ splitName [ 1 ] . Trim ( ) } , { splitName [ 0 ] . Trim ( ) } ";
161- targetIdentity = identityCache . Value . FirstOrDefault ( i => i . DisplayName == sourceDisplayName ) ;
195+ var splitName = sourceName . Split ( ' ' ) ;
196+ sourceName = $ "{ splitName [ 1 ] . Trim ( ) } , { splitName [ 0 ] . Trim ( ) } ";
197+ targetIdentity = identityCache . Value . FirstOrDefault ( i => i . DisplayName == sourceName ) ;
162198 }
163199 }
164-
165- // last attempt to match on unique name
166- // Match: "John Michael Bolden" to Bolden, "John Michael" on "[email protected] " unique name 167- if ( targetIdentity == null )
168- {
169- var sourceUniqueName = sourceCapacity . TeamMember . UniqueName ;
170- targetIdentity = identityCache . Value . FirstOrDefault ( i => i . UniqueName == sourceUniqueName ) ;
171- }
172200 }
173201
174- return targetIdentity is not null ;
202+ return targetIdentity ;
175203 }
176204 }
177205}
0 commit comments