tRuEsAtM
tRuEsAtM

Reputation: 3668

Is there a REST API or any other way to get all branches for all repositories in VSTS?

I would like to have the list of all branches of all repositories under my VSTS account. I could find https://{account}.visualstudio.com/DefaultCollection/_apis/git/repositories?api-version=1.0. But, this does not list all branches under my repository, it only lists default branch.

I have code in C#,

var personalaccesstoken = "TOKEN";

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(
    new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
    Convert.ToBase64String(
        System.Text.ASCIIEncoding.ASCII.GetBytes(
            string.Format("{0}:{1}", "", personalaccesstoken))));

using (HttpResponseMessage response = client.GetAsync(
            "https://{account}.visualstudio.com/DefaultCollection/_apis/git/repositories?api-version=1.0").Result)
{
    response.EnsureSuccessStatusCode();
    string responseBody = await response.Content.ReadAsStringAsync();
    Console.WriteLine(responseBody);
}
Console.ReadLine();

Can anyone point towards the REST API or tell me a way to get all the branches under a repository?

Upvotes: 2

Views: 3592

Answers (2)

Marina Liu
Marina Liu

Reputation: 38096

You can use two loops to get all the branches for all git repositories in your VSTS account. Detail steps as below:

1. Get all the projects in your VSTS account

GET https://{account}.visualstudio.com/_apis/projects?api-version=4.1-preview.1

In the response will return all the projects in the VSTS account.

2. Loop for VSTS projects from step1 and get all the git repos of each VSTS project

GET https://{accountName}.visualstudio.com/{project}/_apis/git/repositories?api-version=4.1

In the reponse of the REST API you can all the git repos for each project.

3. Loop for git repos from step2 and get all the branches for each git repo

You can use list refs REST API to get all the branches for each git repo:

GET https://{accountName}.visualstudio.com/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=4.1

After executing all the loops, you will get all the branches for all git repos.

Upvotes: 5

milbrandt
milbrandt

Reputation: 1486

With Repositories - List you can get a list of all repos in your VSTS account.

Given that list you can iterate over all repos with Refs - List so that you should be able to get all refs and therefore information on branches.

A global API call to get all branches of all repos would also be difficult - branch names (refs) might be identical in different repos. One canoncial candidate for all repos is master branch.

Upvotes: 0

Related Questions