通过 TFS API 从 Visual Studio Online 获取构建定义 [英] Fetching build definitions from Visual Studio Online through TFS API

查看:29
本文介绍了通过 TFS API 从 Visual Studio Online 获取构建定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a problem retrieving build information from a Visual Studio Online project through the TFS API. I can connect to the project, it seems, and find the expected team project collection and team project, but despite the project having two build definitions and a number of builds, the TFS API always returns zero-length arrays to me when querying for build definitions or builds. The project is a Git project. My production code is similar to this test code I've written to attempt to debug the problem:

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.comDefaultCollection");

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

buildDefinitions 总是一个空数组.构建定义肯定在那里 - 我可以使用 Visual Studio 连接到项目,并在团队资源管理器的构建选项卡中显示它们.这段代码过去对我有用,但是我一直连接到 TFVC 项目,而不是 Git 项目.有趣的是,如果我使用团队项目集合中的 VersionControlServer,就像这样:

buildDefinitions is always an empty array. The build definitions are definitely there - I can connect to the project with Visual Studio, and it displays them in Builds tab of the Team Explorer. This code has worked for me in the past, however it was always TFVC projects I was connecting to, not a Git project. Interestingly, if I use a VersionControlServer from the team project collection, like this:

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

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

..teamProjects is always a zero-length array.

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

Also, if I try to get all builds for the team project collection by specifying a very general build detail spec, I get no builds returned.

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

Some additional information: I'm using VS Enterprise 2015 RC. I'm the administrator of the VSO project, and created the team project collection, the team project, and the build definitions. I've pushed some code, and run some builds. I'm logged in as myself when connecting through the API. My credentials appear to be correct.

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

So, in trying to figure this out, I have some questions. Do I need a different approach to accessing a Git repository? Perhaps it's only possible through the new TFS 2015 REST APIs? Perhaps I need to make my builds "visible" to myself in the VSO project?

推荐答案

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

You can utilize the BuildHttpClient to make similar requests as you would with the 2.0 REST API.

GitHub 上的这个 sample 是一个非常好的资源,如果您更愿意轮询单个项目的话应该看起来更像这样:

This sample on GitHub is a pretty good resource, if you would rather poll a single project it should look something more like this:

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

这篇关于通过 TFS API 从 Visual Studio Online 获取构建定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆