GitHub API - 获取存储库的分支数量而不列出所有分支

7

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

我不断地根据存储库包含的分支数量进行计算。我似乎找不到一种不请求列出该存储库的所有分支的方法来实现这一点。请求该存储库的分支会增加不必要的运行时间,特别是在处理数百个存储库时,每个存储库包含数十个分支。

显然缺乏这样做的能力让我有些惊讶,因为相当类似的操作,获取组织的存储库数量,可以通过以下方式轻松获得:

  1. 获取组织。例如,GET https://api.github.com/orgs/cloudify-cosmo,使用适当的GitHub身份验证凭据
  2. 假设身份验证成功,在响应正文中有两个名为public_repostotal_private_repos的字段
  3. 要获取存储库的数量,只需将这两个字段的值相加即可。

那么,我是否遗漏了什么?是否有一种类似方便的方式(或任何方式)可以获取存储库的分支数,而无需列出其分支?


GET /repos/:owner/:repo/branches 返回一个数组,你不能直接使用它的长度吗? - jaredready
我可以做到,但是正如我所指定的那样,这需要我列出该存储库的所有分支。我想要获取分支数量,而不必请求额外的信息,就像只使用“GET /orgs/:org”字段获取每个组织的存储库数量一样,而不必处理存储库请求,例如“GET /orgs/:org/repos”。 - Avia Efrat
3个回答

10

目前没有这样的属性。

但是,有一个巧妙的技巧可以避免获取所有页面。如果将 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。
希望这能帮到你。

4
你也可以使用GraphQL API v4轻松地获取分支计数:
{
  repository(owner: "google", name: "gson") {
    refs(first: 0, refPrefix: "refs/heads/") {
      totalCount
    }
  }
}

在浏览器中尝试

得到:

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

由于您正在多个仓库上执行此操作,使用GraphQL更加直接,因为您可以针对每个存储库构建具有不同别名的查询,并仅使用一个请求获取所有这些存储库的分支计数。
{
  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
  }
}

在浏览器中尝试


在私有存储库上,我在使用此查询时遇到了问题,直到我给予我的“个人访问令牌”完整的存储库访问权限。如果您返回计数为0并且您有分支,请检查您的权限。 - TheLukeMcCarthy

0

我基于Avia的答案创建了一个简单的PowerShell实用程序,希望它能帮助未来的某个人。虽然它是在PowerShell中编写的,但也可以在任何其他语言中采用。


#$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"
    }

}```

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接