通过TFS API从Visual Studio Online获取构建定义

Chr*_*tle 4 c# tfs visual-studio azure-devops

我在通过TFS API从Visual Studio Online项目检索构建信息时遇到问题.我似乎可以连接到项目,并找到预期的团队项目集合和团队项目,但是尽管项目有两个构建定义和一些构建,但在查询构建时,TFS API总是向我返回零长度数组定义或构建.该项目是一个Git项目.我的生产代码类似于我为试图调试问题而编写的测试代码:

var tfsUri = new Uri("https://[my project].visualstudio.com");
var tcs = new TfsConfigurationServer(tfsUri);

var teamProjCollections = tcs
    .CatalogNode
    .QueryChildren(
        new[] { CatalogResourceTypes.ProjectCollection },
        false,
        CatalogQueryOptions.None)
    .Select(collectionNode => new Guid(collectionNode.Resource.Properties["InstanceId"]))
    .Select(collectionId => tcs.GetTeamProjectCollection(collectionId));

var tpc = teamProjCollections
    .Single(x => x.Name == @"[my project].visualstudio.com\DefaultCollection");

var newTeamProjects = tpc
    .CatalogNode
    .QueryChildren(
        new[] { CatalogResourceTypes.TeamProject },
        false,
        CatalogQueryOptions.None);

var tp = newTeamProjects
    .Single(x => x.Resource.DisplayName == "[team project name]");

var buildServer = tpc.GetService<IBuildServer>();
var buildDefinitions = buildServer.QueryBuildDefinitions(tp.Resource.DisplayName);
Run Code Online (Sandbox Code Playgroud)

buildDefinitions永远是一个空数组.构建定义肯定存在 - 我可以使用Visual Studio连接到项目,并在Team Explorer的Builds选项卡中显示它们.这段代码过去对我有用,但它始终是我连接的TFVC项目,而不是Git项目.有趣的是,如果我使用VersionControlServer团队项目集合中的一个,如下所示:

var vcs = tpc.GetService<VersionControlServer>();
var teamProjects = vcs.GetAllTeamProjects(true);
Run Code Online (Sandbox Code Playgroud)

.. teamProjects始终是零长度数组.

此外,如果我尝试通过指定非常通用的构建详细规范来获取团队项目集合的所有构建,则不会返回任何构建.

一些其他信息:我正在使用VS Enterprise 2015 RC.我是VSO项目的管理员,并创建了团队项目集合,团队项目和构建定义.我推了一些代码,然后运行一些构建.我通过API连接时以自己的身份登录.我的凭据似乎是正确的.

所以,在试图解决这个问题时,我有一些问题.我是否需要一种不同的方法来访问Git存储库?也许它只能通过新的TFS 2015 REST API实现?也许我需要在VSO项目中让自己的构建"可见"?

nca*_*ona 7

您可以使用BuildHttpClient发出与2.0 REST API类似的请求.

GitHub上的这个示例是一个相当不错的资源,如果您希望轮询单个项目,它应该看起来更像这样:

using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.VisualStudio.Services.Client;

static void GetBuildStatus()
{
    var tfsUrl = "http://<tfs url>:<port>/tfs/<collectionname>";
    var buildClient = new BuildHttpClient(new Uri(tfsUrl), new VssAadCredential());
    var definitions = buildClient.GetDefinitionsAsync(project: "<projectmame>");
    var builds = buildClient.GetBuildsAsync("<projectname>";

    foreach (var build in builds.Result)
    {
        Console.WriteLine(String.Format("{0} - {1} - {2} - {3}", build.Definition.Name, build.Id.ToString(), build.Status.ToString(), build.StartTime.ToString()));
    }
}
Run Code Online (Sandbox Code Playgroud)