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

我在通过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(); var buildDefinitions = buildServer.QueryBuildDefinitions(tp.Resource.DisplayName); 

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

 var vcs = tpc.GetService(); var teamProjects = vcs.GetAllTeamProjects(true); 

.. teamProjects总是一个零长度数组。

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

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

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

您可以使用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/"; var buildClient = new BuildHttpClient(new Uri(tfsUrl), new VssAadCredential()); var definitions = buildClient.GetDefinitionsAsync(project: ""); var builds = buildClient.GetBuildsAsync(""; 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())); } } 

我相信你是对的我刚刚在GIT项目上对我自己的VSO测试了你的代码,我无法检索任何build.vNext构建,但是我可以检索旧的XAML构建定义。

然后我尝试使用REST API ,它也只能返回旧式XAML构建,直到我使用REST API的第2版,然后回来了XAML和build.vNext样式构建。 这是我在浏览器中尝试的示例字符串(确保首先登录VSO进行测试)

 https://[vsoname].visualstudio.com/defaultcollection/[project]/_apis/build/definitions?api-version=2.0