3

i want to know if it's possible to clone all repositories in a team from bitbucket since git.

Thanks in advance.

Capriatto
  • 947
  • 1
  • 8
  • 17
  • 1
    I don't think GIT supports this natively, however there is an endpoint where you can grab all repos for a particular team, outlined here - https://bitbucket.org/site/master/issue/7659/use-api-to-get-repositories-by-team-name ( For example, Atlassian's repositories: https://bitbucket.org/!api/1.0/users/atlassian ). I'll bet there's a way with some elbow grease to use the results of that API with this somehow - http://stackoverflow.com/questions/18900294/checkout-multiple-git-repositories-in-one-local-directory – streetlogics Jan 23 '14 at 16:13

4 Answers4

8

I've made up this tiny Ruby script. Test it and remove the echo from within the system() call for it to actually do the cloning.

#!/usr/bin/env ruby

# USAGE: ./clone ORG_NAME
# ( ./clone instedd )
require 'open-uri'
require 'json'

team = ARGV[0] || raise("Must specify organization name")

puts "Fetching https://bitbucket.org/!api/1.0/users/#{team}..."

data = JSON.parse(open("https://bitbucket.org/!api/1.0/users/#{team}").read)

data["repositories"].each do | repo |
  # delete "echo" for _actually_ cloning them
  system("echo #{repo["scm"]} clone https://bitbucket.org/#{team}/#{repo["slug"]}")
end
mgarciaisaia
  • 14,521
  • 8
  • 57
  • 81
  • 2
    What about `ssh keys`? – Shamoon Dec 08 '14 at 13:56
  • @Shamoon, the script uses the `https` protocol, so there is no ssh connection. You can tune the `system()` like to meet your needs - that just runs `$VCS clone $URL` for each of the repositories. Do you have any specific issue that makes you ask about those keys? – mgarciaisaia Dec 09 '14 at 03:45
  • Well, I am trying to clone for users of my service – Shamoon Dec 09 '14 at 14:49
  • Do you know how to find the org name if you have a self hosting bitbucket server ? – tyrell_c Aug 24 '16 at 14:34
  • @tyrell_c if you're talking about Attlassian's Stash, then it seems you have something called a Project, in which you put repositories. Check the [REST API docs](https://developer.atlassian.com/static/rest/stash/3.11.6/stash-rest.html#idp2971616) to get an idea - I don't use Stash, so I can't really help there. – mgarciaisaia Oct 04 '16 at 17:24
2

git_cloner is a free and open-source tool that I have developed to clone a bunch of BitBucket or GitHub repositories.

Example:

git_cloner --type bitbucket --login user --password password https://my_bitbucket

Use REST API:

  1. Get JSON with all projects: http://my_bitbucket/rest/api/1.0/projects

    Or: http://my_bitbucket/rest/api/1.0/<user>/projects

  2. Get JSON with repositories for every project:

    http://my_bitbucket/rest/api/1.0/projects/<project_name>/repos?limit=10000
    
  3. Clone repos by ['links']['clone']['href'] parameter from the JSON.

Full example: https://github.com/artiomn/git_cloner/blob/master/src/git_cloner/bitbucket.py

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
A.N.
  • 278
  • 2
  • 13
  • Thanks for improving this. However, if you are the author of this tool, we require that you [disclose your affiliation](https://stackoverflow.com/help/promotion). I couldn't tell for sure if you are affiliated with it or not, but it looks like it. – Cody Gray - on strike Aug 20 '17 at 13:17
  • Yes, it's my tool with a free licence. – A.N. Aug 20 '17 at 14:57
0
  1. download jq
  2. fetch https://bitbucket.org/!api/1.0/users/[teamslug] w/ authed browser inspect cookies, and find the bb_session cookie
  3. exec the following replacing [teamslug] and [bb_cookie]

A=[teamslug]; BBCOKIE=[bb_cookie]; for repo in $(curl -s -o 'https://bitbucket.org/!api/1.0/users/$A' | jq --raw-output '.repositories[].slug'); do echo git clone https://bitbucket.org/$A/$repo >> all_team_repos; done

  1. sh all_team_repos
0

For hg i used follow script, you can use it for git

import getpass
import requests
import urllib.parse
import json
import os

username = input('Username: ')
password = getpass.getpass("Password: ")

params = {
    "pagelen": 100
}
url = "https://api.bitbucket.org/2.0/repositories/%s" % username
url = url + "?" + urllib.parse.urlencode(params)
repos_data = requests.get(url, auth=(username, password))

repos_data = json.loads(repos_data.content)


for repo in repos_data["values"]:
    os.system("hg clone %s" % repo["links"]["clone"][1]["href"])
Gosha null
  • 557
  • 1
  • 5
  • 13