Fetching build definitions from Visual Studio Onli

2019-01-28 10:45发布

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 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 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.

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.

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?

2条回答
混吃等死
2楼-- · 2019-01-28 11:40

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

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()));
    }
}
查看更多
forever°为你锁心
3楼-- · 2019-01-28 11:44

I believe you are right I have just tested your code against my own VSO on a GIT project and I cannot retrieve any build.vNext builds, however I can retrieve old XAML build definitions.

I then tried using the REST API and it too could only return the old style XAML builds until I got it to use version 2 of the REST API and then the XAML and build.vNext style builds came back. Here is an example string I tried in my browser (make sure you are logged into VSO first to test it)

https://[vsoname].visualstudio.com/defaultcollection/[project]/_apis/build/definitions?api-version=2.0
查看更多
登录 后发表回答