首页 > 解决方案 > 如何使用 GitHub API 在 GitHub 组织中检索 1000 多个存储库的列表

问题描述

我正在使用以下脚本在我的一个 GitHub Enterprise 组织中生成一个存储库列表,它工作正常;但是,默认情况下,它一次只能获取 100 个 repos。

如何修改它以生成整个列表?我的 GitHub 组织中有大约 2000 个存储库。

curl --silent --user "myusername:mypassword" "https://github.***.com/api/v3/orgs/myorg/repos?page=1&per_page=2000" | npx jq '.[].clone_url' | while read repo
do
    repo="${repo%\"}"
    repo="${repo#\"}"
    echo "$repo"
done > repolist.txt

我无法在page=*&per_page=*这里调整,无论我使用什么数字组合,当我执行上面的 shell 脚本时,repolist.txt都会生成一个名为的文件,其中包含 GitHub org 中前 100 个 repos 的列表。

标签: shellgithubgithub-api

解决方案


来自文档

您可以指定要接收的物品数量(最多 100 个);

你可以去/orgs/{org}阅读public_repos

使用此值,您可以制作total_pages=$(($public_repos / 100 + 1))和迭代total_pages增加您的page道具。

下面是一个小代码,只需添加您的凭据和组织名称:

#!/bin/bash

user=""
password=""
org=""
public_repos=$(curl -s -u "${user}:${password}" "https://api.github.com/orgs/${org}" | jq .public_repos)
per_page=100
total_pages=$(($public_repos / $per_page + 1))

for page in $(seq 1 $total_pages); do
  curl -s -u "${user}:${password}"\
    "https://api.github.com/orgs/${org}/repos?page=${page}&per_page=${per_page}" | \
    jq -r '.[].clone_url'
done

推荐阅读