自动在bitbucket中创建项目和存储库的脚本

时间:2019-04-19 15:02:25

标签: git bitbucket bitbucket-server bitbucket-api

我正在将整套项目从TFS迁移到Git。我的项目结构如下:

Project 1:
---Repository1
---Repository2
---
---
---Repository10

Project 2:
---Repository1
---Repository2
---
---
---Repository20

因此有多个项目,并且每个项目下都包含多个存储库。当我将代码从本地存储库移到Bitbucket时,最终要分别手动创建每个项目和文件夹。相反,有什么脚本可以在Bitbucket中为我自动创建Project和存储库的这种结构?如果是,有人可以分享一个例子吗? TIA!

编辑:我发现一个脚本可以实际执行此操作:https://bobswift.atlassian.net/wiki/spaces/SCLI/pages/87720019/How+to+create+projects+based+on+a+template

所以我的问题是,要实现链接中提到的内容,我需要安装Bitbucket CLI还是可以使用命令行界面完成它?

1 个答案:

答案 0 :(得分:1)

以下是一些我用来将存储库导入到bitbucket中的Perl:

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use File::Basename;
my $numArgs = $#ARGV + 1;

if($numArgs < 2) {
 die "Usage: $0 [Bit Bucket Project e.g. FW, BDPE] [repo name] [-d dry run (optional)]";
}

my $bitbucketProject = lc $ARGV[0];
my $repoName = $ARGV[1];
my $dryRun = $ARGV[2];
my %moduleHash;
my $bitBucketServer = "localhost";
my $user = "admin";
my $password = "bitbucket";


print "Bit Bucket Project: $bitbucketProject\n";
print "Repository name: $repoName\n";

sub importRepo {

     my $command = sprintf("curl -u %s:%s -X POST -H \"Content-Type: application/json\" -d '{
     \"name\": \"%s\",
     \"scmId\": \"git\",
     \"forkable\": true
     \}' http://%s:7990/rest/api/1.0/projects/%s/repos", $user, $password, $repoName, $bitBucketServer, $bitbucketProject); 

    if ($dryRun) {
      print "$command\n";
    } else {
    print "Doing import\n";
        system $command;
    }
    my $bitbucketUrl = sprintf("ssh://git\@%s:7999/%s/%s.git", $bitBucketServer, lc $bitbucketProject, $repoName);   
    my $gitCommand = sprintf("cd %s; pwd;  git repack -a -d -f; git push %s --mirror", $repoName, $bitbucketUrl);
    if ($dryRun) {
      print "$gitCommand\n";
    } else {   
       print "Running git\n";
       system $gitCommand;
    }

}

importRepo();

然后您可以使用Shell脚本将其包装起来:

#!/bin/bash

BITBUCKETPROJECT=$1

if [ $# -ne 2 ]; then
echo "Usage: $0 [Bit Bucket Project] [Path to repos]"
exit 1;
fi

echo "Bit bucket project: $BITBUCKETPROJECT"

    for f in *; do
        if [[ -d $f ]]; then
          echo $f
          ./importRepository.pl $BITBUCKETPROJECT $f 
        fi
    done

假设您的所有存储库都已克隆到当前目录中。

https://developer.atlassian.com/static/rest/bitbucket-server/latest/bitbucket-rest.html

上面的示例未在Bitbucket上创建项目,但这只是一个起点。