Last active
July 9, 2018 18:11
-
-
Save Laffs2k5/8a86ecd5a68aa3f8f6462a2f1d8647dd to your computer and use it in GitHub Desktop.
Script to clone all repositories in all projects in a bitbucket server (aka stash)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# generate_clone_all_bitbucket_repos.sh | |
# | |
# Based on https://gist.github.com/eeichinger/babbdf99d8fe21fef8634d56f29defba | |
# Credits goes to https://gist.github.com/eeichinger | |
# | |
# Requirements: | |
# - jq - commandline JSON processor | |
# sudo dnf install jq.x86_64 | |
# | |
# Note: make sure set your username, password and server | |
# | |
BITBUCKET_USERNAME= | |
BITBUCKET_PASSWORD= | |
BITBUCKET_SERVER= | |
CURRENT_DIR=$(pwd) | |
echo "Runing in directory: $CURRENT_DIR" | |
echo "Reading projects from: $BITBUCKET_SERVER" | |
PROJECT_KEYS=( $(curl -s -k -u $BITBUCKET_USERNAME:$BITBUCKET_PASSWORD $BITBUCKET_SERVER/rest/api/1.0/projects/?limit\=1000 | jq -r '.values[].key')) | |
NUM_PROJECTS=${#PROJECT_KEYS[@]} | |
echo "Found $NUM_PROJECTS project(s)" | |
if [ $NUM_PROJECTS -le 0 ]; then | |
echo "No projects found!" | |
echo "Please check username/password/server config." | |
else | |
for PROJECT_KEY in "${PROJECT_KEYS[@]}" | |
do | |
echo "Project: $PROJECT_KEY" | |
PROJECT_DIR="$CURRENT_DIR/$PROJECT_KEY" | |
echo -e "\tCreating: $PROJECT_DIR" | |
mkdir -p $PROJECT_DIR | |
echo -ne "\tReading repos..." | |
PROJECT_REPO_URIS=( $( curl -s -u $BITBUCKET_USERNAME:$BITBUCKET_PASSWORD $BITBUCKET_SERVER/rest/api/1.0/projects/$PROJECT_KEY/repos/?limit\=1000 \ | |
| jq -r '.values[].links.clone[] | select(.name=="ssh") | .href' )) | |
NUM_REPOS=${#PROJECT_REPO_URIS[@]} | |
echo "found $NUM_REPOS." | |
if [ $NUM_PROJECTS -le 0 ]; then | |
echo -e "\tNothing to do." | |
else | |
REPO_CHECKOUT_SCRIPT="$PROJECT_DIR/clone_repos.sh" | |
echo -e "\tWriting '$REPO_CHECKOUT_SCRIPT'..." | |
rm -f "$REPO_CHECKOUT_SCRIPT" | |
for REPO_URI in "${PROJECT_REPO_URIS[@]}" | |
do | |
cmd="git clone '$REPO_URI'" | |
echo $cmd >> $REPO_CHECKOUT_SCRIPT | |
# Uncomment to perform checkout directly | |
#cd "$PROJECT_DIR" | |
#eval $cmd | |
#cd "$CURRENT_DIR" | |
done | |
fi | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment