Last active
March 31, 2021 04:29
-
-
Save victornpb/cd51b35836f442dfeebee0e2aa13cc2c to your computer and use it in GitHub Desktop.
Clone All repositories from User or Organization
This file contains hidden or 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
#!/bin/bash | |
echo "-------------------------------" | |
echo " Clone all GitHub Repositories " | |
echo "-------------------------------" | |
#prompt for variables | |
printf "\nCreate a token here https://github.com/settings/tokens\nor leave it empty for public repositories\n\n" | |
read -s -p 'Personal Token (optional): ' GITHUB_TOKEN | |
printf "\n" | |
if [ -n "$GITHUB_TOKEN" ]; then | |
AUTH="-u :$GITHUB_TOKEN" | |
VISIBILITY='all' | |
else | |
AUTH="" | |
VISIBILITY='public' | |
fi | |
read -p 'Type of account <user|org>: ' ACCOUNT_TYPE | |
if [[ $ACCOUNT_TYPE == 'user' ]] ; then | |
API_URL="https://api.github.com/users/$GITHUB_USERNAME/repos" | |
elif [[ $ACCOUNT_TYPE == 'org' ]] ; then | |
API_URL="https://api.github.com/orgs/$GITHUB_USERNAME/repos" | |
else | |
echo 'Invalid answer! Type "org" or "user"'; exit 1 | |
fi | |
read -p "GitHub ${ACCOUNT_TYPE}: " GITHUB_USERNAME | |
if [ -z "$GITHUB_USERNAME" ]; then | |
echo 'Please specify the GitHub username!'; exit 1 | |
fi | |
read -p 'Protocol <ssh|http>: ' PROTOCOL | |
if [[ $PROTOCOL == 'ssh' ]] ; then | |
REPO_PROP=".ssh_url" | |
elif [[ $PROTOCOL == 'http' ]] ; then | |
REPO_PROP='.clone_url' | |
else | |
echo 'Invalid answer! Type "ssh" or "http"'; exit 1 | |
fi | |
# List repositories | |
REPOS_FILE="${VISIBILITY}_repositories_of_${ACCOUNT_TYPE}_${GITHUB_USERNAME}.txt" | |
printf "\n\nListing repos from $ACCOUNT_TYPE: $GITHUB_USERNAME ...\n" | |
printf "" > $REPOS_FILE | |
TOTAL=0 | |
PER_PAGE=100 | |
for ((PAGE=1; ; PAGE+=1)); do | |
COUNT_ITEMS_IN_PAGE=0 | |
while read REPO ; do | |
((COUNT_ITEMS_IN_PAGE++)) | |
((TOTAL++)) | |
echo "$REPO" | |
echo "$REPO" >> $REPOS_FILE | |
done < <(curl $AUTH -s "$API_URL?per_page=$PER_PAGE&page=$PAGE" | jq -r ".[]|$REPO_PROP") | |
if [ $COUNT_ITEMS_IN_PAGE -ne $PER_PAGE ] ; then break ; fi | |
done | |
# confimation | |
printf "\n\nListed $TOTAL repositories in '$REPOS_FILE'.\n(You can edit the file before continuing)\n\n" | |
read -p "Press [enter] to start" | |
# Clone Repositories | |
printf "\n\nCloning repos from $REPOS_FILE ...\n" | |
I=0 | |
TOTAL=$(grep -c . $REPOS_FILE) | |
ERROR=0 | |
while IFS="" read -r p || [ -n "$p" ] | |
do | |
((I++)) | |
printf "\n[%2d/%2d] Cloning... %s " "$I" "$TOTAL" "$p" | |
git clone $p >/dev/null 2>&1 || | |
{ printf "\tFAILED!" ; ((ERROR++)) ; continue ; } | |
printf "\tDONE!" | |
done < $REPOS_FILE | |
printf "\n\nFINISHED!\n" | |
if [ $ERROR -gt 0 ] ; then exit 1 ; fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment