GitHub API - 在不列出其所有分支的情况下获取 repo 的分支数
GitHub API - Get the number of branches of a repo without listing all its branches
我目前正在使用 GitHub API v3.
编写一个小项目
我一直在根据回购协议包含的分支数进行计算。如果不请求 list all the branches of that repo,我似乎无法找到这样做的方法。需要请求 repo 的分支会增加不必要的 运行 时间,尤其是在处理数百个 repo 时,每个 repo 包含数十个分支。
显然缺乏这样做的能力让我有点吃惊,因为非常相似的操作,获取组织的回购数量,通过这样做很容易获得:
- Get an organization. e.g.
GET https://api.github.com/orgs/cloudify-cosmo
, using proper GitHub authentication credentials.
- 假设身份验证成功,在响应正文中有两个字段名为
public_repos
和 total_private_repos
- 要获取回购数量,只需将这两个字段的值相加即可。
那么,我是不是漏掉了什么?是否有一种类似方便的方式(或任何方式)获取回购的分支数量而不必列出其分支?
目前没有这样的属性。
但是,您可以使用一个巧妙的技巧来避免获取所有页面。如果您将 per_page
设置为 1
,则每页将包含 1 个项目,页数(由最后一页显示)也会告诉您项目总数:
https://developer.github.com/v3/#pagination
因此,只需一个请求 -- 您就可以获得分支总数。例如,如果您获取此 URL 并检查 Link header:
https://api.github.com/repos/github/linguist/branches?per_page=1
然后您会注意到 Link
header 是:
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。
希望这对您有所帮助。
您还可以使用 GraphQL API v4 轻松获取分支数:
{
repository(owner: "google", name: "gson") {
refs(first: 0, refPrefix: "refs/heads/") {
totalCount
}
}
}
给出:
{
"data": {
"repository": {
"refs": {
"totalCount": 13
}
}
}
}
当您在多个存储库上执行此操作时,使用 GraphQL 也更直接,因为您可以为每个存储库构建具有不同 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
}
}
我根据 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"
}
}```
我目前正在使用 GitHub API v3.
编写一个小项目我一直在根据回购协议包含的分支数进行计算。如果不请求 list all the branches of that repo,我似乎无法找到这样做的方法。需要请求 repo 的分支会增加不必要的 运行 时间,尤其是在处理数百个 repo 时,每个 repo 包含数十个分支。
显然缺乏这样做的能力让我有点吃惊,因为非常相似的操作,获取组织的回购数量,通过这样做很容易获得:
- Get an organization. e.g.
GET https://api.github.com/orgs/cloudify-cosmo
, using proper GitHub authentication credentials. - 假设身份验证成功,在响应正文中有两个字段名为
public_repos
和total_private_repos
- 要获取回购数量,只需将这两个字段的值相加即可。
那么,我是不是漏掉了什么?是否有一种类似方便的方式(或任何方式)获取回购的分支数量而不必列出其分支?
目前没有这样的属性。
但是,您可以使用一个巧妙的技巧来避免获取所有页面。如果您将 per_page
设置为 1
,则每页将包含 1 个项目,页数(由最后一页显示)也会告诉您项目总数:
https://developer.github.com/v3/#pagination
因此,只需一个请求 -- 您就可以获得分支总数。例如,如果您获取此 URL 并检查 Link header:
https://api.github.com/repos/github/linguist/branches?per_page=1
然后您会注意到 Link
header 是:
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。
希望这对您有所帮助。
您还可以使用 GraphQL API v4 轻松获取分支数:
{
repository(owner: "google", name: "gson") {
refs(first: 0, refPrefix: "refs/heads/") {
totalCount
}
}
}
给出:
{
"data": {
"repository": {
"refs": {
"totalCount": 13
}
}
}
}
当您在多个存储库上执行此操作时,使用 GraphQL 也更直接,因为您可以为每个存储库构建具有不同 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
}
}
我根据 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"
}
}```