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进阶教程之循环对象
Aug 30 Python
深入理解python try异常处理机制
Jun 01 Python
使用python绘制常用的图表
Aug 27 Python
简单实现python收发邮件功能
Jan 05 Python
Python标准库使用OrderedDict类的实例讲解
Feb 14 Python
python for和else语句趣谈
Jul 02 Python
Python 装饰器原理、定义与用法详解
Dec 07 Python
Python压缩模块zipfile实现原理及用法解析
Aug 14 Python
python绘图模块之利用turtle画图
Feb 12 Python
python引入其他文件夹下的py文件具体方法
May 23 Python
Python基础之函数嵌套知识总结
May 23 Python
Python音乐爬虫完美绕过反爬
Aug 30 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通过session防url攻击方法
2014/12/10 PHP
php实现可用于mysql,mssql,pg数据库操作类
2014/12/13 PHP
php安装swoole扩展的方法
2015/03/19 PHP
PHP使用Mysqli类库实现完美分页效果的方法
2016/04/07 PHP
php 截取中英文混合字符串的方法
2018/05/31 PHP
jQuery示例收集
2010/11/05 Javascript
jquery时间下拉框小例子
2013/04/15 Javascript
浅谈javascript对象模型和function对象
2014/12/26 Javascript
原生js实现日期联动
2015/01/12 Javascript
后台获取ZTREE选中节点的方法
2015/02/12 Javascript
JS的框架Polymer中的dom-if和is属性使用说明
2015/07/29 Javascript
JS实现新浪博客左侧的Blog管理菜单效果代码
2015/10/22 Javascript
学习javascript面向对象 理解javascript原型和原型链
2016/01/04 Javascript
nodeJs爬虫获取数据简单实现代码
2016/03/29 NodeJs
jquery插件格式实例分析
2016/06/16 Javascript
Vue.js学习笔记之 helloworld
2016/08/14 Javascript
node.js利用mongoose获取mongodb数据的格式化问题详解
2017/10/06 Javascript
使用ionic(选项卡栏tab) icon(图标) ionic上拉菜单(ActionSheet) 实现通讯录界面切换实例代码
2017/10/20 Javascript
详解如何在vue-cli中使用vuex
2018/08/07 Javascript
vue使用openlayers实现移动点动画
2020/09/24 Javascript
python检测lvs real server状态
2014/01/22 Python
python进阶教程之函数参数的多种传递方法
2014/08/30 Python
Python中的字符串类型基本知识学习教程
2016/02/04 Python
python实现员工管理系统
2018/01/11 Python
使用Python制作微信跳一跳辅助
2018/01/31 Python
python 拷贝特定后缀名文件,并保留原始目录结构的实例
2018/04/27 Python
解决tensorflow训练时内存持续增加并占满的问题
2020/01/19 Python
基于Numba提高python运行效率过程解析
2020/03/02 Python
学校卫生检查制度
2014/02/03 职场文书
技校毕业生自荐书
2014/05/23 职场文书
党员十八大心得体会
2014/09/12 职场文书
云冈石窟导游词
2015/02/04 职场文书
详解TypeScript中的类型保护
2021/04/29 Javascript
python库sklearn常用操作
2021/08/23 Python
MySQL学习之基础操作总结
2022/03/19 MySQL
使用opencv-python如何打开USB或者笔记本前置摄像头
2022/06/21 Python