2016-11-04 109 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"]