Skip to content

Instantly share code, notes, and snippets.

@ziteh
Last active July 25, 2024 10:10
Show Gist options
  • Save ziteh/86749d9289132d9337a82eb24035e541 to your computer and use it in GitHub Desktop.
Save ziteh/86749d9289132d9337a82eb24035e541 to your computer and use it in GitHub Desktop.
Clone all repos from GitHub script
#!/bin/bash
reposFile="repos.txt"
projectPairFile="project-pair.txt"
# Processing GitHub user/org Bitbucket project ket pair
declare -A projectPair
while IFS='=' read -r gh bb; do
if [ -n "$gh" ] && [ -n "$bb" ]; then
echo "GitHub User/Org: $gh Bitbucket Project Key: $bb"
projectPair["$gh"]="$bb"
fi
done <"$projectPairFile"
work="$USERNAME"
curlOp=(-u "$USERNAME:$BITBUCKET_APP_PASSWORD" --silent)
# Validating Bitbucket credentials
curl --fail "${curlOp[@]}" "https://api.bitbucket.org/2.0/user" >/dev/null || {
echo "Validation of Bitbucket credentials failed."
exit 1
}
while IFS='=' read -r user repo; do
if [ -n "$user" ] && [ -n "$repo" ]; then
echo "Processing: $user/$repo"
# Checking if Bitbucket repository exists
if curl "${curlOp[@]}" "https://api.bitbucket.org/2.0/repositories/$work/$repo" | grep "error" >/dev/null; then
projectKey="${projectPair[$user]}"
json=$(printf '{"scm": "git", "is_private": true, "project": {"key": "%s"}}' "${projectKey}")
echo "Bitbucket repository \"$work/$repo\" does NOT exist, creating it in project \"$projectKey\"..."
# https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-repo-slug-post
if ! curl -X POST --fail "${curlOp[@]}" "https://api.bitbucket.org/2.0/repositories/$work/$repo" \
-H "Content-Type: application/json" -d "$json"; then
echo "Failed to created Bitbucket repo \"$work/$repo\" in project \"$projectKey\"."
continue
fi
fi
echo "Cloning from GitHub..."
git clone --mirror "https://$USERNAME:$GITHUB_TOKEN@github.com/$user/$repo.git" temp-repo
echo "Pushing to Bitbucket..."
git -C temp-repo push --mirror "https://$USERNAME:$BITBUCKET_APP_PASSWORD@bitbucket.org/$work/$repo.git"
rm -rf temp-repo
echo "Done."
fi
done <"$reposFile"
#!/bin/bash
# clone all repos from GitHub, using GitHub CLI
# input arg1 for user or org name, arg2 for option
# source: https://stackoverflow.com/questions/19576742/how-to-clone-all-repos-at-once-from-github
if [ -z "$1" ]; then
echo "Usage: bash $0 <USERNAME> [OPTION]"
exit 1
fi
username=$1
option=$2
gh repo list "$username" --limit 1000 | while read -r repo _; do
if [ -d "${repo}.git" ]; then
# Already exists and is not an empty directory, fetch.
echo "Fetch '$repo'"
git -C "${repo}.git" fetch --prune-tags
else
# New repo, clone repo.
echo "Clone '$repo'"
gh repo clone "$repo" "${repo}.git" -- --mirror $option
fi
done
#!/bin/bash
# Check if GitHub CLI is installed
if ! command -v gh &> /dev/null; then
echo "GitHub CLI (gh) is not installed."
exit 1
fi
# Check if required arguments are provided
if [ $# -ne 2 ]; then
echo "Usage: $0 <USERNAME> <OUTPUT_FILE>"
exit 1
fi
username=$1
file=$2
# Clear the output file
: > "$file"
gh repo list "$username" --limit 1000 | while read -r repo _; do
echo "${repo//\//=}" >> "$file"
echo "$repo"
done
@ziteh
Copy link
Author

ziteh commented Jun 25, 2024

git clone --mirror

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment