Skip to content

Instantly share code, notes, and snippets.

@akanieski
Created July 16, 2021 03:50
Show Gist options
  • Save akanieski/04ef1184a3c88df1fc37a9dce0425cb3 to your computer and use it in GitHub Desktop.
Save akanieski/04ef1184a3c88df1fc37a9dce0425cb3 to your computer and use it in GitHub Desktop.
Generate Azure DevOps Security Namespaces and Permissions Enums
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Git.Client;
using Microsoft.VisualStudio.Services.Security.Client;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Clients;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using ADO.ObjectModel.Permissions;
namespace ADO.ObjectModel.Sandbox
{
class Program
{
public static void GeneratePermissionsEnums(TfsTeamProjectCollection tpc, string path)
{
var Security = tpc.GetClient<SecurityHttpClient>();
var Identity = tpc.GetService<IIdentityManagementService>();
var Git = tpc.GetService<GitRepositoryService>();
string file = $"// Auto-Generated - Do not edit {System.Environment.NewLine}using System; {System.Environment.NewLine}namespace ADO.ObjectModel.Permissions {System.Environment.NewLine}{{ ";
foreach (var ns in Security.QuerySecurityNamespacesAsync(Guid.Empty).Result)
{
if (ns.NamespaceId == Guid.Parse("c788c23e-1b46-4162-8f5e-d7585343b5de"))
{
ns.Name = "ReleaseManagement2";
}
file += System.Environment.NewLine + $" public enum {ns.Name.Replace(" ", "")} : int {System.Environment.NewLine}{{";
foreach (var action in ns.Actions)
{
file += System.Environment.NewLine + $" {action.Name} = {action.Bit},";
}
file += System.Environment.NewLine + " }";
}
file += System.Environment.NewLine + System.Environment.NewLine + "public class SecurityNamespaces " + System.Environment.NewLine + "{";
foreach (var ns in Security.QuerySecurityNamespacesAsync(Guid.Empty).Result)
{
if (ns.NamespaceId == Guid.Parse("c788c23e-1b46-4162-8f5e-d7585343b5de"))
{
ns.Name = "ReleaseManagement2";
}
file += System.Environment.NewLine + $" public static Guid {ns.Name.Replace(" ", "")} {{ get => Guid.Parse(\"{ns.NamespaceId}\"); }}";
}
file += System.Environment.NewLine + " }";
file += System.Environment.NewLine + "}";
System.IO.File.WriteAllText(path, file);
}
static void Main(string[] args)
{
// Authenticate using Personal Access Token
VssBasicCredential vssBasicCredential = new VssBasicCredential(string.Empty, "<PAT>");
using (TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("https://dev.azure.com/<YOUR ORG>"), vssBasicCredential))
{
tpc.Authenticate();
GeneratePermissionsEnums(tpc, "..\\..\\namespaces.cs");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment