Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c# - Fetching build definitions from Visual Studio Online through TFS API

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...