一、使用python对gitlab进行自动化操作
1.python-gitlab模块官网文档
2.gitlab官网文档
二、常用使用功能
def login():
gl = gitlab.Gitlab('https://git.4399.com', private_token='dfmkbarHTTSLSGDsdqwt4')
return gl
gl = login()
project = gl.projects.get(4920)
branches = project.branches.list()
print branches
try:
branch = project.branches.get('ReplaceWeb')
project.branches.delete('ReplaceWeb')
print u"删除旧分支成功"
except gitlab.exceptions.GitlabGetError as error:
print(error)
print u"旧分支不存在"
try:
branch = project.branches.create({'branch': 'ReplaceWeb', 'ref': 'develop'})
print u"创建新分支ReplaceWeb成功"
except gitlab.exceptions.GitlabCreateError as error:
print(error)
data = {
'branch': 'ReplaceWeb',
'commit_message': 'update web file',
'actions': [
{
'action': 'update',
'file_path': '/assist/src/main/AppConfig.h',
'content': open('D:/WebContent/AppConfig.h').read(),
},
{
'action': 'update',
'file_path': '/bin/web_content/assist_web.zip',
'encoding': 'base64',
'content': base64.b64encode(open('D:/WebContent/assist_web.zip', "rb").read()),
}
]
}
project.commits.create(data)
mr = project.mergerequests.create({'source_branch': 'ReplaceWeb',
'target_branch': 'develop',
'title': 'update oa_web pack'})
三、python-gitlab:下载文件或者指定文件夹
1.下载文件
def emptyFolder(pathName):
if 1 == os.path.exists(pathName):
shutil.rmtree(pathName)
allPath = 'D:/WebContent/'
emptyFolder(allPath)
os.makedirs(allPath)
with open('D:/WebContent/AppConfig.h', 'wb') as f:
project.files.raw(file_path='/src/main/AppConfig.h', ref='ReplaceWeb', streamed=True, action=f.write)
print u"下载AppConfig.h成功"
2.下载文件夹
def emptyFolder(pathName):
if 1 == os.path.exists(pathName):
shutil.rmtree(pathName)
allPath = 'D:/WebContent/'
emptyFolder(allPath)
os.makedirs(allPath)
fileList = project.repository_tree(path='bin/tools/', ref='master', recursive=True, all=True)
for i in fileList:
if (i['type'] == "tree"):
dirs = 'D:/WebContent/%s' % i['path']
if not os.path.exists(dirs):
os.makedirs(dirs)
print u"...下载文件中..."
for i in fileList:
if (i['type'] == "blob"):
with open('D:/WebContent/%s' % i['path'], 'wb') as f:
project.files.raw(file_path=i['path'], ref='master', streamed=True, action=f.write)
print u"%s" %i['path']
print u"...文件下载完毕..."
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)