#!/bin/python3
__autor__ = "chunk"
__date__ = "2022-05-10"
import gitlab
import logging
import sys
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("debug.log"),
logging.StreamHandler(sys.stdout)
]
)
logging.debug('This message is skipped as a level is set as INFO')
logging.info('So should this')
logging.warning('And this, too')
logging.error('Testing non-ASCII character, Ø and ö')
url = 'https://gitlab.abc.com/' # gitlab安装地址
private_token = '123456' # gitlab 登录密钥 需自己设置
# 登录 获取gitlab操作对象gl
gl = gitlab.Gitlab(url, private_token)
# 获取第一页的用户列表 (返回[<User id:1>,<User id:2>])
# users = gl.users.list()
# log.info(users)
# 获取所有的用户列表
users = gl.users.list(all=True)
for a in users:
if a.id != 1 and a.id != 2 and a.id != 23 and a.id !=13 and a.id !=4:
# 修改用户属性权限
logging.info("正在处理当前用户: {user}".format(user=a.username))
a.can_create_group = False
a.projects_limit = 0
a.save()
logging.info("用户名:{a} ,名字: {b}, ID: {c}, projects limit: {d},允许创建组: {e}".format(a=a.username,b=a.name,c=a.id,d=a.projects_limit,e=a.can_create_group))
# print(a.username)
# print(a.name)
# print(a.id)
# print(a.projects_limit)
# print(a.can_create_group)
# print(users)
# log.info(users)
# 创建用户
"""
常用参数:以下参数都可以使用user对象进行修改
email (必填)-电子邮件
username (必填)-用户名
name (必填)-名称
password (可选)-密码
can_create_group (可选)-用户可以创建组-正确或错误
skip_confirmation (可选)-跳过确认-正确或错误(默认)
external (可选)-将用户标记为外部用户-true或false(默认)
"""
# user = gl.users.create({'email': 'qinsh@qq.com',
# 'password': 'qinsh123456',
# 'username': 'qinsh',
# 'name': '秦始皇'})
# 根据用户ID获取用户对象
# user = gl.users.get(1)
# 根据用户Git账号获取用户对象
# user = gl.users.list(username="qinsh")[0]
# # 根据用户对象可以进行如下操作
# username = user.username # 获取用户Git账号
# name = user.name # 获取用户姓名
# user_id = user.id # 获取用户ID
# # 修改用户属性
# user.external = True # 将用户标记为外部用户
# user.save()
# # 删除用户
# gl.users.delete(user_id) # 根据用户ID进行删除
# user.delete() # 根据用户对象直接进行删除