11using System ;
22using System . Collections . Generic ;
33using System . Diagnostics ;
4+ using System . IO ;
45using System . Linq ;
56using Microsoft . Extensions . Logging ;
67using Microsoft . Extensions . Options ;
@@ -42,30 +43,26 @@ protected override void InternalExecute()
4243 {
4344 Stopwatch stopwatch = Stopwatch . StartNew ( ) ;
4445
45- if ( string . IsNullOrEmpty ( CommonTools . UserMapping . Options . UserMappingFile ) )
46- {
47- Log . LogError ( "UserMappingFile is not set" ) ;
48- throw new ArgumentNullException ( "UserMappingFile must be set on the TfsUserMappingToolOptions in CommonEnrichersConfig." ) ;
49- }
46+ CheckOptions ( ) ;
5047
51- List < IdentityMapData > usersToMap = new List < IdentityMapData > ( ) ;
48+ IdentityMapResult data ;
5249 if ( Options . OnlyListUsersInWorkItems )
5350 {
5451 Log . LogInformation ( "OnlyListUsersInWorkItems is true, only users in work items will be listed" ) ;
5552 List < WorkItemData > sourceWorkItems = Source . WorkItems . GetWorkItems ( Options . WIQLQuery ) ;
5653 Log . LogInformation ( "Processed {0} work items from Source" , sourceWorkItems . Count ) ;
5754
58- usersToMap = CommonTools . UserMapping . GetUsersInSourceMappedToTargetForWorkItems ( this , sourceWorkItems ) ;
59- Log . LogInformation ( "Found {usersToMap} total mapped" , usersToMap . Count ) ;
55+ data = CommonTools . UserMapping . GetUsersInSourceMappedToTargetForWorkItems ( this , sourceWorkItems ) ;
56+ Log . LogInformation ( "Found {usersToMap} total mapped" , data . IdentityMap . Count ) ;
6057 }
6158 else
6259 {
6360 Log . LogInformation ( "OnlyListUsersInWorkItems is false, all users will be listed" ) ;
64- usersToMap = CommonTools . UserMapping . GetUsersInSourceMappedToTarget ( this ) ;
65- Log . LogInformation ( "Found {usersToMap} total mapped" , usersToMap . Count ) ;
61+ data = CommonTools . UserMapping . GetUsersInSourceMappedToTarget ( this ) ;
62+ Log . LogInformation ( "Found {usersToMap} total mapped" , data . IdentityMap . Count ) ;
6663 }
6764
68- usersToMap = usersToMap . Where ( x => x . Source . DisplayName != x . Target ? . DisplayName ) . ToList ( ) ;
65+ List < IdentityMapData > usersToMap = data . IdentityMap . Where ( x => x . Source . DisplayName != x . Target ? . DisplayName ) . ToList ( ) ;
6966 Log . LogInformation ( "Filtered to {usersToMap} total viable mappings" , usersToMap . Count ) ;
7067 Dictionary < string , string > usermappings = [ ] ;
7168 foreach ( IdentityMapData userMapping in usersToMap )
@@ -74,11 +71,40 @@ protected override void InternalExecute()
7471 // it would throw with duplicate key. This way we just overwrite the value – last item in source wins.
7572 usermappings [ userMapping . Source . DisplayName ] = userMapping . Target ? . DisplayName ;
7673 }
77- System . IO . File . WriteAllText ( CommonTools . UserMapping . Options . UserMappingFile , JsonConvert . SerializeObject ( usermappings , Formatting . Indented ) ) ;
78- Log . LogInformation ( "Writen to: {LocalExportJsonFile}" , CommonTools . UserMapping . Options . UserMappingFile ) ;
74+ File . WriteAllText ( CommonTools . UserMapping . Options . UserMappingFile , JsonConvert . SerializeObject ( usermappings , Formatting . Indented ) ) ;
75+ Log . LogInformation ( "User mappings writen to: {LocalExportJsonFile}" , CommonTools . UserMapping . Options . UserMappingFile ) ;
76+ if ( Options . ExportAllUsers )
77+ {
78+ ExportAllUsers ( data ) ;
79+ }
7980
8081 stopwatch . Stop ( ) ;
8182 Log . LogInformation ( "DONE in {Elapsed} seconds" , stopwatch . Elapsed ) ;
8283 }
84+
85+ private void ExportAllUsers ( IdentityMapResult data )
86+ {
87+ var allUsers = new
88+ {
89+ data . SourceUsers ,
90+ data . TargetUsers
91+ } ;
92+ File . WriteAllText ( Options . UserExportFile , JsonConvert . SerializeObject ( allUsers , Formatting . Indented ) ) ;
93+ Log . LogInformation ( "All user writen to: {exportFile}" , Options . UserExportFile ) ;
94+ }
95+
96+ private void CheckOptions ( )
97+ {
98+ if ( string . IsNullOrEmpty ( CommonTools . UserMapping . Options . UserMappingFile ) )
99+ {
100+ Log . LogError ( "UserMappingFile is not set" ) ;
101+ throw new ArgumentNullException ( "UserMappingFile must be set on the TfsUserMappingToolOptions in CommonTools." ) ;
102+ }
103+ if ( Options . ExportAllUsers && string . IsNullOrEmpty ( Options . UserExportFile ) )
104+ {
105+ Log . LogError ( $ "Flag ExportAllUsers is set but export file UserExportFile is not set.") ;
106+ throw new ArgumentNullException ( "UserExportFile must be set on the TfsExportUsersForMappingProcessorOptions in Processors." ) ;
107+ }
108+ }
83109 }
84110}
0 commit comments