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

查看:74
本文介绍了通过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.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);

buildDefinitions始终是一个空数组.那里的构建定义是 definely -我可以使用Visual Studio连接到项目,并在Team Explorer的Builds选项卡中显示它们.这段代码过去对我有用,但是它一直是我连接的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上的示例是一个很好的资源,如果您愿意轮询单个项目,应该看起来像这样:

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天全站免登陆