是否有 REST API 或任何其他方式来获取 VSTS 中所有存储库的所有分支?

Sam*_*eer 2 git azure-devops azure-devops-rest-api

我希望获得我的 VSTS 帐户下所有存储库的所有分支的列表。我能找到https://{account}.visualstudio.com/DefaultCollection/_apis/git/repositories?api-version=1.0。但是,这并没有列出我的存储库下的所有分支,它只列出了默认分支。

我有 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();
Run Code Online (Sandbox Code Playgroud)

谁能指出 REST API 或告诉我一种获取存储库下所有分支的方法?

Mar*_*Liu 5

您可以使用两个循环来获取 VSTS 帐户中所有 git 存储库的所有分支。详细步骤如下:

1.获取您的VSTS帐户中的所有项目

GET https://{account}.visualstudio.com/_apis/projects?api-version=4.1-preview.1
Run Code Online (Sandbox Code Playgroud)

在响应中将返回 VSTS 帐户中的所有项目。

2.从步骤1开始循环查找VSTS项目并获取每个VSTS项目的所有git repos

GET https://{accountName}.visualstudio.com/{project}/_apis/git/repositories?api-version=4.1
Run Code Online (Sandbox Code Playgroud)

在 REST API 的响应中,您可以获取每个项目的所有 git 存储库。

3.从步骤2循环获取git repos并获取每个git repo的所有分支

您可以使用list refs REST API 获取每个 git 存储库的所有分支:

GET https://{accountName}.visualstudio.com/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=4.1
Run Code Online (Sandbox Code Playgroud)

执行完所有循环后,您将获得所有 git 存储库的所有分支。