python api gitlab迁移所有项目

1、 gitlab生成token (略)

帐号后台生成toekn

2、python拉取所有的gitlab项目

代码如下: vim get_git_all.py

import os,sys
if sys.version_info < (3, 0):
    import urllib
else:
    from urllib.request import urlopen
 
import json
import subprocess, shlex
import time
import os
 
gitlabAddr  = 'gitlab.xxx.com'
gitlabToken = 'xxx'
 
for index in range(10):
    url     = "http://%s/api/v4/projects?private_token=%s&per_page=100&page=%d&order_by=name" % (gitlabAddr, gitlabToken, index)
    print(url)
    
    if sys.version_info < (3, 0):
        allProjects     = urllib.urlopen(url)
    else:
        allProjects     = urlopen(url)
        
    allProjectsDict = json.loads(allProjects.read().decode(encoding='UTF-8'))
    if len(allProjectsDict) == 0:
        break
    for thisProject in allProjectsDict: 
        try:
            thisProjectURL  = thisProject['http_url_to_repo']
            thisProjectPath = thisProject['path_with_namespace']
            print(thisProjectURL + ' ' + thisProjectPath)
            
            if os.path.exists(thisProjectPath):
#                command     = shlex.split('cd "%s" && git pull' % (thisProjectPath))
                os.system('cd %s && git pull' % thisProjectPath)

            else:
                command     = shlex.split('git clone --mirror  %s %s' % (thisProjectURL, thisProjectPath))
                resultCode  = subprocess.Popen(command)
            
            time.sleep(3)
        except Exception as e:
            print("Error on %s: %s" % (thisProjectURL, e.strerror))

3 、修改git地址脚本&&上传到新的gitlab (得保证目标仓库没有同路径的仓库)

for tmp in  `ls -r |sort  -n`;do


if [ -d $tmp ];then

cd $tmp

  for xx in `ls -r`;do

  if [ -d $xx ];then
    echo $tmp/$xx
    cd $xx
    sed -i 's#gitlab.xxx.com#gitlab.newxxx.com/lbx#g' config
    git push --mirror 

    cd ..
  fi

  done

cd ..
fi

done

提示: 运维git迁移命令:

git clone --mirror xxxxx.git

cd xxxxx

sed -i 's#gitlab.xxx.com#gitlab.xxxnew.com/lbx#g' config
 
git push --mirror
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值