我正在设置一个脚本,用于导出所有提交和pull请求,以获得更大的github存储库列表(大约4000个)。
在脚本的基本思想工作之后,我需要一种方法来循环通过存储库的所有提交页面。
我发现我可以导出每页100个提交。对于一些repos有一些更多的提交(如8000),所以这将是80页,我需要循环通过。
我找不到从github API中提取页面数量的方法。
到目前为止,我所做的是设置脚本,它循环所有提交并将它们导出到txt / csv文件。
我需要做的是在开始循环一个repo的提交之前知道总页数。
这里给了我一个我不能使用它的方式的页数。
curl -u "user:password" -I https://api.github.com/repos/0chain/rocksdb/commits?per_page=100
字符串
结果:
友情链接:https://api.github.com/repositories/152923130/commits?per_page=100&page=2; rel=“next”,https://api.github.com/repositories/152923130/commits?per_page=100&page=75 ; rel=“last”
我需要将值75(或其他存储库中的任何其他值)用作循环中的变量。
像这样:
repolist=`cat repolist.txt`
repolistarray=($(echo $repolist))
repolength=$(echo "${#repolistarray[@]}")
for (( i = 0; i <= $repolength; i++ )); do
#here i need to extract the pagenumber
pagenumber=$(curl -u "user:password" -I https://api.github.com/repos/$(echo "${repolistarray[i]}")/commits?per_page=100)
for (( n = 1; n <= $pagenumber; n++ )); do
curl -u "user:password" -s https://api.github.com/repos/$(echo "${repolistarray[i]}")/commits?per_page=100&page$(echo "$n") >committest.txt
done
done
done
型
我怎样才能得到“75”或任何其他结果了这一点
友情链接:https://api.github.com/repositories/152923130/commits?per_page=100&page=2; rel=“next”,https://api.github.com/repositories/152923130/commits?per_page=100&page=75 ; rel=“last”
被用作n
3条答案
按热度按时间balp4ylt1#
以下是@Poshi评论的内容:循环无限期地请求下一个页面,直到遇到空页面,然后跳出内部循环,继续下一个repo。
字符串
7gs2gvoe2#
好吧,你要求的方法不是最常见的方法,通常它是通过获取页面直到没有更多的数据可用。但要回答您的具体问题,我们必须解析包含信息的行。一个快速而肮脏的方法可以做到这一点:
字符串
还有一些其他的方法可以做到这一点,用更少的命令,也许更简单,但这一个允许我一步一步地解释。这些其他方式之一可以是:
型
这一个做了一些假设,比如
page
总是最后一个参数。gcxthw6b3#
官方的GitHub CLI(
gh
)支持--paginate
标志,它可以为您完成繁重的工作。结合jq
,您可以得到您正在寻找的答案。这更简单,而且应该比之前发布的其他Bash解决方案更健壮。
示例
过去90天内的提交总数:
字符串
过去6个月的提交数量,按月细分,以CSV格式显示:
型
输出量:
型