Skip to content

Instantly share code, notes, and snippets.

@filipeandre
Created May 24, 2018 11:57
Show Gist options
  • Save filipeandre/2db6f2984c35639b839f8f547c4f214b to your computer and use it in GitHub Desktop.
Save filipeandre/2db6f2984c35639b839f8f547c4f214b to your computer and use it in GitHub Desktop.
Generate a file to clone all repos from bitbucket server
# this script uses syntax for bitbucket server.
# For bitbucket cloud see https://confluence.atlassian.com/bitbucket/use-the-bitbucket-cloud-rest-apis-222724129.html
#
# Note: replace your_server_url, your_username with your values
SERVER=your_server_url
USERNAME=your_username
echo "Whats your password?"
read -s PASSWORD
# https://stackoverflow.com/questions/46923816/how-to-retrieve-the-list-of-repos-using-a-particular-plugin-using-bitbucket-api
for r in $(curl -k -s --user $USERNAME:$PASSWORD --request GET https://$SERVER/rest/api/1.0/projects | jq --raw-output '.values[].key')
do
# jq syntax helpful links:
# - http://stackoverflow.com/questions/30601278/how-to-extract-keys-from-multiple-nested-arrays-using-jq
# - http://stackoverflow.com/questions/18592173/select-objects-based-on-value-of-variable-in-object-using-jq
curl -k -s -u $USERNAME:$PASSWORD https://$SERVER/rest/api/1.0/projects/$r/repos/ \
| jq -r '.values[].links.clone[] | select(.name=="ssh") | .href' \
| xargs -I {} echo "git clone '{}'" >> clone_repos.sh
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment