Python中GitlabApi怎么用-创新互联
这篇文章给大家分享的是有关Python中Gitlab Api怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都做网站、成都网站建设、成都外贸网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的林州网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!安装
pip install python-gitlab
环境: py3
DEMO
# -*- coding: utf-8 -*- __Author__ = "xiewm" __Date__ = '2017/12/26 13:46' """ gitlab 经常使用到的api DOC_URL: http://python-gitlab.readthedocs.io/en/stable/ LOCAL_PATH: C:\Python36\Lib\site-packages\gitlab """ import gitlab url = 'http://xxxxxxx' token = 'xxxxxxxxxxxxxx' # 登录 gl = gitlab.Gitlab(url, token) # ---------------------------------------------------------------- # # 获取第一页project projects = gl.projects.list() # 获取所有的project projects = gl.projects.list(all=True) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 获取所有project的name,id for p in gl.projects.list(all=True, as_list=False): print(p.name, p.id) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 获取第一页project的name,id for 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() # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 获取指定项目的所有tags tags = project.tags.list() # 获取某个指定tag 的信息 tags = project.tags.list('1.0') # 创建一个tag tag = project.tags.create({'tag_name':'1.0', 'ref':'master'}) # 设置tags 说明: tag.set_release_description('awesome v1.0 release') # 删除tags project.tags.delete('1.0') # or tag.delete() # ---------------------------------------------------------------- # # 获取所有commit info commits = project.commits.list() for c in commits: print(c.author_name, c.message, c.title) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 获取指定commit的info commit = project.commits.get('e3d5a71b') # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 获取指定项目的所有merge request mrs = project.mergerequests.list() print(mrs) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 获取 指定mr info mr = project.mergerequests.get(mr_id) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 创建一个merge request mr = 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) # or mr.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 # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 创建一个commit data = { '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 commits for commit in result['commits']: print(commit) # # get the diffs for file_diff in result['diffs']: print(file_diff) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # get the commits for commit in result['commits']: print(commit) # # get the diffs for file_diff in result['diffs']: print(file_diff) # ---------------------------------------------------------------- #
感谢各位的阅读!关于“Python中Gitlab Api怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前文章:Python中GitlabApi怎么用-创新互联
分享网址:http://ybzwz.com/article/ddpdog.html