如何在Gitlab中列出用户的公共组?

问题描述 投票:0回答:2

我想要 gitlab 中特定用户的所有公共组。有没有API或者其他方法可以实现。

https://gitlab.com/api/v4/groups
如果我在不提供任何身份验证令牌的情况下使用此 API,它会为我提供 GITLAB 中的所有公共组。但我的要求是获取特定用户的所有公共组(使用 user_id)。

api gitlab gitlab-api
2个回答
1
投票

没有直接的方法来列出用户的组。

最接近的方法是列出所有组,然后使用 memberships API 查看用户是否属于特定组。


0
投票

要获取 GitLab 中特定用户的所有公共组,您可以使用 GitLab API 的

users/:id/memberships
端点。这将返回一个字典列表,每个字典代表用户所属的组或项目。每个字典包含成员资格的
source_id
source_name
source_type
access_level
。 source_type 可以是
Project
Namespace
(代表一个组)。

以下是如何使用此 API 的示例:

import requests

def get_user_memberships(user_id, private_token):
    headers = {'PRIVATE-TOKEN': private_token}
    response = requests.get(f"https://gitlab.com/api/v4/users/{user_id}/memberships", headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        return None

# Replace 'user_id' and 'your_private_token' with the actual user ID and your private token
user_memberships = get_user_memberships('user_id', 'your_private_token')

如果您只想过滤公共群组,可以通过检查响应中每个成员资格的 source_type 属性来实现:

public_groups = [membership for membership in user_memberships if membership['source_type'] == 'Namespace']

请注意,只有当您在 GitLab 中拥有管理员访问权限时,这才有效。

© www.soinside.com 2019 - 2024. All rights reserved.