如何通过 python-gitlab 使用 Gitlab API 查找 Gitlab 组 ID

install_python_gitlab.sh
pip install python-gitlab
get_gitlab_group_id.py
import gitlab

def get_gitlab_group_id(group_name, access_token):
    # Initialize a GitLab instance with your private token
    gl = gitlab.Gitlab('https://gitlab.com', private_token=access_token)

    # Search for groups by name
    groups = gl.groups.list(search=group_name)
    for group in groups:
        if group.name == group_name or group.path == group_name:
            return group.id

    raise ValueError("Group not found")

# Usage example
group_id = get_gitlab_group_id("Protectors of the Footprint Realm", 'glpat-yykIsrTg6RyKcFAvd2os')

group_id 将是一个整数,例如 6604163


Check out similar posts by category: Gitlab, Python