C#使用DEPECRATED任务查找所有devops构建定义

时间:2019-07-12 07:59:38

标签: c# azure-devops-rest-api

我们从TFS Version 2017更新为Azure Devops。现在我们看到我们有一些构建步骤,它们被标记为DEPRECATED。现在的想法是,我们编写一个C#控制台应用程序,该应用程序生成包含所有DEPRECATED构建步骤的列表。最后,我们在Microsoft.TeamFoundation.Build.WebApi.BuildDefinitionStep中找不到属性,可以检查此步骤是否标记为DEPRECATED

我们使用下面用C#编写的代码进行了尝试。变量step没有一个属性,我们可以检查是否不赞成使用该步骤。

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();
}

1 个答案:

答案 0 :(得分:1)

Microsoft.TeamFoundationServer.Client NuGet包中没有您尝试实现的目标。

但是您可以使用HttpClient请求DevOps服务器到“ _apis / distributedTask / tasks / {id}”端点。如果任务定义被取消,您将在响应中返回一个JSON对象,其中不赞成使用的字段可用。最后,您可以将JSON序列化为动态对象以检查不推荐使用的属性。

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();
}