2015-10-20 70 views
6

我目前正在使用GitHub API v3编写一个小项目。GitHub API - 获取没有列出所有分支的回购分支数量

我经常根据回购包含的分支数量进行计算。我似乎无法找到这样做的方式,也没有要求list all the branches of that repo。需要回购分行增加不必要的运行时间,特别是在处理数百个回购时,每个分行都包含数十个分支。

的明显缺乏这样做的一个小惊喜抓住了我,因为相当类似的操作,得到组织的回购数量的能力,是通过这样做容易获得:

  1. Get an organization。例如GET https://api.github.com/orgs/cloudify-cosmo,正确使用GitHub authentication credentials
  2. 假定身份验证成功,会在回应主体有两个字段名为public_repostotal_private_repos
  3. 要获得回购的数量,只需添加这两个字段的值。

那么,我错过了什么吗?是否有类似方便的方式(或者任何方式)获得回购分支机构的数量而不必列出分支机构?

+0

'GET /回购/:业主/:回购/​​ branches'返回数组,可你不只是使用的长度是多少? – jready

+0

我可以这样做,但是,正如我指定的那样,要求我列出该回购的所有分支。我希望获得分支机构数量,而不必要求获取exrta信息,就像仅使用GET/orgs /:org字段获取每个组织的回购数量一样,而不必处理回购请求,例如'GET/orgs /:org/repos' – aviyoop

回答

6

目前没有这样的属性。

但是,有一个巧妙的技巧可以避免抓取所有页面。如果设置per_page1,则每个页面包含1项和页面(由最后一个页面显示)的数量还会告诉你的项目总数:

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

所以,只有一个请求 - 您可以获得分支机构的总数。例如,如果您抓取该网址,检查链接标题:

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.

希望这会有所帮助。

0

您还可以使用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 
     } 
    } 
    } 
} 

当你在多台回购这样做,它也更简单与GraphQL,因为你可以建立不同的查询aliases每个回购&使用只有一个r eQUEST的获得枝数为所有这些:

{ 
    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

相关问题