Python脚本自动拉取GitLab代码(自动下载各分支)

  • 配置Python编译器

 

 

 

  • 引入依赖包

 

  • 执行python脚本
# -*- coding: UTF-8 -*-

# 在Python3.0测试通过
# 需要在gitlab里面新建一个AccessToken填入gitlabToken

import 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 = 'xxxxxxx'
gitlabToken = 'xxxxxxxxxxxx'

# sync syn projects
for index in range(10):
    url = "https://%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:
            thisProjectId = thisProject['id']
            thisProjectURL = thisProject['http_url_to_repo']
            thisProjectPath = thisProject['path_with_namespace']
            isContinue = True
            if not thisProjectPath.startswith('jiaoguan/yingji/'):
                continue

            print(thisProjectURL + ' ' + thisProjectPath)
            if not os.path.isdir(thisProjectPath):
                print("create project dir %s" % (thisProjectPath))
                os.makedirs(thisProjectPath)
                time.sleep(0.5)

            # syn branches
            branchesUrl = "https://%s/api/v4/projects/%s/repository/branches?private_token=%s" % (gitlabAddr,  thisProjectId , gitlabToken)
            print(branchesUrl)

            allBranches = urlopen(branchesUrl)
            allBranchesDict = json.loads(allBranches.read().decode(encoding='UTF-8'))
            for thisBranch in allBranchesDict:
                thisBranchName = thisBranch['name']
                thisBranchPath = thisProjectPath + '/' + thisBranchName
                if not os.path.exists(thisBranchPath):
                    print("create branch dir %s" % (thisBranchPath))
                    os.makedirs(thisBranchPath)
                    time.sleep(0.5)

                # sync code
                gitDirPath = thisBranchPath + '/.git'
                if os.path.exists(gitDirPath):
                    command = shlex.split('C:/Program Files/Git/bin/git -C "%s" pull' % (thisBranchPath))
                else:
                    command = shlex.split(
                        'C:/Program Files/Git/bin/git clone -b %s %s %s' % (thisBranchName,thisProjectURL, thisBranchPath))

                resultCode = subprocess.Popen(command)
                time.sleep(1)
        except Exception as e:
            print("Error on %s: %s" % (thisProjectURL, e.strerror))
  • 趟坑指南
  • python版本必须选3.7及以下
  • gitlab在设置中,设置访问令牌
gitlabAddr  gitlab网址(不带http)
gitlabToken 访问令牌

 

posted @ 2021-04-22 10:06  yang7527  阅读(2572)  评论(0编辑  收藏  举报