gpt4 book ai didi

github - 如何通过 GitHub API 获取存储库的分支数?

转载 作者:行者123 更新时间:2023-12-05 02:59:46 25 4
gpt4 key购买 nike

我是 working on a UI其中列出了给定用户或组织的所有存储库。这是使用树格式,其中第一级是存储库,第二级层次结构(子节点)将是每个分支,如果展开的话。

我使用的机制故意不要求我提取给定存储库的所有分支的列表,因为 API 对 API 调用有速率限制。相反,我所要做的就是指示它包含多少个子节点,而不实际为它们分配值(直到用户展开它的那一刻)。我几乎可以肯定获取 repo 列表的结果中包含分支计数,但令我失望的是,我没有看到它。我只能看到 forks、stargazers、watchers、issues 等的计数。除了分支计数之外的所有内容。

UI 的意图是它会提前知道填充子节点的分支数,但直到用户展开父节点后才真正获取它们 - 因此会立即显示 < strong>每个分支的空占位符,然后异步加载要填充的实际分支。同样,因为我需要避免过多的 API 调用。当用户滚动时,它将使用分页仅获取需要向用户显示的页面,并将其缓存以供以后显示。

具体来说,我正在使用 Delphi 的 Virtual TreeView:

procedure TfrmMain.LstInitChildren(Sender: TBaseVirtualTree; Node: PVirtualNode;
var ChildCount: Cardinal);
var
L: Integer;
R: TGitHubRepo;
begin
L:= Lst.GetNodeLevel(Node);
case L of
0: begin
//TODO: Return number of branches...
R:= TGitHubRepo(Lst.GetNodeData(Node));
ChildCount:= R.I['branch_count']; //TODO: There is no such thing!!!
end;
1: ChildCount:= 0; //Branches have no further child nodes
end;
end;

我是否遗漏了一些可以让我无需预先获取所有分支的完整列表即可获得 repo 分支计数的东西?

最佳答案

您可以使用新的 GraphQL API反而。这使您可以根据需要定制查询和结果。您可以在一个查询中完成这两项操作,而不是先获取计数然后再填充分支。

试用 Query Explorer .

query {
repository(owner: "octocat", name: "Hello-World") {
refs(first: 100, refPrefix:"refs/heads/") {
totalCount
nodes {
name
}
},
pullRequests(states:[OPEN]) {
totalCount
}
}
}
{
"data": {
"repository": {
"refs": {
"totalCount": 3,
"nodes": [
{
"name": "master"
},
{
"name": "octocat-patch-1"
},
{
"name": "test"
}
]
},
"pullRequests": {
"totalCount": 192
}
}
}
}

Pagination是用游标完成的。首先你会得到第一页,一次最多 100 页,但为了简洁起见,我们在这里只使用 2 页。响应将包含一个唯一的游标。

{
repository(owner: "octocat", name: "Hello-World") {
pullRequests(first:2, states: [OPEN]) {
edges {
node {
title
}
cursor
}
}
}
}
{
"data": {
"repository": {
"pullRequests": {
"edges": [
{
"node": {
"title": "Update README"
},
"cursor": "Y3Vyc29yOnYyOpHOABRYHg=="
},
{
"node": {
"title": "Just a pull request test"
},
"cursor": "Y3Vyc29yOnYyOpHOABR2bQ=="
}
]
}
}
}
}

然后您可以在光标后请求更多元素。这将获取接下来的 2 个元素。

{
repository(owner: "octocat", name: "Hello-World") {
pullRequests(first:2, after: "Y3Vyc29yOnYyOpHOABR2bQ==", states: [OPEN]) {
edges {
node {
title
}
cursor
}
}
}
}

Queries can be written like functions and passed arguments .参数以单独的 JSON 位发送。这允许查询是一个简单的不变字符串。

这个查询和以前做同样的事情。

query NextPullRequestPage($pullRequestCursor:String) {
repository(owner: "octocat", name: "Hello-World") {
pullRequests(first:2, after: $pullRequestCursor, states: [OPEN]) {
edges {
node {
title
}
cursor
}
}
}
}

{
"pullRequestCursor": "Y3Vyc29yOnYyOpHOABR2bQ=="
}

{ "pullRequestCursor": null } 将获取第一页。


它的 rate limit calculations比 REST API 更复杂。不是每小时通话,而是每小时 5000 积分。每个查询都会花费一定数量的点,这大致对应于 Github 计算结果的成本。您可以通过查询查询的 rateLimit 来了解查询的费用。信息。如果你传递给它 dryRun: true 它只会告诉你成本而不运行查询。

{
rateLimit(dryRun:true) {
limit
cost
remaining
resetAt
}
repository(owner: "octocat", name: "Hello-World") {
refs(first: 100, refPrefix: "refs/heads/") {
totalCount
nodes {
name
}
}
pullRequests(states: [OPEN]) {
totalCount
}
}
}
{
"data": {
"rateLimit": {
"limit": 5000,
"cost": 1,
"remaining": 4979,
"resetAt": "2019-08-21T05:13:56Z"
}
}
}

此查询仅花费一分。我还剩 4979 分,我会在世界标准时间 05:13 重置我的速率限制。

GraphQL API 非常灵活。您应该能够使用更少的 Github 资源和更少的编程来解决速率限制,从而用它做更多的事情。

关于github - 如何通过 GitHub API 获取存储库的分支数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57583064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com