Last active
October 15, 2025 10:13
-
-
Save svcavallar/b6ae9169479684d7ca9f2a82e5946c05 to your computer and use it in GitHub Desktop.
Retrieve Gitlab users of all repositories under a group
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| from dotenv import load_dotenv | |
| import os | |
| import gitlab | |
| load_dotenv() | |
| # you can find the group ID right on the home page of your group, just underneath the name of your group | |
| group_id = 123456789 | |
| # the translation of the access level codes according to Gitlab: https://docs.gitlab.com/ee/api/members.html | |
| access_dict = { | |
| 0: "no access", | |
| 5: "minimal access", | |
| 10: "guest", | |
| 20: "reporter", | |
| 30: "developer", | |
| 40: "maintainer", | |
| 50: "owner", | |
| } | |
| # | |
| # Initialize the Gitlab API and start listing all information from within our group, getting a list of all projects (repositories) | |
| # in the group and all of its subgroups. | |
| # | |
| # init the gitlab object | |
| gl = gitlab.Gitlab(private_token=os.getenv("GITLAB_PRIVATE_TOKEN")) | |
| # get gitlab group | |
| group = gl.groups.get(group_id, lazy=True) | |
| # get all projects | |
| projects = group.projects.list(include_subgroups=True, all=True, owned=True) | |
| # get all project ids | |
| project_ids = [] | |
| for project in projects: | |
| project_ids.append((project.id, project.path_with_namespace, project.name)) | |
| df_project = pd.DataFrame(project_ids, columns=["id", "path", "name"]) | |
| # | |
| # After retrieving all the projects, loop through and get all members of each group. | |
| # This way, we end up with a complete list with all members within our group, to which repositories they have | |
| # access and which access rights they have for each of these repositories. | |
| # | |
| # get all members | |
| members = [] | |
| for _, row in df_project.iterrows(): | |
| proj = gl.projects.get(row["id"], all=True) | |
| for member in proj.members_all.list(get_all=True): | |
| members.append( | |
| ( | |
| row["id"], | |
| row["path"], | |
| row["name"], | |
| member.username, | |
| member.state, | |
| member.access_level, | |
| ) | |
| ) | |
| df_members = pd.DataFrame( | |
| members, | |
| columns=[ | |
| "project_id", | |
| "project_path", | |
| "project_name", | |
| "username", | |
| "state", | |
| "access_level_code", | |
| ], | |
| ).drop_duplicates() | |
| df_members["access_level"] = df_members["access_level_code"].map(access_dict) | |
| df_members.sort_values("username", inplace=True) | |
| # store as csv | |
| df_members.to_csv("gitlab_members.csv", sep=";", header=True, index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment