python操作gitlab API过程解析

这篇文章主要介绍了python操作gitlab API过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

使用 python-gitlab 模块来调用gitlab的API来管理gitlab
install

pip install python-gitlab
# 如果是安装到Python3使用可以使用如下命令
pip3 install python-gitlab

配置

为了保护API 用到的 private_token,一般会将其写到系统的配置文件中去
/etc/python-gitlab.cfg 或者 ~/.python-gitlab.cfg

配置示例

root@pts/1 $ cat ~/.python-gitlab.cfg
[global]
default = kaishugit
ssh_verify = False
timeout = 8
 
[kaishugit]
url = http://10.0.0.6
private_token = xxxxx-V4Yxxxxxxks7u
api_version = 3

程序使用

在程序中使用的时候可以直接用如下方式调用

## login
gl = gitlab.Gitlab.from_config('kaishugit', ['~/.python-gitlab.cfg'])

examples

## 得到第一页project列表
projects = gl.projects.list()
## 得到所有project
projects = gl.projects.list(all=True)
projects = gl.projects.all()

附加

自定义脚本获取指定用户或者分组或者全部的代码仓库地址

#!/usr/bin/env python3
# encoding: utf-8
#
 
__Author__ = 'Colin'
__Date__ = '2018-06-20'
 
import gitlab
 
import os
import sys
 
class GitlabAPI(object):
  def __init__(self, *args, **kwargs):
    if os.path.exists('/etc/python-gitlab.cfg'):
      self.gl = gitlab.Gitlab.from_config('kaishugit', ['/etc/python-gitlab.cfg'])
    elif os.path.exists(os.getenv('HOME') + '/.python-gitlab.cfg'):
      self.gl = gitlab.Gitlab.from_config('kaishugit', [os.getenv('HOME') + '/.python-gitlab.cfg'])
    else:
      print('You need to make sure there is a file named "/etc/python-gitlab.cfg" or "~/.python-gitlab.cfg"')
      sys.exit(5)
 
  def get_user_id(self, username):
    user = self.gl.users.get_by_username(username)
    return user.id
 
  def get_group_id(self, groupname):
    group = self.gl.users.search(groupname)
    return group[0].id
 
  def get_all_projects(self):
    projects = self.gl.projects.list(all=True)
    result_list = []
    for project in projects:
      result_list.append(project.http_url_to_repo)
    return result_list
 
  def get_user_projects(self, userid):
    projects = self.gl.projects.owned(userid=userid, all=True)
    result_list = []
    for project in projects:
      result_list.append(project.http_url_to_repo)
    return result_list
 
  def get_group_projects(self, groupname):
    projects = self.gl.projects.owned(groupname=groupname, all=True)
    result_list = []
    for project in projects:
      result_list.append(project.http_url_to_repo)
    return result_list
 
if __name__ == '__main__':
  username='caichenyu'
  git = GitlabAPI()
  userid = git.get_user_id(username)
  print(username + '->' + str(userid))
  userprojects = git.get_user_projects(userid)
  print(userprojects)

以上就是本文的全部内容,希望对大家的学习有所帮助

你可能感兴趣的:(python)