Skip to content

Instantly share code, notes, and snippets.

@snobear
Created October 26, 2021 14:44
Show Gist options
  • Save snobear/759a71f8dccec8f8b472ad8bed10f53d to your computer and use it in GitHub Desktop.
Save snobear/759a71f8dccec8f8b472ad8bed10f53d to your computer and use it in GitHub Desktop.
Clone all repos for a Github organization
# clone all public and private repos for a github org. user must have full access to repos.
USERNAME=someuser
ORGNAME=myorg
# personal access token
TOKEN=abc123tokenstringhere
# num repos to return per page - max 100
PERPAGE=100
BASEURL="https://api.github.com/orgs/${ORGNAME}/repos"
TOTALPAGES=`curl -I -i -u $USERNAME:$TOKEN -H "Accept: application/vnd.github.v3+json" -s ${BASEURL}\?per_page\=${PERPAGE} | grep -i link: 2>/dev/null|sed 's/link: //g'|awk -F',' -v ORS='\n' '{ for (i = 1; i <= NF; i++) print $i }'|grep -i last|awk '{print $1}' | tr -d '\<\>' | tr '\?\&' ' '|awk '{print $3}'| tr -d '=;page'`
i=1
repo_count=0
until [[ $i -gt $TOTALPAGES ]]; do
for repo_url in $(curl -s -u $USERNAME:$TOKEN -H 'Accept: application/vnd.github.v3+json' "${BASEURL}?per_page=${PERPAGE}&page=${i}" | jq -r '.[].ssh_url'); do
git clone $repo_url
((repo_count=$repo_count+1))
done
((i=$i+1))
done
echo "Total repos: ${repo_count}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment