Python Gitlab Api 使用方法


Posted in Python onAugust 28, 2019

简述

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

官方文档

安装

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)
# ---------------------------------------------------------------- #

总结

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

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

Python 相关文章推荐
Python实现拼接多张图片的方法
Dec 01 Python
Python编写生成验证码的脚本的教程
May 04 Python
简单介绍Python中用于求最小值的min()方法
May 15 Python
Python 关于反射和类的特殊成员方法
Sep 14 Python
python with提前退出遇到的坑与解决方案
Jan 05 Python
Python读写文件基础知识点
Jun 10 Python
解决Python命令行下退格,删除,方向键乱码(亲测有效)
Jan 16 Python
如何解决tensorflow恢复模型的特定值时出错
Feb 06 Python
python中什么是面向对象
Jun 11 Python
详解python3类型注释annotations实用案例
Jan 20 Python
Python数据可视化之绘制柱状图和条形图
May 25 Python
Python序列化模块JSON与Pickle
Jun 05 Python
face++与python实现人脸识别签到(考勤)功能
Aug 28 #Python
OpenCV+face++实现实时人脸识别解锁功能
Aug 28 #Python
Python的垃圾回收机制详解
Aug 28 #Python
Python通过cv2读取多个USB摄像头
Aug 28 #Python
python3.5 cv2 获取视频特定帧生成jpg图片
Aug 28 #Python
Django--权限Permissions的例子
Aug 28 #Python
Python中函数的返回值示例浅析
Aug 28 #Python
You might like
php中支持多种编码的中文字符串截取函数!
2007/03/20 PHP
php获取网页中图片、DIV内容的简单方法
2014/06/19 PHP
ThinkPHP查询返回简单字段数组的方法
2014/08/25 PHP
php+ajax实现无刷新文件上传功能(ajaxuploadfile)
2018/02/11 PHP
php原生数据库分页的代码实例
2019/02/18 PHP
js Date自定义函数 延迟脚本执行
2010/03/10 Javascript
jQuery旋转木马式幻灯片轮播特效
2015/12/04 Javascript
jQuery实现图像旋转动画效果
2016/05/29 Javascript
jQuery实现删除li节点的方法
2016/12/06 Javascript
js闭包用法实例详解
2016/12/13 Javascript
基于node.js制作简单爬虫教程
2017/06/29 Javascript
js中getter和setter用法实例分析
2018/08/14 Javascript
对vue事件的延迟执行实例讲解
2018/08/28 Javascript
js replace替换字符串同时替换多个方法
2018/11/27 Javascript
微信公众平台获取access_token的方法步骤
2019/03/29 Javascript
微信小程序实现的一键连接wifi功能示例
2019/04/24 Javascript
JS判断数组四种实现方法详解
2020/06/29 Javascript
[59:08]DOTA2上海特级锦标赛C组小组赛#2 LGD VS Newbee第一局
2016/02/27 DOTA
简单讲解Python编程中namedtuple类的用法
2016/06/21 Python
pycharm 取消默认的右击运行unittest的方法
2018/11/29 Python
python语言基本语句用法总结
2019/06/11 Python
Python线程threading模块用法详解
2020/02/26 Python
如何在pycharm中安装第三方包
2020/10/27 Python
Pandas数据分析的一些常用小技巧
2021/02/07 Python
WoolOvers爱尔兰:羊绒、羊毛和棉针织品
2017/01/04 全球购物
德国健康生活方式网上商店:Landkaufhaus Mayer
2019/03/12 全球购物
越南综合购物网站:Lazada越南
2019/06/10 全球购物
Athleta官网:购买女士瑜伽服、技术运动服和休闲运动服
2020/11/12 全球购物
小学综合实践活动总结
2014/07/07 职场文书
不遵守课堂纪律的检讨书
2014/09/24 职场文书
亲属关系公证书样本
2015/01/23 职场文书
2015年清剿火患专项行动工作总结
2015/07/27 职场文书
《平移和旋转》教学反思
2016/02/19 职场文书
html5中sharedWorker实现多页面通信的示例代码
2021/05/07 Javascript
Java实现给Word文件添加文字水印
2022/02/15 Java/Android
Netty客户端接入流程NioSocketChannel创建解析
2022/03/25 Java/Android