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
Copy link

bhatikuldeep commented Nov 12, 2018

works like a charm, thanks @eeichinger!

@beingamarnath
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
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

@Justin-Schmitz
Copy link

Justin-Schmitz commented Dec 18, 2020

Worked well but I had to add this to the end of the call because the limit of returned repo's == 10 by default

curl -s -u "$ORG_USER:$ORG_PASS" https://api.bitbucket.org/2.0/repositories/$ORG_TEAM/\?q="project.key=\"$ORG_PROJECT\""\&pagelen=100\&page=2 | jq -r '.values[] | {slug:.slug, links:.links.clone[] } | select(.links.name=="ssh") | "git clone \(.links.href) \(.slug)-cloud"' >> clone-repos.sh

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

I didnt have time to work a loop for pages, so just added inline

@d3vAdv3ntur3s
Copy link

d3vAdv3ntur3s commented Feb 23, 2021

Having to use an older hosted version of Bitbucket 1.0 api, may prove useful for others. Using token generated via your user settings instead of username password option.

curl -H "Authorization: Bearer GENERATE-TOKEN-HERE" \
"$HOSTED_BITBUCKET_URL/rest/api/1.0/projects/BTT/repos?limit=1000" | \
jq -r '.values[].links.clone[] | select(.name=="http") | .href' | \
xargs -n1 git clone

@jensim
Copy link

jensim commented Feb 23, 2021

Having to use an older hosted version of Bitbucket 1.0 api, may prove useful for others. Using token generated via your user settings instead of username password option.

curl -H "Authorization: Bearer GENERATE-TOKEN-HERE" \
"$HOSTED_BITBUCKET_URL/rest/api/1.0/projects/BTT/repos?limit=1000" | \
jq -r '.values[].links.clone[] | select(.name=="http") | .href' | \
xargs -n1 git clone

Correct me if I'm wrong.. But isnt the username:token also allowed in Basic Auth requests to bitbucket server?

@d3vAdv3ntur3s
Copy link

d3vAdv3ntur3s commented Feb 24, 2021

Using token generated via your user settings instead of username password option.

Yeah two options, basic or token

@Sghosh1999
Copy link

Sghosh1999 commented Mar 2, 2021

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

@Sghosh1999
Copy link

Sghosh1999 commented Mar 2, 2021

How to create a create a repository under specific project key in bitbucket server using Git bash?
I am trying the below script.

curl -X POST -v -u $username:$password http://localhost:7990/rest/api/1.0/projects/$project_key/repos -H "Content-Type: application/json" -d "{\"name\": \"$FOLD_PATH\",\"scmId\": \"git\", \"forkable\": true }"

But it is not working. Can anyone suggest any answer? Thanks in advance.

@jensim
Copy link

jensim commented Mar 2, 2021

How to create a create a repository under specific project key in bitbucket server using Git bash?
I am trying the below script.

curl -X POST -v -u $username:$password http://localhost:7990/rest/api/1.0/projects/$project_key/repos -H "Content-Type: application/json" -d "{\"name\": \"$FOLD_PATH\",\"scmId\": \"git\", \"forkable\": true }"

But it is not working. Can anyone suggest any answer? Thanks in advance.

You used the verbose flag, but what was the error output?

https://docs.atlassian.com/bitbucket-server/rest/7.10.0/bitbucket-rest.html#idp174

You did not provide a default branch name, not sure if thats needed though

The authenticated user must have PROJECT_ADMIN permission for the context project to call this resource.

Were you using a project_admin-user?

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