Skip to content

Instantly share code, notes, and snippets.

@chrisdlangton
Last active February 13, 2020 03:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrisdlangton/a59e85386ed741cb517177816b8d0823 to your computer and use it in GitHub Desktop.
Save chrisdlangton/a59e85386ed741cb517177816b8d0823 to your computer and use it in GitHub Desktop.
Quick clone all org repos using Github Enterprise API v3
#!/usr/bin/env bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
if [[ $EUID -eq 0 ]]; then
echo -e "${RED}x${NC} This script must not be run as root"
exit 1
fi
github_host="..."
prefix_url="https://${github_host}"
declare -a groups=(
# "ics"
"anzx"
# "ex"
# "platforms"
# "sre"
# "csp"
)
CWD=$(pwd)
WORKDIR=${HOME}/workspace
mkdir -p ${WORKDIR}
cd ${WORKDIR}
for group in "${groups[@]}"
do
echo -e "${YELLOW}>${NC} Query github for repos"
repos=$(wget -q --no-check-certificate ${prefix_url}/api/v3/orgs/${group}/repos\?type=sources -O- | jq -r '.[].name')
mkdir -p ${group}
cd ${group}
while read -r repo; do
if [[ ! -d ${repo} ]]; then
echo -e "${YELLOW}>${NC} Cloning ${group}/${repo}"
git clone -q ${prefix_url}/${group}/${repo}.git && \
echo -e "${GREEN}✔${NC} Cloned"
cd ${repo}
echo -e "${YELLOW}>${NC} Use SSH remote"
git remote remove origin && \
git remote add origin git@${github_host}:${group}/${repo}.git && \
echo -e "${GREEN}✔${NC} Using SSH remote"
else
cd ${repo}
echo -e "${YELLOW}>${NC} Git pull"
git pull origin master -q && \
echo -e "${GREEN}✔${NC} Updated"
fi
cd ${WORKDIR}/${group}
done <<< "${repos}"
cd ${WORKDIR}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment