脚本适用于:python2
测试版本:2.7.18
apiToken: gitlab个人账户授权的apitoken ,通过点击右上角【用户头像】->选择【Edit profile】 ->菜单中选择【 访问令牌】,右侧 进行创建,建议给予全部权限
projectUrl: gitlab地址,一般只需要修改ip和端口
python git.py
allproject.json: 所有有权限的非空项目信息
projectclone.sh : 所有项目组成的git clone 命令
projectclonemirror.sh 所有项目组成的git clone -mirror命令
projectpull.sh 所有项目组成的git pull命令
通过gitlab 提供的api接口访问项目信息
# coding: utf-8
import os
import requests
import json
import sys
#python2
reload(sys)
sys.setdefaultencoding('utf-8')#配置信息,根据实际情况修改
apiToken="X******Seq-******tc"
projectUrl='http://127.0.0.1/api/v4/projects'
#页容量
pageSize=100
projectInfo=[]
currPath=os.getcwd()
#所有项目信息(路径根据所使用环境信息,可能需要修改分隔符)
projectJson=open(currPath+"\\allproject.json",mode="w")
#clone所有项目
projectClone=open(currPath+"\\projectclone.sh",mode="w")
#clone --mirror所有项目
projectCloneMirror=open(currPath+"\\projectclonemirror.sh",mode="w")
#pull所有分支
projectPull=open(currPath+"\\projectpull.sh",mode="w")projectClone.write("#!/bin/bash\nworkpath=$(cd \"$(dirname \"$0\")\";pwd)\n")
projectCloneMirror.write("#!/bin/bash\nworkpath=$(cd \"$(dirname \"$0\")\";pwd)\n")
projectPull.write("#!/bin/bash\nworkpath=$(cd \"$(dirname \"$0\")\";pwd)\n")#get请求gitlab的v4 api接口
def get(getUrl):pageNo = 1totalPage = 1totaldata = [];while pageNo < totalPage+1:res = requests.get(url=getUrl+'?per_page='+str(pageSize)+'&page='+str(pageNo),headers={"PRIVATE-TOKEN":apiToken})#print(res.headers)totalPage=int(res.headers.get('X-Total-Pages'))totaldata.extend(json.loads(res.text));#print('total:',totalPage,'pageNo',pageNo)pageNo+=1#print(len(totaldata))return totaldata;
#获取所有分支
def getBranch(branchRepo):branchR = []branchs = get(branchRepo)for branch in branchs:branchR.append(branch['name'])return branchR#获取所有tag
def getTages(branchRepo):tagRepo = branchRepo.replace("/branches","/tags")branchR = []branchs = get(tagRepo)for branch in branchs:branchR.append(branch['name'])return branchR#获取所有项目
def getAllProject():projectList = get(projectUrl)for proj in projectList:if proj['empty_repo']:print('null project:',proj['path_with_namespace'])else:projInfo = {}projInfo['id'] = proj['id']projInfo['path_with_namespace'] = proj['path_with_namespace']projInfo['empty_repo'] = proj['empty_repo']projInfo['http_url_to_repo'] = proj['http_url_to_repo']projInfo['default_branch'] = proj['default_branch']projInfo['repo_branches'] = proj['_links']['repo_branches']projInfo['branchs'] = getBranch(projInfo['repo_branches'])projInfo['tages'] = getTages(projInfo['repo_branches'])projectInfo.append(projInfo)#处理输出文件,可单独处理#处理clone脚本projectClone.write("cd $workpath\n")projectClone.write("git clone " + projInfo['http_url_to_repo'] + " " + projInfo['path_with_namespace'] +" \n")projectClone.write("cd "+projInfo['path_with_namespace']+"\n")#处理clone --mirror脚本projectCloneMirror.write("cd $workpath\n")projectCloneMirror.write("git clone --mirror " + projInfo['http_url_to_repo'] + " " + projInfo['path_with_namespace'] +" \n")projectCloneMirror.write("cd "+projInfo['path_with_namespace']+"\n")#处理pull脚本projectPull.write("cd $workpath\n")projectPull.write("cd "+projInfo['path_with_namespace']+"\n")for branch in projInfo['branchs']:projectClone.write("git checkout \""+branch+"\"\n")projectClone.write("git pull\n")projectCloneMirror.write("git checkout \""+branch+"\"\n")projectCloneMirror.write("git pull\n")projectPull.write("git checkout \""+branch+"\"\n")projectPull.write("git pull\n")for tag in projInfo['tages']:projectClone.write("git checkout \""+tag+"\"\n")projectCloneMirror.write("git checkout \""+tag+"\"\n")projectPull.write("git checkout \""+tag+"\"\n")#projectCloneMirror.write("#!/bin/bash\nworkpath=$(cd \"$(dirname \"$0\")\";pwd)\n")#projectPull.write("#!/bin/bash\nworkpath=$(cd \"$(dirname \"$0\")\";pwd)\n")return projectInfo#
getAllProject()
#将所有项目信息写入文件
projectJson.write(json.dumps(projectInfo))projectJson.flush()
projectJson.close()projectClone.flush()
projectClone.close()projectCloneMirror.flush()
projectCloneMirror.close()projectPull.flush()
projectPull.close()
#直接调起系统命令
print(os.system('git --version'))
#自动识别仓库类型(git/svn)并更新
#!/bin/bash
basepath=$(pwd)
echo $basepath
updateSource(){#local currpath=$1cd $1if [ -d $1/.git ]; thenecho 'start update sourcecode'$1echo "开始更新"$currpathgit pull#老版本的svn每个文件夹一个.svn文件夹,elif [ -d $1/.svn ]; then#revision=`svn info |grep "Last Changed Rev:" |awk '{print $4}'`revision=`svn info |grep "最后修改的版本" |awk '{print $2}'`if [ $revision > 0 ]; thenecho '$revision 开始更新代码'$1svn update .fielsefor childfile in `ls .`dopwdif [ -d $1/$childfile ]; thenupdateSource $1/$childfileelseecho $1/$childfile' is not dir , will ignore-------<'fidonefi
}
#main run
updateSource $basepath
官网:https://about.gitlab.com/