python学习:python-gitlab 包

用 python 来操作 gitlab 相关的功能
更新于: 2022-06-15 15:23:58

安装

pip install --upgrade python-gitlab

基本用法

import gitlab
import os

gl = gitlab.Gitlab()

# private token or personal token authentication (GitLab.com)
gl = gitlab.Gitlab(url="https://git.saybot.net/",
                   private_token=os.getenv('GITLAB_TOKEN'))

prj = gl.projects.get(2563)

variables = prj.variables.list()

for vitem in variables:
    print(vitem.key, vitem.value)

常用功能

import gitlab
import os

# private token or personal token authentication (GitLab.com)
gl = gitlab.Gitlab(url="https://git.saybot.net/",
                   private_token=os.getenv('GITLAB_TOKEN'))

prj = gl.projects.get(2563) # 2563为项目id
功能代码
用配置来登录
import gitlab
import os

gl = gitlab.Gitlab.from_config(
    'saybot', [os.get_env('HOME') + '/.python-gitlab.cfg'])

prj = gl.projects.get(2563)

print(prj.variables.list())
列出项目变量
variables = prj.variables.list()
创建变量
prj.variables.create(
    {'key': 'AWS_ACCESS_KEY_ID', 'value': nx.get_env('AWS_ACCESS_KEY_ID')})
删除变量
for vitem in variables:
    vitem.delete()
添加变量
aws_settings_from_env = ['AWS_ACCESS_KEY_ID',
                         'AWS_SECRET_ACCESS_KEY', 'AWS_DEFAULT_REGION']

for key in aws_settings_from_env:
    prj.variables.create({'key': key, 'value': nx.get_env(key),
          'protected': True, 'masked': True})

print(prj.variables.list())

全局配置

  • 位置在这里:~/.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'])

对应 nodejs 的实现

https://github.com/jdalrymple/gitbeaker

参考