Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions Source/TeamMate/Services/ResolverService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.Services.Graph.Client;
using Microsoft.VisualStudio.Services.Users;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand All @@ -10,21 +11,23 @@ namespace Microsoft.Tools.TeamMate.Services
{
public class ResolverService
{
private List<GraphUser> GraphUserCache { get; set; }
private Dictionary<string, VisualStudio.Services.Common.SubjectDescriptor> GraphUserCache { get; set; }

private List<GraphGroup> GraphGroupCache { get; set; }
private Dictionary<string, VisualStudio.Services.Common.SubjectDescriptor> GraphGroupCache { get; set; }

private List<Task> Tasks = new List<Task>();

private async Task<List<GraphUser>> FetchUsersAsync(
private bool Cached = false;

private async Task<Dictionary<string, VisualStudio.Services.Common.SubjectDescriptor>> FetchUsersAsync(
GraphHttpClient graphClient)
{
if (GraphUserCache != null)
{
return GraphUserCache;
}

List<GraphUser> users = new List<GraphUser>();
var users = new Dictionary<string, VisualStudio.Services.Common.SubjectDescriptor>();

string continuationToken = null;
do
Expand All @@ -33,7 +36,10 @@ private async Task<List<GraphUser>> FetchUsersAsync(
continuationToken = data.ContinuationToken != null ? data.ContinuationToken.First() : null;
foreach (var user in data.GraphUsers)
{
users.Add(user);
if (user.MailAddress != null)
{
users[user.MailAddress] = user.Descriptor;
}
}
}
while (continuationToken != null);
Expand All @@ -43,15 +49,15 @@ private async Task<List<GraphUser>> FetchUsersAsync(
return users;
}

private async Task<List<GraphGroup>> FetchGroupsAsync(
private async Task<Dictionary<string, VisualStudio.Services.Common.SubjectDescriptor>> FetchGroupsAsync(
GraphHttpClient graphClient)
{
if (GraphGroupCache != null)
{
return GraphGroupCache;
}

List<GraphGroup> groups = new List<GraphGroup>();
var groups = new Dictionary<string, VisualStudio.Services.Common.SubjectDescriptor>();

string continuationToken = null;
do
Expand All @@ -61,7 +67,10 @@ private async Task<List<GraphGroup>> FetchGroupsAsync(
continuationToken = data.ContinuationToken != null ? data.ContinuationToken.First() : null;
foreach (var group in data.GraphGroups)
{
groups.Add(group);
if (group.MailAddress != null)
{
groups[group.MailAddress] = group.Descriptor;
}
}
}
while (continuationToken != null);
Expand All @@ -71,11 +80,19 @@ private async Task<List<GraphGroup>> FetchGroupsAsync(
return groups;
}

public void FetchDataSync(
private void FetchDataSyncIfNeeded(
GraphHttpClient client)
{
Tasks.Add(FetchUsersAsync(client));
Tasks.Add(FetchGroupsAsync(client));
lock (Tasks)
{
if (!Cached)
{
Tasks.Add(FetchUsersAsync(client));
Tasks.Add(FetchGroupsAsync(client));

Cached = true;
}
}
}

public async Task<Guid?> Resolve(
Expand All @@ -87,32 +104,31 @@ public void FetchDataSync(
return null;
}

this.FetchDataSyncIfNeeded(client);

await Task.Run(() => { foreach (var task in this.Tasks) { task.Wait(); } });

foreach (var user in GraphUserCache)
{
if (user.MailAddress != null &&
(user.MailAddress.Contains(value)))
if (user.Key.Contains(value))
{
var storageKey = client.GetStorageKeyAsync(user.Descriptor).Result;
var storageKey = client.GetStorageKeyAsync(user.Value).Result;

return storageKey.Value;
}
}

foreach (var group in GraphGroupCache)
{
if (group.MailAddress != null &&
(group.MailAddress.Contains(value)))
if (group.Key.Contains(value))
{
var storageKey = client.GetStorageKeyAsync(group.Descriptor).Result;
var storageKey = client.GetStorageKeyAsync(group.Value).Result;

return storageKey.Value;
}
}

throw new ArgumentException("Could not resolve '" + value + "'. Try the full email for the person and/or group.");

}
}
}
3 changes: 0 additions & 3 deletions Source/TeamMate/Services/VstsConnectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@ private async Task<ProjectContext> DoConnectAsync(ProjectInfo projectInfo, Cance
projectContext.WorkItemFieldsByName = fields.ToDictionary(f => f.ReferenceName, StringComparer.OrdinalIgnoreCase);
projectContext.RequiredWorkItemFieldNames = GetWorkItemFieldsToPrefetch(projectContext.WorkItemFieldsByName);

this.ResolverService.FetchDataSync(
graphClient);

return projectContext;
}
catch (Exception e)
Expand Down