2016-11-04 181 views
0

我想用bash腳本和http克隆我所有bitbucket團隊存儲庫。我發現了一些使用Bitbucket api的例子,但它們似乎都沒有返回任何存儲庫。有任何想法嗎?使用mac。Bitbucket克隆所有團隊存儲庫

+1

如果您的存儲庫託管在Bitbucket上,爲什麼使用Github API? – larsks

+0

錯字我打算說bitbucket – Imran

+1

你是否認證?您認證的用戶是否可以訪問團隊信息庫?如果不是,我建議您設置一個應用程序密碼(在https:// bitbucket。org/account/user//app-passwords)具有足夠的權限(對帳戶/團隊成員資格/項目/存儲庫的讀取權限應涵蓋您需要的大部分內容)。 – Dymos

回答

2

確保你有你的SSH密鑰設置在到位桶和Git是通過克隆手動一個回購安裝:

這將克隆由你所擁有的所有回購:

USER=bitbucket_username; curl --user ${USER} https://api.bitbucket.org/2.0/repositories/${USER} | grep -o '"ssh:[^ ,]\+' | xargs -L1 git clone 

要備份你的團隊庫使用相同的腳本,但很難代碼的到位桶隊名是這樣的:

USER=bitbucket_username; curl --user ${USER} https://api.bitbucket.org/2.0/repositories/TEAMNAME | grep -o '"ssh:[^ ,]\+' | xargs -L1 git clone 

這裏有一個better way

curl -u ${1} https://api.bitbucket.org/1.0/users/TEAMNAME > repoinfo 

for repo_name in `cat repoinfo | sed -r 's/("name":)/\n\1/g' | sed -r 's/"name": "(.*)"/\1/' | sed -e 's/{//' | cut -f1 -d\" | tr '\n' ' '` 
do 
    echo "Cloning " $repo_name 
    git clone [email protected]:TEAMNAME/$repo_name.git 
    echo "---" 
done 
1

如果您的存儲庫少於100個,並且在查詢中添加'pagelen = 100',那麼只能使用一個簡單的命令,因爲這是bitbucket API一次報告的最多。如果您有超過100個存儲庫,則需要處理返回的JSON中的「下一個」鏈接,以獲取URL以查詢下一組存儲庫,這對於腳本來說更容易。

如果您使用HTTP克隆而不是使用ssh,那麼您可能需要對受保護的資料庫輸入密碼,否則拿到到位桶應用程序的密碼,並修改URL以插入到他們,讓他們看起來像:

https://bitbucketuserhere:[email protected]/teamorusername/repositoryname.git 

此外,克隆不會獲得所有版本的git LFS文件,因此請注意這一點。根據bitbucket,使用'git fetch --all'在本地複製所有LFS文件版本。

爲了讓您的個人資料庫列表中,使用URL,如:

https://api.bitbucket.org/2.0/repositories/BITBUCKETUSERNAME?pagelen=100

爲了讓您的團隊庫的列表,使用這樣的網址,讓你的所有存儲庫列表成員:

https://api.bitbucket.org/2.0/repositories/TEAMNAME?pagelen=100&role=member

下面是一個例子Perl腳本,你可以用它來克隆,然後保持你的版本庫的拷貝,使用http而不是ssh來FE TCH。它使 - 克隆克隆而不是完全填充的工作副本(適用於移動或災難恢復)。它沒有而不是備份所有的LFS文件。

#!/usr/bin/env perl 

use warnings; 
use strict; 
use JSON::Parse 'parse_json'; 

# CONFIGURATION: 
# Bitbucket team or user name to get list of repositories from 
my $teamORuserName = "myteam"; 

# Bitbucket app password with access to query the API for the 
# list of repositories. Format: "user-name:app-token" 
my $appPassword= "frank-james:LAYDxtc8H6FGKUZeHEef"; 

#------------------------------------------------------------------------------ 

my $nextPageLink = "https://api.bitbucket.org/2.0/repositories/$teamORuserName?pagelen=100&role=member"; 
while (defined $nextPageLink) 
{ 
    $nextPageLink =~ m/page=(\d+)/; 
    print "Fetching page " . ($1 || 1). "\n"; 
    my $response = `curl -q --silent --request GET --user '$appPassword' '$nextPageLink'`; 
    my $json = parse_json($response); 
    my $values = $json->{values}; 

    foreach my $repo (@$values) 
    { 
     die "'$repo->{name}' is not a 'git' repo: $repo->{scm}" unless $repo->{scm} eq "git"; 
     my $links = $repo->{links} || die "no links data for '$repo->{name}'"; 
     my $clones = $links->{clone} || die "no clone data for '$repo->{name}'"; 
     my $url = $clones->[0]->{href} || die "no clone url found for $repo->{name}"; 

     # use uuid as directory name, to survive project name changes 
     my $uuid = $repo->{uuid}; $uuid =~ s/[\{\}]//g; 
     if (not -e $uuid) 
     { 
      print "cloning '$repo->{name}' into $uuid\n"; 
      # replace user name with token to avoid password prompts 
      $url =~ s|(https?://).+(\@bitbucket.org)|$1$appPassword$2|; 
      system("git clone --progress --mirror '$url' $uuid") == 0 or die "clone failed"; 
      # make a human friendly link to current repository name 
      symlink $uuid, $repo->{slug} or warn "symlink failed: $!"; 
     } 
     else 
     { 
      print "updating '$repo->{name}' in $uuid\n"; 
      system("cd $uuid && git fetch --all --tags --prune") == 0 or die "fetch failed"; 
     } 
     print "\n"; 
    } 

    $nextPageLink = $json->{next}; 
} 
exit 0; 
0

這是一個python腳本,用於克隆bitbucket中的所有團隊或用戶的存儲庫。由於團隊存儲庫通常是私有的,因此我在使用bitbucket API時注意到了這一點。因此,只需輸入您的bitbucket用戶名,密碼和團隊的用戶名,它將負責爲您克隆所有團隊存儲庫。

import subprocess 
import json 

cmd = "curl -u <bitbucket_username>:<bitbucket_password> https://api.bitbucket.org/2.0/repositories/<team_name_or_project_name>" 
cmd = cmd.split() 

while 1: 
    from_api = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
    from_api = from_api.communicate() 
    json_from_api = json.loads(from_api[0]) 
    for unit_dict in json_from_api["values"]: 
     clone_cmd = "git clone " + unit_dict["links"]["clone"][1]["href"] 
     clone_cmd = clone_cmd.split() 
     clone_out = subprocess.call(clone_cmd, shell=False) 
    if "next" not in json_from_api: 
     break 
    else: 
     cmd[-1] = json_from_api["next"]