Skip to content

Instantly share code, notes, and snippets.

@armchairdeity
Last active April 6, 2021 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armchairdeity/27ed76fa6982d1b275662063735bf456 to your computer and use it in GitHub Desktop.
Save armchairdeity/27ed76fa6982d1b275662063735bf456 to your computer and use it in GitHub Desktop.
TFS API wrapper DSL
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Identity;
using Microsoft.VisualStudio.Services.Directories.DirectoryService.Client;
using Microsoft.VisualStudio.Services.Identity.Client;
using Microsoft.VisualStudio.Services.Organization.Client;
using Microsoft.VisualStudio.Services.Profile.Client;
using Microsoft.VisualStudio.Services.Security.Client;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Web;
namespace Crawler.lib.TFSData {
public class Base {
// TFS Collection Object
private TfsTeamProjectCollection TFSCollection { get; set; }
// HTTP CLIENTS START HERE
public TeamHttpClient TeamClient {
get => TFSCollection.GetClient<TeamHttpClient>();
}
public ProjectHttpClient ProjectClient {
get => TFSCollection.GetClient<ProjectHttpClient>();
}
public SecurityHttpClient SecurityClient {
get => TFSCollection.GetClient<SecurityHttpClient>();
}
public DirectoryHttpClient DirectoryClient {
get => TFSCollection.GetClient<DirectoryHttpClient>();
}
public IdentityHttpClient IdentityClient {
get => TFSCollection.GetClient<IdentityHttpClient>();
}
public OrganizationHttpClient OrgClient {
get => TFSCollection.GetClient<OrganizationHttpClient>();
}
public ProfileHttpClient ProfileClient {
get => TFSCollection.GetClient<ProfileHttpClient>();
}
public DirectorySearcher ADSearcher {
get => new DirectorySearcher(new DirectoryEntry("LDAP://mmm.com", "mmm\\a80fbzz", "Gallium^2018"));
}
// END CLIENT OBJECTS
protected PreloadDepthEnum PreloadDepth { get; set; }
public Base(PreloadDepthEnum preloadDepth) {
PreloadDepth = preloadDepth;
TFSCollection = TFSHelper.GetTFSCollectionObject();
}
}
}
using Microsoft.VisualStudio.Services.Identity;
namespace Crawler.lib.TFSData {
public class BaseTFSIdentity : Base {
private Identity Ident { get; set; }
public BaseTFSIdentity(Identity identity, PreloadDepthEnum loadDepth): base(loadDepth) {
Ident = identity;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Crawler.lib.TFSData {
public enum PreloadDepthEnum {
None,
Project,
Teams,
Members,
ADGroups,
ADMembers
}
}
using Microsoft.TeamFoundation.Core.WebApi;
using System;
namespace Crawler.lib.TFSData {
public class TFSProject : Base {
public Guid Id { get => Project.Id; }
public string Name { get => Project.Name; }
private TeamProject Project { get; set; }
private TFSTeams Teams { get; set; }
public TFSProject(TeamProject project, PreloadDepthEnum loadDepth) : base(loadDepth) {
Project = project;
Teams = new TFSTeams(this, Project.DefaultTeam.Id.ToString(), PreloadDepth);
}
public string[] GetTeamNames() {
return Teams.GetTeamNames();
}
public string[] GetTeamNamesSorted() {
return Teams.GetTeamNamesSorted();
}
}
}
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Crawler.lib.TFSData {
public class TFSTeam : Base {
// Properties of this team defined here
public string Name { get => Team.Name; }
public Guid Id { get => Team.Id; }
// MS Extended Client Team
private WebApiTeam Team { get; set; }
// These are OUR objects
private TFSProject Project { get; set; }
private TFSUsers Users { get; set; }
private ADGroupCollection ADGroups { get; set; }
private TFSGroups Groups { get; set; }
public TFSTeam(TFSProject project, WebApiTeam team, PreloadDepthEnum loadDepth) : base(loadDepth) {
Project = project;
Team = team;
Users = new TFSUsers(Project.Id.ToString(), Team.Id.ToString(), PreloadDepth);
Groups = new TFSGroups(Project.Id.ToString(), Team.Id.ToString(), PreloadDepth);
ADGroups = new ADGroupCollection();
if (PreloadDepth >= PreloadDepthEnum.Members) {
Task.WaitAll(new Task[] {
LoadMembershipListsAsync()
});
}
}
//Task.Run(() => this.LoadMembershipListsAsync()).Wait();
/*
* (Identity.IsContainer = true && Identity.Descriptor.IdentityType == "System.Security.Principal.WindowsIdentity") == AD Group
* (Identity.IsContainer = false && Identity.Descriptor.IdentityType == "System.Security.Principal.WindowsIdentity") == AD User
* (Identity.IsContainer = true && Identity.Descriptor.IdentityType == "Microsoft.TeamFoundation.Identity") == TFS Group
* (Identity.IsContainer = false && Identity.Descriptor.IdentityType == "Microsoft.TeamFoundation.Identity") == TFS Group
*/
public Task LoadMembershipListsAsync() {
return Task.Factory.StartNew(async () => {
List<Identity> memberships = (await Task.WhenAll((
await TeamClient.GetTeamMembersAsync(Project.Id.ToString(), Id.ToString())
).Select(async
x => await IdentityClient.ReadIdentityAsync(x.Id, QueryMembership.Expanded)
))).OrderBy(x => x.DisplayName).ToList();
List<TFSUser> users = memberships.Where(m => !m.IsContainer && m.Descriptor.IdentityType == "Microsoft.TeamFoundation.Identity")
.Select(m => new TFSUser(m, PreloadDepth)).ToList();
Users = new TFSUsers(Project.Id.ToString(), Team.Id.ToString(), users, PreloadDepth);
List<TFSGroup> groups = memberships.Where(g => g.IsContainer && g.Descriptor.IdentityType == "Microsoft.TeamFoundation.Identity")
.Select(g => new TFSGroup(g, PreloadDepth)).ToList();
Groups = new TFSGroups(Project.Id.ToString(), Team.Id.ToString(), groups, PreloadDepth);
});
/*
List<ADGroup> adgroups = memberships.Where(
a => a.IsContainer && a.Descriptor.IdentityType == "System.Security.Principal.WindowsIdentity"
).Select(a => new ADGroup(a.DisplayName)).ToList();
*/
}
}
}
using Microsoft.TeamFoundation.Core.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Crawler.lib.TFSData {
public class TFSTeams : Base {
private TFSProject Project { get; set; }
private string DefaultTeamId { get; set; }
private List<TFSTeam> Teams { get; set; }
public TFSTeams(TFSProject project, string defaultTeamId, PreloadDepthEnum loadDepth) : base(loadDepth) {
Project = project;
DefaultTeamId = defaultTeamId;
if (PreloadDepth >= PreloadDepthEnum.Teams) {
Task.WhenAll(LoadAllTeams());
/*
Task.WaitAll(new Task[] {
LoadAllTeams()
});
*/
}
else {
Teams = new List<TFSTeam>();
}
}
private Task<Task> LoadAllTeams() {
return Task.Factory.StartNew(async () => {
Teams = (await TeamClient.GetTeamsAsync(Project.Id.ToString())).Select(t => new TFSTeam(Project, t, PreloadDepth)).ToList();
});
}
public TFSTeam GetTeamById(string id) {
return Teams.Where(x => id == x.Id.ToString()).FirstOrDefault();
}
public TFSTeam GetTeamByName(string teamName) {
return Teams.Where(x => teamName == x.Name).FirstOrDefault();
}
public string[] GetTeamNames() {
return Teams.Select(t => t.Name).ToArray();
}
public string[] GetTeamNamesSorted() {
return Teams.Select(t => t.Name).OrderBy(t => t).ToArray();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment