Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate a script to clone all repos within a specific project from bitbucket server and bitbucket cloud
# generate a script ./clone-repos.sh from a list of repos fetched via Bitbucket API
# note: requires 'curl' and 'jq' to be installed
set -e
echo -n '' > clone-repos.sh
chmod +x clone-repos.sh
ONPREM_USER=xxxxx
ONPREM_PASS=......
ONPREM_PROJECT=MYINTPROJ
curl -s -u "$ONPREM_USER:$ONPREM_PASS" https://bitbucket.mycompany.internal/rest/api/1.0/projects/$ONPREM_PROJECT/repos/\?limit=1000 | jq -r '.values[] | {slug:.slug, links:.links.clone[] } | select(.links.name=="ssh") | "git clone \(.links.href) \(.slug)-server"' >> clone-repos.sh
ORG_USER=yyyyyy
ORG_PASS=.......
ORG_PROJECT=MYCLOUDPROJ
ORG_TEAM=myteam
curl -s -u "$ORG_USER:$ORG_PASS" https://api.bitbucket.org/2.0/repositories/$ORG_TEAM/\?\q="project.key=\"$ORG_PROJECT\"" | jq -r '.values[] | {slug:.slug, links:.links.clone[] } | select(.links.name=="ssh") | "git clone \(.links.href) \(.slug)-cloud"' >> clone-repos.sh
# run the generated script
./clone-repos.sh
@bhatikuldeep

This comment has been minimized.

Copy link

bhatikuldeep commented Nov 12, 2018

works like a charm, thanks @eeichinger!

@beingamarnath

This comment has been minimized.

Copy link

beingamarnath commented Dec 12, 2018

Nice work, But I would prefer to use personal token with Read access. The script can be updated as following

# generate a script ./clone-repos.sh from a list of repos fetched via Bitbucket API
# note: requires 'curl' and 'jq' to be installed

set -e

echo -n '' > clone-repos.sh
chmod +x clone-repos.sh

ONPREM_TOKEN=......
ONPREM_PROJECT=MYINTPROJ
curl -s -H "Authorization: Bearer $ONPREM_TOKEN" https://bitbucket.mycompany.internal/rest/api/1.0/projects/$ONPREM_PROJECT/repos/\?limit=1000 | jq -r '.values[] | {slug:.slug, links:.links.clone[] } | select(.links.name=="ssh") | "git clone \(.links.href) \(.slug)-server"' >> clone-repos.sh

ORG_TOKEN=.......
ORG_PROJECT=MYCLOUDPROJ
ORG_TEAM=myteam
curl -s "Authorization: Bearer $ORG_TOKEN" https://api.bitbucket.org/2.0/repositories/$ORG_TEAM/\?\q="project.key=\"$ORG_PROJECT\"" | jq -r '.values[] | {slug:.slug, links:.links.clone[] } | select(.links.name=="ssh") | "git clone \(.links.href) \(.slug)-cloud"' >> clone-repos.sh

# run the generated script
./clone-repos.sh
@jensim

This comment has been minimized.

Copy link

jensim commented May 11, 2020

I've built a cli for cloning and pulling git repos from bitbucket server
https://github.com/jensim/bitbucket_server_cli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.