使用.NET客户端库针对VSTS / TFS检索构建定义任务列表

时间:2018-09-06 13:19:38

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

我正在将.NET客户端库用于VSTS / TFS(https://docs.microsoft.com/en-us/vsts/integrate/concepts/dotnet-client-libraries?view=vsts),以检索所有团队项目的所有构建定义的任务列表。我使用的是NuGet软件包Microsoft.TeamFoundation.ExtendedClient的v16.139.0预览版(我需要这样做是因为我还需要检索“发行定义”工作流,而您需要Microsoft.VisualStudio.Services.Release.Client,对ExtendedClient的依赖性要求)。服务器(本地)是TFS 2017.2。我绝对无法检索任务/阶段/过程。这是我的代码:

VssConnection connection = new VssConnection(new Uri("http://tfsserver:8080/tfs/defaultcollection"), new VssClientCredentials());
ProjectHttpClient projectClient = connection.GetClient<ProjectHttpClient>();
IEnumerable<TeamProjectReference> projects = projectClient.GetProjects().Result;
BuildHttpClient buildClient = connection.GetClient<BuildHttpClient>();
foreach (var project in projects)
{
  IPagedList<BuildDefinition> buildDefinitions = buildClient.GetFullDefinitionsAsync2(project: project.Name, name: null).Result;
  foreach (BuildDefinition buildDefinition in buildDefinitions)
  {
    // get the tasks
  }
}

有人对如何解决这个问题有想法吗?

3 个答案:

答案 0 :(得分:1)

只需使用.NET客户端库尝试以下C#示例,并在 TFS 2017.3 VSTS 上进行测试,即可使用。 (我没有TFS 2017.2,如果我没记错的话,TFS 2017.2具有与TFS 2015类似的构建过程,它没有“ Process”和“ phases”属性。)

using System;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;

namespace RetrieveTaskList
{
    class Program
    {
        static void Main(string[] args)
        {
            //For TFS :
            var tfsUrl = "http://ws-tfs2017:8080/tfs/DefaultCollection";
            var buildClient = new BuildHttpClient(new Uri(tfsUrl), new VssAadCredential());

            //For VSTS:
            //var tfsUrl = "https://{account}.visualstudio.com";
            //var buildClient = new BuildHttpClient(new Uri(tfsUrl), new VssBasicCredential(string.Empty, "PAT here"));

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

            foreach (var definition in definitions.Result)
            {
                Console.WriteLine(string.Format("\n {0} - {1}:", definition.Id, definition.Name));

                // Get BuildDefinitionStep to array, each of which has a task property that contains things like the name of the task and the inputs.
                var tasks = definition.Steps.ToArray();

                //Get each step/task from the array
                foreach (var task in tasks)
                {
                    Console.WriteLine(task.DisplayName);
                }
            }
            Console.ReadLine();
        }
    }
}

enter image description here


您还可以使用REST API从构建定义中检索任务列表。

例如PowerShell:

Param(
   [string]$baseurl = "http://server:8080/tfs/DefaultCollection", 
   [string]$projectName = "ProjectName",
   [string]$buildDefinitionID = "26",
   [string]$user = "domain\user",
   [string]$token = "password"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))


$uri = "$baseurl/$($projectName)/_apis/build/definitions/$buildDefinitionID"
Write-Host $uri
$result = (Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$tasks = $result.process.phases.steps.displayName

foreach ($task in $tasks)
{
  write-host $task
}

enter image description here

您也可以尝试REST客户端,请参考以下线程:Retrieve VSTS/TFS Build task name list

答案 1 :(得分:1)

在@Andy的帮助下,我得以解决了该问题。我使用Fiddler调用REST Api(http://server:8080/tfs/DefaultCollection/MyProject/_apis/build/definitions/1)并读取JSON响应。我发现“ build”属性包含任务的集合。我修复了@Andy提供的PowerShell脚本:

Param(
   [string]$baseurl = "http://server:8080/tfs/DefaultCollection", 
   [string]$projectName = "MyProject",
   [string]$buildDefinitionID = "530",
   [string]$user = "domain\user",
   [string]$token = "PersonalAccessToken"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$uri = "$baseurl/$($projectName)/_apis/build/definitions/$buildDefinitionID"
Write-Host $uri
$result = (Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})

foreach ($task in $result.build)
{
    Write-Host $task.displayName
}

答案 2 :(得分:1)

聚会晚了一点,但是如果您想使用AzDO .NET Client库遍历Build任务,则需要将Process转换为DesignerProcess / DockerProcess / YamlProcess。

var buildDefinitions = await _buildClient.GetFullDefinitionsAsync(project.Id);
foreach (var buildDefinition in buildDefinitions)
{
    if (buildDefinition.Process != null && buildDefinition.Process is Microsoft.TeamFoundation.Build.WebApi.DesignerProcess designerProcess)
    {
        foreach (var phase in designerProcess.Phases)
            foreach (var step in phase.Steps)
                Console.WriteLine($"taskname={step.DisplayName}");
        break;//lets exit the loop early
    }
}

演示回购,https://github.com/f2calv/azdo-api-net-client-issue