gitlab - 从所有 GitLab 项目(我拥有的)中查找(并删除)成员
问题描述
有没有办法搜索特定用户的所有 GitLab 组和相关项目(我拥有或管理的)(目的是删除该用户)?我知道如果他们是组本身的成员,但是如果组有很多项目并且某些项目有单独添加的用户,那么逐个搜索每个项目是很乏味的。
解决方案
我在 GitLab UI 中没有找到这样的功能,所以我使用GitLab API开发了 python 脚本。
这是我的脚本和文档的源代码的回购链接:https ://gitlab.com/CVisionLab/gitlab-tools/
只需下载gitlab-delete-members.py
脚本并在 bash 命令行中运行。使用示例:
$ gitlab-delete-members.py --query johndoe123 --token Abcdefg123
Auth at https://gitlab.com
Project "MyGroupSpam / my-project-foo" (id=11111111) : no users to delete
Project "MyGroupSpam / my-project-bar" (id=11111112) : delete johndoe123 (id=3333333) : ok
Project "NotMyGroupEggs / not-my-project-baz" (id=11111113) : delete johndoe123 (id=3333333) : fail
1 members deleted in 2 repositories
以防万一我在这里提供脚本的完整来源:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Handy script for searching and deleting members from Gitlab projects.
Built on top of GitLab Python API: https://python-gitlab.readthedocs.io/
"""
import argparse
import gitlab
def parse_args():
parser = argparse.ArgumentParser(
description='Delete member from all accessible gitlab projects.')
parser.add_argument('--query', required=True,
help='Gitlab query string that defines users '
'to be deleted (username is recommended)')
parser.add_argument('--token', required=True,
help='Gitlab token of your user')
parser.add_argument('--url', default='https://gitlab.com',
help='Gitlab URL')
parser.add_argument('--visibility', default='private',
help='Gitlab projects visibility')
parser.add_argument('--dry', action='store_true',
help='dry run')
return parser.parse_args()
def print_ok():
print(' ok')
def print_fail():
print(' fail')
def print_skip():
print(' skip')
def main():
# Greeting and args parsing.
args = parse_args()
# Initialize Gitlab API.
print(f' Auth to {args.url} : ', end='')
gl = gitlab.Gitlab(args.url, private_token=args.token)
try:
gl.auth()
print_ok()
except: # noqa
print_fail()
return
# Iterate over projects.
projects = gl.projects.list(all=True, visibility=args.visibility)
del_members_count = 0
del_projects_count = 0
for p in projects:
print(f'Project "{p.name_with_namespace}" (id={p.id}) :',
end='')
# Query members.
members = p.members.list(query=args.query)
# Delete members.
if len(members) == 0:
print(' no users to delete')
else:
del_projects_count += 1
for m in members:
print(f' delete {m.username} (id={m.id}) : ', end='')
if not args.dry:
try:
m.delete()
print_ok()
del_members_count += 1
except: # noqa
print_fail()
else:
print_skip()
# Statistics.
print(f' {del_members_count} members deleted '
f'in {del_projects_count} repositories')
if __name__ == '__main__':
main()
如果您想知道使用了哪些 GitLab API 函数,以下是简短列表:
gl = gitlab.Gitlab(...)
– 创建 API 对象gl.auth()
- 验证projects = gl.projects.list(...)
- 获取项目列表members = p.members.list(...)
- 列出与查询匹配的某个项目的成员m.delete()
- 从项目中删除成员
推荐阅读
- laravel - 使用 sanctum 的 SPA 身份验证失败,并显示以下错误消息:“未经身份验证”。
- java - 期待 IDENT,找到 ':'
- python - 在 Jupyter 砍属性
- javascript - 单击图库中的缩略图时显示 Swiper 滑块
- swift - 如何改变从其他视图传递的变量
- encryption - PGP加密说明
- python - ModuleNotFoundError:使用自然语言时没有名为“google”的模块
- parsing - 使用 Pest.rs,有没有办法将评论存储为令牌?
- linux - 构建 rpm - glob 找不到文件:
- amazon-web-services - Boto3:调用 client.associate_iam_instance_profile 时 IAM 实例配置文件 ARN 无效