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


posted @   清风木  阅读(374)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示