安装
pip install python-gitlab
示例程序
# -*- coding: utf-8 -*-
"""
gitlab 经常使用到的api
DOC_URL: http://python-gitlab.readthedocs.io/en/stable/
"""
import gitlab
# 配置gitlab地址和token
url = 'http://10.253.84.9:31000'
token = 'C-C-cu9cdcFRAx9-P3Zm-'
# 登录
gl = gitlab.Gitlab(url, token)
# 获取所有project的name,id
dic = {}
for g in gl.groups.list(all=True):
for p in g.projects.list(all=True):
print("group: %s, project,id: %s,%s" % (g.name, p.name, p.id))
project = gl.projects.get(p.id)
branches = []
for b in project.branches.list():
branches.append(b.name)
print("project branches: ", branches)
k = str(g.name) + '/' + str(p.name)
if k not in dic:
dic[k] = branches
# 保存至.json
import json
json_str = json.dumps(dic)
with open('group_project_branch.json', 'w') as json_file:
json_file.write(json_str)