Last active
February 22, 2024 21:33
-
-
Save AlexAtkinson/b08037e721671ad15fa356d54f6d22e8 to your computer and use it in GitHub Desktop.
Get all Github repo URLs from an organization.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# get-gh-ssh-urls.sh | |
# ---------------------- | |
# Can be used to clone everything like: | |
# for i in $(./get-gh-ssh-urls.sh); do git clone $i ; done | |
# | |
# NOTE: The UI might say 600 repos, and the API only reflect 500... | |
# This is probably due to forks, which hasn't been implemented here. | |
# See: https://docs.github.com/en/rest/repos/forks?apiVersion=2022-11-28#list-forks | |
DIR_NAME="${PWD##*/}" | |
TYPE='users' # 'users' or 'orgs' | |
NAME='<name of user or org>' | |
# NAME="$DIR_NAME" # Use if your parent dir matches the git org or user exactly. | |
TOKEN='ghp_supersecret' | |
# See: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token | |
PER_PAGE=100 # MAX | |
# Get repo count | |
# Get repo count | |
#REPO_COUNT=$(( $(curl -sS --request GET \ | |
#--url "https://api.github.com/$TYPE/$ORG" \ | |
#--header "Authorization: Bearer $TOKEN" \ | |
#--header "X-GitHub-Api-Version: 2022-11-28" | \ | |
#jq -r '. | "\(.owned_private_repos) + \(.public_repos)"') )) | |
REPO_COUNT=$(curl -sS --request GET \ | |
--url "https://api.github.com/search/repositories?q=$TYPE:$NAME" \ | |
--header "Authorization: Bearer $TOKEN" \ | |
--header "X-GitHub-Api-Version: 2022-11-28" | jq -r .total_count) | |
# Figure out how many pages to call | |
PAGES="$(( REPO_COUNT / $PER_PAGE + 1 ))" | |
# Get the repo data, merge into one array, and output just ssh_urls. | |
COUNT=1 | |
while [[ $COUNT -le $PAGES ]]; do | |
curl -sS --request GET \ | |
--url "https://api.github.com/$TYPE/$NAME/repos?page=$COUNT&per_page=$PER_PAGE" \ | |
--header "Authorization: Bearer $TOKEN" \ | |
--header "X-GitHub-Api-Version: 2022-11-28" | |
((COUNT++)) | |
done | jq -s 'add' | jq -r '.[].ssh_url' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BONUS
You can use the list of repo URLs for a lot of useful automation. Just pipe it into repo_list.txt.
Clone new repos.
Pull all the repos.
Doesn't really need the above script, but useful if you have many repos.
Check for Deprecations
For example, save-state and set-output Github actions deprecations.