Python Gitlab Api 使用方法

时间:2021-05-22

简述

公司使用gitlab 来托管代码,日常代码merge request 以及其他管理是交给测试,鉴于操作需经常打开网页,重复且繁琐,所以交给Python 管理。

官方文档

安装

pip install python-gitlab

环境: py3

DEMO

# -*- coding: utf-8 -*-__Author__ = "xiewm"__Date__ = '2017/12/26 13:46'"""gitlab 经常使用到的apiDOC_URL: http://python-gitlab.readthedocs.io/en/stable/LOCAL_PATH: C:\Python36\Lib\site-packages\gitlab"""import gitlaburl = 'http://xxxxxxx'token = 'xxxxxxxxxxxxxx'# 登录gl = gitlab.Gitlab(url, token)# ---------------------------------------------------------------- ## 获取第一页projectprojects = gl.projects.list()# 获取所有的projectprojects = gl.projects.list(all=True)# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 获取所有project的name,idfor p in gl.projects.list(all=True, as_list=False): print(p.name, p.id)# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 获取第一页project的name,idfor p in gl.projects.list(page=1): print(p.name, p.id)# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 通过指定id 获取 project 对象project = gl.projects.get(501)# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 查找项目projects = gl.projects.list(search='keyword')# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 创建一个项目project = gl.projects.create({'name':'project1'})# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 获取公开的项目projects = gl.projects.list(visibility='public') # public, internal or private# ---------------------------------------------------------------- ## 获取 project 对象是以下操作的基础# ---------------------------------------------------------------- ## 通过指定project对象获取该项目的所有分支branches = project.branches.list()print(branches)# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 获取指定分支的属性branch = project.branches.get('master')print(branch)# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 创建分支branch = project.branches.create({'branch_name': 'feature1', 'ref': 'master'})# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 删除分支project.branches.delete('feature1')# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 分支保护/取消保护branch.protect()branch.unprotect()# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 获取指定项目的所有tagstags = project.tags.list()# 获取某个指定tag 的信息tags = project.tags.list('1.0')# 创建一个tagtag = project.tags.create({'tag_name':'1.0', 'ref':'master'})# 设置tags 说明:tag.set_release_description('awesome v1.0 release')# 删除tagsproject.tags.delete('1.0')# ortag.delete()# ---------------------------------------------------------------- ## 获取所有commit infocommits = project.commits.list()for c in commits: print(c.author_name, c.message, c.title)# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 获取指定commit的infocommit = project.commits.get('e3d5a71b')# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 获取指定项目的所有merge requestmrs = project.mergerequests.list()print(mrs)# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 获取 指定mr infomr = project.mergerequests.get(mr_id)# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 创建一个merge requestmr = project.mergerequests.create({'source_branch':'cool_feature', 'target_branch':'master', 'title':'merge cool feature', })# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 更新一个merge request 的描述mr.description = 'New description'mr.save()# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 开关一个merge request (close or reopen):mr.state_event = 'close' # or 'reopen'mr.save()# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## Delete a MR:project.mergerequests.delete(mr_id)# ormr.delete()# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## Accept a MR:mr.merge()# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 指定条件过滤 所有的merge request# state: state of the MR. It can be one of all, merged, opened or closed# order_by: sort by created_at or updated_at# sort: sort order (asc or desc)mrs = project.mergerequests.list(state='merged', sort='asc') # all, merged, opened or closed# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## 创建一个commitdata = { 'branch_name': 'master', # v3 'commit_message': 'blah blah blah', 'actions': [ { 'action': 'create', 'file_path': 'blah', 'content': 'blah' } ]}commit = project.commits.create(data)# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## Compare two branches, tags or commits:result = project.repository_compare('develop', 'feature-20180104')print(result)# get the commitsfor commit in result['commits']: print(commit)## get the diffsfor file_diff in result['diffs']: print(file_diff)# ---------------------------------------------------------------- ## ---------------------------------------------------------------- ## get the commitsfor commit in result['commits']: print(commit)## get the diffsfor file_diff in result['diffs']: print(file_diff)# ---------------------------------------------------------------- #

总结

通过以上的api 可以封装一整套gitlab 的脚本操作或者是命令行操作。

以上这篇Python Gitlab Api 使用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章