有没有办法搜索特定用户的所有GitLab组和相关项目(我拥有或管理)(目的是删除该用户)?我知道如果他们是小组成员本身,但如果小组有很多项目,有些项目单独添加用户,那么逐个搜索每个项目是很乏味的。
答案 0 :(得分:1)
我在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)
Project "MyGroupEggs / my-project-baz" (id=11111113) : delete johndoe123 (id=3333333)
2 members deleted in 2 repositories
以防万一,我在此处提供了脚本的完整来源:
#!/usr/bin/env python3
"""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 main():
# Greeting and args parsing.
args = parse_args()
# Initialize Gitlab API.
print(f'Auth at {args.url}')
gl = gitlab.Gitlab(args.url, private_token=args.token)
gl.auth()
# 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', end='')
else:
del_projects_count += 1
for m in members:
print(f' delete {m.username} (id={m.id})', end='')
del_members_count += 1
if not args.dry:
m.delete()
pass
print()
# 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()
-从项目中删除成员答案 1 :(得分:0)
如果该组有很多项目,而某些项目已经单独添加了用户,则逐个搜索每个项目是很繁琐的。
然而,考虑到脚本易于编写,这将是一个选项:
如果您是管理员,还可以lists the project of a User
通过API从项目中移除用户requested in issue 7687和resolved in GitLAb 8.7。
DEL /projects/#{project.id}/members/#{user.id}