Skip to content

Instantly share code, notes, and snippets.

@tmiland
Forked from clrung/clone_all_repos.sh
Last active November 14, 2022 20:58
Show Gist options
  • Save tmiland/eb8247df9d7b89b2aabadac4633b6ff7 to your computer and use it in GitHub Desktop.
Save tmiland/eb8247df9d7b89b2aabadac4633b6ff7 to your computer and use it in GitHub Desktop.
Clones all repos in a GitHub organization
#!/bin/bash
# Usage: clone_all_repos.sh [organization] <output directory>
ORG=$1
PER_PAGE=100
GIT_OUTPUT_DIRECTORY=${2:-"/tmp/${ORG}_repos"}
if [ -z "$GITHUB_TOKEN" ]; then
echo -e "Variable GITHUB_TOKEN isn't set! Please specify your GitHub token.\n\nMore info: https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/"
exit 1
fi
if [ -z "$ORG" ]; then
echo "Variable ORG isn't set! Please specify the GitHub organization."
exit 1
fi
mkdir -p $GIT_OUTPUT_DIRECTORY
echo "Cloning repos in $ORG to $GIT_OUTPUT_DIRECTORY/..."
for ((PAGE=1; ; PAGE+=1)); do
REPO_COUNT=0
ERROR=0
while read REPO_NAME ; do
((REPO_COUNT++))
echo -n "Cloning $REPO_NAME to $GIT_OUTPUT_DIRECTORY/$REPONAME... "
git clone https://github.com/$ORG/$REPO_NAME.git $GIT_OUTPUT_DIRECTORY/$REPO_NAME >/dev/null 2>&1 ||
{ echo -e "ERROR: Unable to clone!" ; ERROR=1 ; continue ; }
echo "done"
done < <(curl -u :$GITHUB_TOKEN -s "https://api.github.com/orgs/$ORG/repos?per_page=$PER_PAGE&page=$PAGE" | jq -r ".[]|.name")
if [ $ERROR -eq 1 ] ; then exit 1 ; fi
if [ $REPO_COUNT -ne $PER_PAGE ] ; then exit 0 ; fi
done
#!/usr/bin/env bash
# gh repo fork --clone=false atom/flight-manual.atom.io --org atomeditor-io
# Usage: fork-all-repos.sh [source organization] [destinsation organization]
# Source: https://gist.github.com/clrung/75459a9fe954313c57f69d6cdfd502ec#file-clone_all_repos-sh
# Modified by Tommy Miland - Nov 13 2022
# Use gh instead: https://cli.github.com/manual/gh_repo_fork
ORG="$1"
DEST_ORG="$2"
COUNT=100
for ((PAGE=1; ; PAGE+=1)); do
REPO_COUNT=0
ERROR=0
while read REPO_NAME ; do
((REPO_COUNT++))
echo -n "Forking $REPO_NAME to $DEST_ORG... "
gh repo fork --clone=false $ORG/$REPO_NAME --org $DEST_ORG >/dev/null 2>&1 ||
{ echo -e "ERROR: Unable to fork!" ; ERROR=1 ; continue ; }
echo "done"
done < <(gh repo list $ORG -L $COUNT --json name | jq '.[].name' | tr -d '"')
if [ $ERROR -eq 1 ] ; then exit 1 ; fi
if [ $REPO_COUNT -ne $COUNT ] ; then exit 0 ; fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment