Skip to content

Instantly share code, notes, and snippets.

@dbeley
Last active December 27, 2022 20:20
Show Gist options
  • Save dbeley/e9061f734e32f914cc8c9fb8cd6b2548 to your computer and use it in GitHub Desktop.
Save dbeley/e9061f734e32f914cc8c9fb8cd6b2548 to your computer and use it in GitHub Desktop.
Archive all github repos of an user.
#!/usr/bin/env bash
set -eEu -o pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd -P)"
usage() { printf "%s" "\
Usage: ./archive_github_user.sh [-h] USER
"; exit 0;
}
if [ "$1" == "-h" ]; then
usage
fi
USER=$1
USER_DIR="$DIR/repos/$USER"
mkdir -p "$USER_DIR"
[ -f "$USER_DIR/$USER.txt" ] && printf "##### $USER_DIR/$USER.txt file detected.\nDelete it if you want to update the repository list.\n"
if [ ! -f "$USER_DIR/$USER.txt" ]; then
printf "##### Extracting repos for $USER.\n"
PAGE_NUMBER=`curl -sI "https://api.github.com/users/"$USER"/repos" | awk "/^link/" | cut -d? -f3`
for PAGE in `seq 1 ${PAGE_NUMBER//[!0-9]/}`; do
printf "##### Extracting starred repos from page $PAGE\n"
URL="https://api.github.com/users/$USER/repos?page=$PAGE"
curl -s "$URL" | jq -r '.[]|.html_url' >> "$USER_DIR/$USER.txt"
done
fi
for URL in `cat "$USER_DIR/$USER".txt`; do
REPO=`basename -s ".git" "$URL"`
FOLDER="$USER_DIR/$REPO"
if [ ! -d "$FOLDER" ] ; then
printf "##### Cloning $URL in $FOLDER.\n"
mkdir -p "$FOLDER"
git clone $URL "$FOLDER"
else
printf "##### Updating $URL in $FOLDER.\n"
cd "$FOLDER"
git pull $URL
cd "$DIR"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment