Skip to content

Instantly share code, notes, and snippets.

@AlexAtkinson
Last active February 22, 2024 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexAtkinson/b08037e721671ad15fa356d54f6d22e8 to your computer and use it in GitHub Desktop.
Save AlexAtkinson/b08037e721671ad15fa356d54f6d22e8 to your computer and use it in GitHub Desktop.
Get all Github repo URLs from an organization.
#!/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'
@AlexAtkinson
Copy link
Author

AlexAtkinson commented May 17, 2023

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.

while read repo; do
  name=$(echo $repo | cut -d/ -f2 | cut -d. -f1) 
  test ! -d $name && git clone $repo
done <<< $(cat repo_list.txt)

Pull all the repos.

Doesn't really need the above script, but useful if you have many repos.

for dir in $(ls -1); do
  if [[ -d $dir ]]; then
    cd $dir
    if [[ $(git rev-parse --is-inside-work-tree) ]]; then
      git pull
      cd ../
    fi
  fi
done

Check for Deprecations

For example, save-state and set-output Github actions deprecations.

for i in $(ls -1); do
  rm -f dep-check-2000
  count=0
  if [[ -d $i/.github/workflows/ ]]; then
    grep -rli 'save-state\|set-output' $i/.github/workflows/ >> dep-check-2000
  fi
  if [[ -f dep-check-2000 ]]; then
    count=$(wc -l < dep-check-2000)
    [[ $count -gt 0 ]] && echo "DEPRECATION DETECTED: $i, in:"
    for i in $(cat dep-check-2000); do
      echo "  - $i"
    done
  fi
done >> cool-org-gh-actions-deprecations.txt
rm -f dep-check-2000
cat cool-org-gh-actions-deprecations.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment