【问题标题】:Is there a REST API or any other way to get all branches for all repositories in VSTS?是否有 REST API 或任何其他方式来获取 VSTS 中所有存储库的所有分支?
【发布时间】:2018-07-19 18:30:50
【问题描述】:

我想获得我的 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();

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

【问题讨论】:

  • 你想获取给定 repo 的所有分支吗? REST API 列表 refs 可以返回给定 git repo 的所有分支和标签。
  • 不是给定的仓库,我需要所有仓库的分支信息。
  • 我在回答中添加了获取git repos所有分支的方法,你可以试试。

标签: git azure-devops azure-devops-rest-api


【解决方案1】:

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

1。 Get all the projects 在您的 VSTS 帐户中

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

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

2。从 step1 和每个 VSTS 项目的get all the git repos 循环 VSTS 项目

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

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

3。从 step2 循环 for git repos 并获取每个 git repo 的所有分支

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

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

执行完所有循环后,你会得到所有 git repos 的所有分支。

【讨论】:

    【解决方案2】:

    使用Repositories - List,您可以获得您的 VSTS 帐户中所有 repo 的列表。

    鉴于该列表,您可以使用 Refs - List 遍历所有 repos,以便您应该能够获取所有 refs 以及分支上的信息。

    获取所有存储库的所有分支的全局 API 调用也很困难 - 不同存储库中的分支名称 (refs) 可能相同。所有 repos 的一个规范候选者是 master 分支。

    【讨论】:

    • 看起来它没有提供所有存储库的分支信息。我需要选择一个存储库,然后它会给我有关与该存储库关联的所有参考的信息。
    • @Sameer 很抱歉回复了您代码下方的问题。我已经更新了我的答案,希望对你有所帮助。
    猜你喜欢
    • 2019-01-27
    • 2023-03-20
    • 2014-09-15
    • 2016-12-04
    • 2019-08-05
    • 2020-08-21
    • 2011-02-10
    • 2020-03-16
    • 2015-05-23
    相关资源
    最近更新 更多