Skip to content

Instantly share code, notes, and snippets.

@cristaloleg
Last active March 28, 2023 09:06
Show Gist options
  • Save cristaloleg/9242e988ad6768873150bd4f6d1f232f to your computer and use it in GitHub Desktop.
Save cristaloleg/9242e988ad6768873150bd4f6d1f232f to your computer and use it in GitHub Desktop.
Full Github organization clone
#!/bin/bash
# Usage: ./script NAME MAX-PAGE-NUMBER REGEX-PATTERN
#
# Also curl, jq and git are required.
#
# Author: https://gist.github.com/cristaloleg
#
# Based on https://gist.github.com/erdincay/4f1d2e092c50e78ae1ffa39d13fa404e
if [[ -z "${GITHUB_TOKEN}" ]]; then
echo "GITHUB_TOKEN env var is not set, get one from https://github.com/settings/tokens"
exit 1
else
GITHUB_TOKEN=${GITHUB_TOKEN}
fi
if [ -z "$1" ]; then
echo "waiting for the following arguments: name + max-page-number"
exit 1
else
name=$1
fi
if [ -z "$2" ]; then
max=2
else
max=$2
fi
if [ -z "$3" ]; then
pat=".*"
else
pat=$3
fi
ctx="orgs"
page=1
echo "Cloning: ${name}"
echo "Up to page: ${max}"
echo "Context: ${ctx}"
echo "With name pattern: ${pat}"
echo ""
until [ $page -gt $max ]
do
curl -H 'Authorization: token '${GITHUB_TOKEN} "https://api.github.com/$ctx/$name/repos?page=$page&per_page=100" | jq --arg PAT $pat '.[] | select(.name | test($PAT) ) | .ssh_url' | xargs -L1 git clone
page=$((page+1))
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment