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写asp详细讲解
Dec 16 Python
Python实现拼接多张图片的方法
Dec 01 Python
django2用iframe标签完成网页内嵌播放b站视频功能
Jun 20 Python
删除DataFrame中值全为NaN或者包含有NaN的列或行方法
Nov 06 Python
Python的UTC时间转换讲解
Feb 26 Python
Python实现的合并两个有序数组算法示例
Mar 04 Python
关于python3中setup.py小概念解析
Aug 22 Python
Python实现TCP通信的示例代码
Sep 09 Python
Python模拟伯努利试验和二项分布代码实例
May 27 Python
python中plt.imshow与cv2.imshow显示颜色问题
Jul 16 Python
python 用pandas实现数据透视表功能
Dec 21 Python
python基于opencv 实现图像时钟
Jan 04 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
服务器端解压缩zip的脚本
2006/12/22 PHP
建站常用13种PHP开源CMS比较
2009/08/23 PHP
CodeIgniter框架数据库事务处理的设计缺陷和解决方案
2014/07/25 PHP
DOM基础及php读取xml内容操作的方法
2015/01/23 PHP
php使用unset()删除数组中某个单元(键)的方法
2015/02/17 PHP
PHP模板引擎Smarty之配置文件在模板变量中的使用方法示例
2016/04/11 PHP
php 使用expat方式解析xml文件操作示例
2019/11/26 PHP
PHP Pipeline 实现中间件的示例代码
2020/04/26 PHP
JQuery radio(单选按钮)操作方法汇总
2015/04/15 Javascript
JS中捕获console.log()输出的方法
2015/04/16 Javascript
JavaScript实现的冒泡排序法及统计相邻数交换次数示例
2017/04/26 Javascript
Vue filters过滤器的使用方法
2017/07/14 Javascript
two.js之实现动画效果示例
2017/11/06 Javascript
JS中使用cavas截图网页并解决跨域及模糊问题
2018/11/13 Javascript
详解VUE项目中安装和使用vant组件
2019/04/28 Javascript
JS获取本地地址及天气的方法实例小结
2019/05/10 Javascript
express启用https使用小记
2019/05/21 Javascript
使用python实现个性化词云的方法
2017/06/16 Python
pandas重新生成索引的方法
2018/11/06 Python
python给微信好友定时推送消息的示例
2019/02/20 Python
Django如何使用第三方服务发送电子邮件
2019/08/14 Python
python实现一个点绕另一个点旋转后的坐标
2019/12/04 Python
Python基于模块Paramiko实现SSHv2协议
2020/04/28 Python
使用opencv中匹配点对的坐标提取方式
2020/06/04 Python
Python绘图之柱形图绘制详解
2020/07/28 Python
英国老牌潮鞋店:Offspring
2019/08/19 全球购物
教师个人的自我评价分享
2014/01/02 职场文书
本科毕业生专业自荐书范文
2014/02/05 职场文书
企业精神口号
2014/06/11 职场文书
光学与应用专业毕业生求职信
2014/09/01 职场文书
2014员工聘用协议书(最新版)
2014/11/24 职场文书
侵犯商业秘密的律师函
2015/05/27 职场文书
导游词创作书写原则以及开场白技巧怎么学?
2019/09/25 职场文书
WordPress多语言翻译插件 - WPML使用教程
2021/04/01 PHP
JavaScript声明变量和数据类型的转换
2022/04/12 Javascript
Golang实现可重入锁的示例代码
2022/05/25 Golang