日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python gitlab_Python Gitlab Api 使用方法

發布時間:2023/12/2 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python gitlab_Python Gitlab Api 使用方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡述

公司使用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 gitlab_Python Gitlab Api 使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。