Skip to content

Instantly share code, notes, and snippets.

@mikaelvesavuori
Created May 13, 2024 11:40
Show Gist options
  • Save mikaelvesavuori/d287b7b43751cc423b0da0c760a49922 to your computer and use it in GitHub Desktop.
Save mikaelvesavuori/d287b7b43751cc423b0da0c760a49922 to your computer and use it in GitHub Desktop.
Clone all repositories belonging to a GitHub organization.
#!/bin/bash
GITHUB_TOKEN="your_access_token"
ORG_NAME="your_organization"
PAGE=1
function updateCloneUrlList() {
rm repos.txt
while true; do
RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/orgs/$ORG_NAME/repos?per_page=100&page=$PAGE")
CLONE_URLS=$(echo "$RESPONSE" | jq -r '.[].clone_url | sub("^https://github.com/$ORG_NAME/"; "")') # UPDATE ORG NAME
# Check if there are no more repositories on the current page
if [ -z "$CLONE_URLS" ]; then
break
fi
echo "$CLONE_URLS" >>repos.txt
PAGE=$((PAGE + 1))
done
}
function cloneRepos() {
CLONE_URLS=()
while IFS= read -r line; do
CLONE_URLS+=("$line")
done <repos.txt
for URL in "${CLONE_URLS[@]}"; do
echo "Handling repository: $URL"
REPO_PART="${URL#https://github.com/${ORG_NAME}/}"
REPO_NAME="${REPO_PART%.git}"
REPO_PATH="./$REPO_NAME"
if [ -d "$REPO_PATH" ]; then
echo "⬇️ Pulling latest changes for $REPO_NAME"
(cd "$REPO_PATH" && git pull)
else
echo "🤖 Cloning $REPO_NAME"
git clone [email protected]:$ORG_NAME/$URL $REPO_PATH
fi
done
}
function checkDeletedRepos() {
file_path="repos.txt"
if [ ! -f "$file_path" ]; then
echo "❌ Error: 'repos.txt' file not found."
exit 1
fi
touch deleted_repos.txt
while IFS= read -r folder; do
folder=$(echo "$folder" | xargs)
REPO_NAME="${folder%.git}"
if [ ! -d "$REPO_NAME" ]; then
echo "$REPO_NAME" >>deleted_repos.txt
echo "❌ Folder '$REPO_NAME' does not exist in the current directory."
fi
done <"$file_path"
}
updateCloneUrlList
cloneRepos
checkDeletedRepos
echo "✅ Cloning, pulling, and checking complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment