MAHA
MAHA

Reputation: 13

C# find all devops build definitions with DEPECRATED tasks

We updated from TFS Version 2017 to Azure Devops. We see now that we have some build steps they are marked with DEPRECATED. The idea is now that we write a C# console application which generate a list with all DEPRECATED build steps. Finally we don't find a property in Microsoft.TeamFoundation.Build.WebApi.BuildDefinitionStep which we can check is this step marked as DEPRECATED or not.

We tried this with the code below written in C#. Variable step has not a property which we can check is the step deprecated or not.

static void Main(string[] args)
{
    //For TFS :
    var tfsUrl = "http://[serername]:[port]/[tfs]/[name]";
    var buildClient = new BuildHttpClient(new Uri(tfsUrl), new VssAadCredential());

    var definitions = buildClient.GetFullDefinitionsAsync(project: "Projects");

    foreach (var definition in definitions.Result)
    {
        Console.WriteLine(string.Format("\n {0} - {1}:", definition.Id, definition.Name));
        foreach(var phase in ((Microsoft.TeamFoundation.Build.WebApi.DesignerProcess)definition.Process).Phases)
        {
            foreach(var step in phase.Steps)
            {
                Console.WriteLine($"{step.DisplayName} has no property to check is this step marked as deprecated or not.");
            }
        }
    }
    Console.ReadLine();
}

Upvotes: 1

Views: 295

Answers (1)

beskuet
beskuet

Reputation: 176

What you try to achieve is not available in Microsoft.TeamFoundationServer.Client NuGet package.

But you can request your DevOps server with an HttpClient to "_apis/distributedTask/tasks/{id}" endpoint. You would get back a JSON object in the response where the deprecated field is available if your task definition is depracated. Finally you can serialize your JSON into a dynamic object to check the deprecated property.

static void Main(string[] args)
{
    //For TFS :
    var tfsUrl = "http://[serername]:[port]/[tfs]/[name]";
    var buildClient = new BuildHttpClient(new Uri(tfsUrl), new VssAadCredential());

    var definitions = buildClient.GetFullDefinitionsAsync(project: "Projects");

    foreach (var definition in definitions.Result)
    {
        Console.WriteLine($"Check {definition.Id} - {definition.Name}...");
        foreach (var phase in ((Microsoft.TeamFoundation.Build.WebApi.DesignerProcess)definition.Process).Phases)
        {
            foreach (var step in phase.Steps)
            {
                var handler = new HttpClientHandler();
                handler.UseDefaultCredentials = true;
                var client = new HttpClient(handler);
                client.BaseAddress = new Uri(tfsUrl);
                var response = client.GetAsync($"_apis/distributedTask/tasks/{step.TaskDefinition.Id}").Result;

                var jsonResponse = response.Content.ReadAsStringAsync().Result;

                dynamic d = JObject.Parse(jsonResponse);
                if (d.Result != null && d.value[0].deprecated == true)
                {
                    Console.WriteLine($"'{step.DisplayName}' is deprecated");
                }
            }
        }
    }
    Console.WriteLine("Press any key to continue..");
    Console.ReadLine();
}

Upvotes: 1

Related Questions