Bash 如何提取github存储库的提交页面总数

Bash 如何提取github存储库的提交页面总数,bash,shell,github,github-api,git-bash,Bash,Shell,Github,Github Api,Git Bash,我正在设置一个脚本,用于导出github存储库(大约4000个)的所有提交和拉取请求 在脚本的基本思想起作用之后,我需要一种方法来循环一个存储库的所有提交页面 我发现我可以导出每页100次提交。对于一些回购协议,还有一些提交(比如8000),因此我需要循环浏览80页 我找不到从GithubAPI中提取页面数的方法 到目前为止,我所做的是设置脚本,使其在所有提交中循环并将它们导出到txt/csv文件 我需要做的是,在我开始循环提交回购协议之前,知道总页数 这给了我一个我无法使用的页数 curl -

我正在设置一个脚本,用于导出github存储库(大约4000个)的所有提交和拉取请求

在脚本的基本思想起作用之后,我需要一种方法来循环一个存储库的所有提交页面

我发现我可以导出每页100次提交。对于一些回购协议,还有一些提交(比如8000),因此我需要循环浏览80页

我找不到从GithubAPI中提取页面数的方法

到目前为止,我所做的是设置脚本,使其在所有提交中循环并将它们导出到txt/csv文件

我需要做的是,在我开始循环提交回购协议之前,知道总页数

这给了我一个我无法使用的页数

curl -u "user:password" -I https://api.github.com/repos/0chain/rocksdb/commits?per_page=100
结果:

链接:;rel=“下一步”;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
repolist=`cat repolist.txt`
repolistarray=($(echo$repolist))
repolistLength=$(回显“${#repolistarray[@]}”)

对于((i=0;i这里有一些类似@Poshi评论的东西:循环无限期地请求下一页,直到你碰到一个空页,然后打破内部循环,进入下一个回购

# this is the contents of a page past the last real page:
emptypage='[

]'

# here's a simpler way to iterate over each repo than using a bash array
cat repolist.txt | while read -d' ' repo; do

  # loop indefinitely
  page=0
  while true; do
    page=$((page + 1))

    # minor improvement: use a variable, not a file.
    # also, you don't need to echo variables, just use them
    result=$(curl -u "user:password" -s \ 
      "https://api.github.com/repos/$repo/commits?per_page=100&page=$n")

    # if the result is empty, break out of the inner loop
    [ "$result" = "$emptypage" ] && break

    echo "$result" > committest.txt
    # note that > overwrites (whereas >> appends),
    # so committest.txt will be overwritten with each new page.
    #
    # in the final version, you probably want to process the results here,
    # and then
    #
    #       echo "$processed_results"
    #     done > repo1.txt
    #   done
    #
    # to ouput once per repo, or
    #
    #       echo "$processed_results"
    #     done
    #   done > all_results.txt
    #
    # to output all results to a single file

  done
done

嗯,您要求的方法不是最常见的方法,通常是通过获取页面直到没有更多数据可用。但要回答您的特定问题,我们必须解析包含信息的行。快速而肮脏的方法可以是:

response="Link: 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\""

<<< "$response" cut -f2- -d: | # First, get the contents of "Link": everything after the first colon
tr "," $'\n' |      # Separate the different parts in different lines
grep 'rel="last"' | # Select the line with last page information
cut -f1 -d';' |     # Keep only the URL
tr "?&" $'\n' |     # Split URL and its parameters, one per line
grep -e "^page" |   # Select the "page" parameter
cut -f2 -d=         # Finally, extract the number we are interested in
<<< "$response" sed 's/.*&page=\(.*\); rel="last".*/\1/'
response=“链接:https://api.github.com/repositories/152923130/commits?per_page=100&page=2;rel=\“下一步”,https://api.github.com/repositories/152923130/commits?per_page=100&page=75;rel=\“最后一次”

你不需要那个号码。在第一页有“下一个”链接时,跟着它,循环到最后,“下一个”链接是空的。@Poshi我对这一切都很陌生。你有什么关于如何跟随“下一个”的提示吗在特定情况下链接?我不需要一个完整的解决方案,只需要一个关于如何链接的想法。谢谢你的回答为什么使用github?使用git会容易得多。git本身非常容易编写。嘿,webb,首先感谢你的评论。我得到了你所有的观点,代码中的评论非常有用。只有我的问题是,如果我像你发布的那样使用它,它将不会输出任何文件。我感觉我的TXT文件可能有问题。它是一个用破折号分隔的字符串。例如:``Zolmeister/treemix Zolmeister/truffle core Zolmeister/Tuxman Zolmeister/udio Zolmeister/UltimateICOCalendar Zolmeister/varnish dashboard``什么时候我执行脚本,它启动了,但没有真正输出任何东西。到目前为止,ok-short调试更新。for循环出现了一些问题。其他一切工作都非常出色。不知何故,它没有像应该的那样添加页面。另一个short调试更新:不知何故,它没有接受“true”在for loopHey中有一个值,因此我最终的快速而肮脏的解决方案是将“true”替换为“nhi adonis.正如您所指出的,
for((…;true;…)
不起作用,所以我用一个while循环代替了它。我还将
while read
改为
while read-d'
来打断空格(或换行符)上的输入数据,而不是只打断换行符。嘿,Poshi,非常感谢。不过我使用了webbs解决方案,因为我的代码看起来更干净了(谁会猜到呢)不过我记下了你所有的笔记。谢谢。是的,我知道:这是解决这个问题最常用的方法,因为它更简单、更干净。这是我在最初的评论中说的。无论如何,你的问题是不同的:如何提取你要找的号码,所以我的答案是:-)可能更简单一点:
grep-o'[0-9]\+;rel=“last”或
echo“$response”| perl-ne'/([0-9]+);rel=“last”/;print“$1\n”;