Getting build definitions from Visual Studio Online via the TFS API

I am having an issue retrieving build information from a Visual Studio Online project via the TFS API. I can connect to the project, presumably and find the expected collective project and team project, but even though the project has two build definitions and a number of builds, the TFS API always returns zero length arrays to me when requesting a definition build or build ... The project is a Git project. My production code is similar to this test code I wrote to try and 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. Build definitions definitely exist - I can connect to the project using Visual Studio and display them in the Builds tab in Team Explorer. This code has worked for me in the past, however, there have always been TFVC projects that I connected to, not a Git project. I wonder if I am using VersionControlServer

from a team project, for example:

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

      

.. is teamProjects

always a zero-length array.

Also, if I try to get all the assemblies for a team project by specifying a very general assembly detail specification, I don't get any built files.

More info: I am using VS Enterprise 2015 RC. I am a VSO project administrator and am building a collective project collection, team project and build definitions. I pushed some code and ran some builds. I connected via API through me. My credentials look correct.

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

+3


source to share


2 answers


You can use BuildHttpClient to make similar requests, as is the case with the REST 2.0 API.

This sample on GitHub is a pretty good resource, if you prefer to poll a single project it should look something 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()));
    }
}

      

+6


source


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

Then I tried using the REST API and it too was only able to revert the old XAML styles until I got it to use version 2 of the REST API and then the XAML style assemblies and build.vNext appeared. Here is an example of the line I tried in my browser (make sure you log into VSO first to check)



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

      

+2


source







All Articles