Visual Studio Team Services: Having a chat with the API

TL;DR;

Use the Visual Studio Team Services REST API to get data about your VSO environment. The code: https://github.com/nzthiago/ExtractVSOProjectInfo

Context

We recently hosted a large team of technology evangelists from around the world to an internal two day hackathon. Each team worked on their own Visual Studio Team Services (VSTS) project in a shared VSTS account.

I was tasked with extracting some information once it was over, which you might also be interested in for your own projects:

  • How many git commits were there from each team?
  • Who were the most prolific team members (i.e., most number of check ins)?
  • How many VSO builds did each team kick off?

For a lot of it I used the Visual Studio Team Services provider for Power BI which I will cover on a future post. But there were two specific items I did not manage to extract through Power BI:

  • Number of commits from team member including the ones that did not do any commits
  • Number of builds from each team

Approach and Setting up

I decided to write a command line app and go directly against the Visual Studio API, which is well documented on the Visual Studio Team Services REST API Referencepages. I had already used the API to create the 40 projects needed for the teams to use during the hackathon so this was a good option.

The Visual Studio Team Services REST API now supports OAuth and Basic authentication. When I started on this only Basic authentication was supported using alternate credentials, so that is the path I took with the code. This is the page with the documentation on both methods:

In app.settings I reference a separate settings file (so that credentials don’t get stored in source control or GitHub):

<appSettings file="..\..\..\AppSettingsSecrets.config">
</appSettings>

And the AppSettingsSecrets.configfile has the three pieces of information we need to connect to the REST API. You need to create this file with your specific credentials a folder above the project folder if running from Visual Studio:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
    <add key="user" value="<your VSO alternate login>" />
    <add key="password" value="<your VSO alternate password>" />
    <add key="url" value="<your VSO account>.visualstudio.com" />
</appSettings>

Working with the Rest API

I used the Google Chrome App Postman to test out the API as I went along. For example, here is Postman getting a list of builds for the Cirrus project:

For the two sets of data I was after I ended up calling the following API endpoints:

DescriptionURL
Base for all queries https://.visualstudio.com/DefaultCollection/
Get all projects in the VSO account_apis/projects?api-version=2.0
Get all the teams in a project_apis/projects/{Project Name}/teams?api-version=2.0
Get all the team members in a team_apis/projects/{Project Name}/teams/{Team Name}/members?api-version=2.0
Get all repositories_apis/git/repositories?api-version=2.0
Get all the repository commits_apis/git/repositories/{Repository ID}/commits?api-version=2.0
Get commit details_apis/git/repositories/{Repository ID}/commits/{Commit ID}?api-version=2.0
Get all the project builds{Project Name}/_apis/build/builds?api-version=2.0

Using the above it makes calls in parallel and works through it:

But more importantly, using the LINQtoCSV nuget package it creates two CSV files with the information I was after, which you can then open in Excel. For example, here’s MemberCommits.csv including team members with no commits after I changed the content to a table and added a pivot chart (random names and emails used to protect the innocent):

results.png

The Code

The code for this sample is on my GitHub account. There’s a list of improvements to be made. Pull requests and suggestions welcome!