Skip to content

Commit b690d78

Browse files
committed
Match users by email
1 parent 5a7ea90 commit b690d78

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/MigrationTools.Clients.TfsObjectModel/Tools/TfsUserMappingTool.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,40 @@ public List<IdentityMapData> GetUsersInSourceMappedToTarget(TfsProcessor process
134134
var sourceUsers = GetUsersListFromServer(processor.Source.GetService<IGroupSecurityService>());
135135
Log.LogInformation($"TfsUserMappingTool::GetUsersInSourceMappedToTarget Loading identities from target server");
136136
var targetUsers = GetUsersListFromServer(processor.Target.GetService<IGroupSecurityService>());
137-
return sourceUsers.Select(sUser => new IdentityMapData { Source = sUser, Target = targetUsers.SingleOrDefault(tUser => tUser.FriendlyName == sUser.FriendlyName) }).ToList();
137+
138+
if (Options.MatchUsersByEmail)
139+
{
140+
Log.LogInformation("TfsUserMappingTool::GetUsersInSourceMappedToTarget "
141+
+ "Matching users between source and target by email is enabled. In no match by email is found, "
142+
+ "matching by display name will be used.");
143+
}
144+
145+
List<IdentityMapData> identityMap = [];
146+
foreach (var sourceUser in sourceUsers)
147+
{
148+
IdentityItemData targetUser = null;
149+
if (Options.MatchUsersByEmail && !string.IsNullOrEmpty(sourceUser.MailAddress))
150+
{
151+
var candidates = targetUsers
152+
.Where(tu => tu.MailAddress.Equals(sourceUser.MailAddress, StringComparison.OrdinalIgnoreCase))
153+
.ToList();
154+
if (candidates.Count == 1)
155+
{
156+
// If there are more than one user with the same email address, we can't be sure which one is
157+
// the correct one, so mapping will match either by display name, or will be skipped and
158+
// exported for manual mapping.
159+
targetUser = candidates[0];
160+
}
161+
}
162+
targetUser ??= targetUsers.SingleOrDefault(x => x.DisplayName == sourceUser.DisplayName);
163+
identityMap.Add(new IdentityMapData { Source = sourceUser, Target = targetUser });
164+
}
165+
return identityMap;
138166
}
139167
else
140168
{
141169
Log.LogWarning("TfsUserMappingTool is disabled in settings. You may have users in the source that are not mapped to the target. ");
142-
return null;
170+
return [];
143171
}
144172
}
145173

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using Microsoft.TeamFoundation.Build.Client;
4-
using MigrationTools.Enrichers;
1+
using System.Collections.Generic;
52
using MigrationTools.Tools.Infrastructure;
63

74
namespace MigrationTools.Tools
@@ -10,7 +7,7 @@ public class TfsUserMappingToolOptions : ToolOptions, ITfsUserMappingToolOptions
107
{
118

129
/// <summary>
13-
/// This is a list of the Identiy fields in the Source to check for user mapping purposes. You should list all identiy fields that you wan to map.
10+
/// This is a list of the Identiy fields in the Source to check for user mapping purposes. You should list all identiy fields that you want to map.
1411
/// </summary>
1512
public List<string> IdentityFieldsToCheck { get; set; }
1613

@@ -19,11 +16,17 @@ public class TfsUserMappingToolOptions : ToolOptions, ITfsUserMappingToolOptions
1916
/// </summary>
2017
public string UserMappingFile { get; set; }
2118

19+
/// <summary>
20+
/// By default, users in source are mapped to target users by their display name. If this is set to true, then the
21+
/// users will be mapped by their email address first. If no match is found, then the display name will be used.
22+
/// </summary>
23+
public bool MatchUsersByEmail { get; set; }
2224
}
2325

2426
public interface ITfsUserMappingToolOptions
2527
{
2628
List<string> IdentityFieldsToCheck { get; set; }
2729
string UserMappingFile { get; set; }
30+
bool MatchUsersByEmail { get; set; }
2831
}
29-
}
32+
}

0 commit comments

Comments
 (0)