簡體   English   中英

使用 VSTS/TFS 的 .NET 客戶端庫檢索構建定義的任務列表

[英]Retrieve the list of Tasks for Build Definition using .NET client libraries for VSTS/TFS

我正在使用 VSTS/TFS 的 .NET 客戶端庫( 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
  }
}
  • 我嘗試使用 buildClient.GetDefinitionAsync 重新檢索構建定義而沒有額外效果
  • “Steps”屬性(將被棄用)始終為空
  • “進程”屬性為空
  • 沒有可用的“階段”屬性(從構建定義中包含多個階段的選項來看,這似乎是合乎邏輯的
  • BuildDefinitionStep 有合同可用: https ://docs.microsoft.com/en-us/vsts/extend/reference/client/api/tfs/build/contracts/builddefinitionstep ? view = vsts
  • REST API 文檔沒有名為“Step”的屬性: https : //docs.microsoft.com/en-us/rest/api/vsts/build/definitions/get?view= vsts-rest-4.1# builddefinition

有沒有人知道如何解決這個問題?

參加聚會有點晚了,但是如果您想使用 AzDO .NET 客戶端庫迭代構建任務,您需要將 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

只需使用 .NET 客戶端庫嘗試下面的 C# 示例,在TFS 2017.3VSTS上測試,都可以工作。 (我這邊沒有 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();
        }
    }
}

在此處輸入圖片說明


您還可以使用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
}

在此處輸入圖片說明

您也可以嘗試 REST Client,請參考此線程: 檢索 VSTS/TFS 構建任務名稱列表

在@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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM