【问题标题】:GitHub API - Get the number of branches of a repo without listing all its branchesGitHub API - 在不列出所有分支的情况下获取 repo 的分支数
【发布时间】:2020-07-30 15:45:37
【问题描述】:

我目前正在使用 GitHub API v3 编写一个小项目。

我经常根据回购包含的分支数量进行计算。如果不请求list all the branches of that repo,我似乎找不到这样做的方法。请求 repo 的分支会增加不必要的运行时间,尤其是在处理数百个 repo 时,每个 repo 包含数十个分支。

明显缺乏这样做的能力让我有点意外,因为通过这样做很容易获得一个非常相似的操作,即获取组织的回购数量:

  1. Get an organization。例如GET https://api.github.com/orgs/cloudify-cosmo,使用正确的GitHub authentication credentials
  2. 假设认证成功,在响应正文中有两个字段名为public_repostotal_private_repos
  3. 要获取repos的数量,只需将这两个字段的值相加即可。

那么,我错过了什么吗?是否有类似方便的方式(或任何方式)无需列出其分支即可获取 repo 的分支数量?

【问题讨论】:

  • GET /repos/:owner/:repo/branches 返回一个数组,不能只用它的长度吗?
  • 我可以这样做,但正如我指定的那样,这需要我列出该 repo 的所有分支。我想获得分支机构的数量,而无需询问 exrta 信息,就像仅使用 GET /orgs/:org 的字段获得每个组织的回购数量一样,而不必处理回购请求,例如GET /orgs/:org/repos

标签: github-api


【解决方案1】:

目前没有这个属性。

但是,您可以使用一个巧妙的技巧来避免获取所有页面。如果您将per_page 设置为1,则每页将包含 1 个项目,并且页数(由最后一页显示)也会告诉您项目总数:

https://developer.github.com/v3/#pagination

因此,只需一个请求 - 您就可以获得分支的总数。例如,如果您获取此 URL 并检查 Link 标头:

https://api.github.com/repos/github/linguist/branches?per_page=1

然后你会注意到Link 标头是:

Link: <https://api.github.com/repositories/1725199/branches?per_page=1&page=2>; rel="next", <https://api.github.com/repositories/1725199/branches?per_page=1&page=28>; rel="last"

这告诉您有 28 页结果,并且因为每页有一项 - 分支总数为 28。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您也可以使用GraphQL API v4 轻松获取分支数:

    {
      repository(owner: "google", name: "gson") {
        refs(first: 0, refPrefix: "refs/heads/") {
          totalCount
        }
      }
    }
    

    Try it in the explorer

    给出:

    {
      "data": {
        "repository": {
          "refs": {
            "totalCount": 13
          }
        }
      }
    }
    

    当您在多个 repo 上执行此操作时,使用 GraphQL 也更加简单,因为您可以为每个 repo 使用不同的 aliases 构建查询,并且只使用一个请求来获取所有这些的分支计数:

    {
      fetch: repository(owner: "github", name: "fetch") {
        ...RepoFragment
      }
      hub: repository(owner: "github", name: "hub") {
        ...RepoFragment
      }
      scientist: repository(owner: "github", name: "scientist") {
        ...RepoFragment
      }
    }
    
    fragment RepoFragment on Repository {
      refs(first: 0, refPrefix: "refs/heads/") {
        totalCount
      }
    }
    

    Try it in the explorer

    【讨论】:

    • 在我给我的“个人访问令牌”完全回购访问权限之前,我在私人回购上的这个查询遇到了麻烦。如果您返回的计数为 0 并且您有分支机构检查您的权限
    【解决方案3】:

    我根据 Avia 的回答创建了一个简单的 power shell 实用程序。希望它将来对某人有所帮助。虽然它在 powershel 中,但也可以在任何其他语言中采用。

    
    #$response = Invoke-WebRequest  -Uri https://api.github.com/repos/github/linguist/branches?per_page=1
    
    $response = Invoke-WebRequest  -Uri https://api.github.com/repos/angular/angular/branches?per_page=1
    
    #https://github.com/angular/angular
    
    $numberOfBranchesString = $response.Headers.link.Split(";")[1].Split(",")[1].Split("&")[1].Split("=")[1];
    
    $numberOfBranches = $numberOfBranchesString.Substring(0, $numberOfBranchesString.length-1);
    
    $numBranch = $numberOfBranches -as [int];
    
    $loopToRunBranch = $numBranch/10 + 1
    
    
    $remainingBranches = $numBranch%10;
    
    for($i=0; $i -lt $loopToRunBranch; $i++) {
        
        if($i -lt $loopToRunBranch -1 ) {
        #    $response = Invoke-WebRequest  -Uri "https://api.github.com/repos/github/linguist/branches?per_page=10"
            $response = Invoke-WebRequest  -Uri "https://api.github.com/repos/angular/angular/branches?page=$($i+1)&per_page=10"
        
            echo $response
        }
        else {
            $remainingBranches = $numberOfBranches%10
        #    $response = Invoke-WebRequest  -Uri "https://api.github.com/repos/github/linguist/branches?per_page=$remainingBranches"
            $response = Invoke-WebRequest  -Uri "https://api.github.com/repos/angular/angular/branches?page=$($i+1)&per_page=$remainingBranches"
        
            echo "$i from Else $response"
        }
    
    }```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 2021-11-23
      • 2014-11-02
      • 2013-12-18
      • 2019-09-28
      • 1970-01-01
      相关资源
      最近更新 更多