Python Gitlab Api 使用方法
作者:Op小剑 时间:2021-03-11 16:30:11
简述
公司使用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 的脚本操作或者是命令行操作。
来源:https://blog.csdn.net/xie_0723/article/details/75215869
标签:Python,Gitlab,Api
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python openCV自制绘画板
2022-05-17 23:45:51
![](https://img.aspxhome.com/file/2023/3/86373_0s.jpg)
ASP.NET对路径"xxxxx"的访问被拒绝的解决方法小结
2023-07-07 20:08:41
PyCharm+Miniconda3安装配置教程详解
2022-03-26 08:31:16
![](https://img.aspxhome.com/file/2023/7/79627_0s.png)
Python Socketserver实现FTP文件上传下载代码实例
2022-10-10 11:03:04
![](https://img.aspxhome.com/file/2023/6/85226_0s.png)
Python3 使用selenium插件爬取苏宁商家联系电话
2023-12-20 01:39:57
Python 注解方式实现缓存数据详解
2023-07-15 22:11:22
彻底解决MySql在UTF8字符集下乱码问题
2011-06-02 12:02:00
python中关于CIFAR10数据集的使用
2021-04-14 22:08:05
![](https://img.aspxhome.com/file/2023/7/105757_0s.jpg)
解决python虚拟环境切换无效的问题
2023-02-01 14:37:50
![](https://img.aspxhome.com/file/2023/0/77860_0s.jpg)
Python判断字符串是否为字母或者数字(浮点数)的多种方法
2023-04-12 07:55:47
解决每次打开pycharm直接进入项目的问题
2021-05-08 00:27:45
![](https://img.aspxhome.com/file/2023/2/115732_0s.jpg)
去掉CSS赘余代码,CSS可以更简洁
2008-11-05 13:07:00
OpenCV+face++实现实时人脸识别解锁功能
2023-03-17 20:10:19
![](https://img.aspxhome.com/file/2023/6/93866_0s.jpg)
Pycharm 操作Django Model的简单运用方法
2022-05-09 14:07:15
![](https://img.aspxhome.com/file/2023/9/110259_0s.jpg)
OpenCV图像缩放resize各种插值方式的比较实现
2023-03-18 13:31:45
![](https://img.aspxhome.com/file/2023/0/92070_0s.png)
python中pandas操作apply返回多列的实现
2023-03-04 06:46:44
python 的 scapy库,实现网卡收发包的例子
2021-08-08 17:25:05
PHP join()函数用法与实例讲解
2023-06-02 00:39:19
![](https://img.aspxhome.com/file/2023/8/55378_0s.png)
opencv调用yolov3模型深度学习目标检测实例详解
2022-04-30 19:37:55
![](https://img.aspxhome.com/file/2023/1/95201_0s.jpg)
Pandas数据处理加速技巧汇总
2023-08-12 19:02:36
![](https://img.aspxhome.com/file/2023/8/105268_0s.png)