获取我们所有私人仓库的清单 [英] get a list of all our private repos
问题描述
我想获取我们组织github中所有私有存储库的列表.我尝试过-
I want to get a list of all private repositories in our organisation github. I tried -
curl -u user:pw -X GET https://api.github.com/orgs/xxx/repos?per_page=100 >> list.txt
我发现 per_page 的最大值是100,如何将所有存储库保存到一个文件中?
I figure out that the per_page MAX value is 100 , how can I get all our repos into one file ?
谢谢
推荐答案
您可以使用bash脚本循环遍历每个页面,直到到达的最后一页为空.
You can use a bash script that loops over each page until the last page reached is empty.
可以使用 jq
JSON解析器来过滤私有存储库,您可以也排除分叉的仓库或您想要的任何东西.
The filter can be done with jq
JSON parser to filter private repository, you can also exclude forked repo or anything you want.
请注意,我正在使用个人访问令牌进行身份验证(以获取私有存储库) :
Note that I'm using a personal access token for the authentication (to get private repo) :
#!/bin/bash
# change those vars :
GITHUB_ORG=docker
GITHUB_ACCESS_TOKEN=12345666799897950400303332323
OUTPUT_FILE=repo_list.json
loop=0
index=1
TMP_FILE=tmpfile.txt
PER_PAGE=100
rm -f $TMP_FILE
echo "[]" > $OUTPUT_FILE
while [ "$loop" -ne 1 ]
do
data=`curl -s "https://api.github.com/orgs/$GITHUB_ORG/repos?access_token=$GITHUB_ACCESS_TOKEN&page=$index&per_page=$PER_PAGE"`
check_error=`echo "$data" | jq 'type!="array"'`
if [ "$check_error" == "true" ]; then
echo "access token is invalid"
exit 1
fi
filtered=`echo "$data" | jq '[ .[] | select(.private == true) ]'`
if [ "$filtered" == "[]" ]; then
loop=1
else
echo "$filtered" > $TMP_FILE
concat=`jq -s add $TMP_FILE $OUTPUT_FILE`
echo "$concat" > $OUTPUT_FILE
size=`jq '. | length' $OUTPUT_FILE`
echo "computed $index page - fetched total repo size of : $size"
index=$((index+1))
fi
done
如果只希望使用存储库URL数组而不是整个JSON对象,请替换:
If want to have only an array of repository URL instead of the whole JSON object, replace :
jq '[ .[] | select(.private == true) ]'
与:
jq '[ .[] | select(.private == true) | .html_url ]'
这篇关于获取我们所有私人仓库的清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!