From 7115c77e37bac1ce28717ac6cf122777b7deb4a8 Mon Sep 17 00:00:00 2001 From: karreg Date: Thu, 25 Sep 2025 13:58:36 +0200 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=A9=B9=20move=20back=20to=20full=20pa?= =?UTF-8?q?th=20for=20naming=20teams=20without=20collision?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sync_gitlab.py | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/team-mapping-gitlab-gitguardian/sync_gitlab.py b/team-mapping-gitlab-gitguardian/sync_gitlab.py index 1b67839..f206c9e 100644 --- a/team-mapping-gitlab-gitguardian/sync_gitlab.py +++ b/team-mapping-gitlab-gitguardian/sync_gitlab.py @@ -88,7 +88,7 @@ def list_group_members( for user in gitlab_users: for group in user["groups"]: - group_members[group["fullName"]].add(user["email"]) + group_members[team_name_from_group_dict(group)].add(user["email"]) return group_members @@ -315,18 +315,21 @@ def create_team_from_group(group: GitlabGroup) -> Team: """ Given a Gitlab group, create the corresponding GitGuardian team """ + + team_name = team_name_from_group(group) + payload = CreateTeam( - group["fullName"], + team_name, description=team_description_from_group(group), ) response = CONFIG.client.create_team(payload) if isinstance(response, Detail): raise RuntimeError( - f"Unable to create team {group['fullName']}: {response.detail}" + f"Unable to create team {team_name}: {response.detail}" ) - logger.info(f"Successfully created team {group['fullName']}") + logger.info(f"Successfully created team {team_name}") return response @@ -335,21 +338,35 @@ def rename_team_from_group(team: Team, group: GitlabGroup) -> Team: Given a GitGuardian team and a Gitlab group, rename the GitGuardian team to match the Gitlab group full path """ + + team_name = team_name_from_group(group) + payload = UpdateTeam( team.id, - name=group["fullName"], + name=team_name, description=team_description_from_group(group), ) response = CONFIG.client.update_team(payload) if isinstance(response, Detail): raise RuntimeError( - f"Unable to rename team {group['fullName']}: {response.detail}" + f"Unable to rename team {team.name}: {response.detail}" ) - logger.info(f"Successfully renamed team from {team.name} to {group['fullName']}") + logger.info(f"Successfully renamed team from {team.name} to {team_name}") return response +def team_name_from_group(group: GitlabGroup) -> str: + """ + Given a Gitlab group, return the corresponding GitGuardian team name + """ + return group["fullPath"] + +def team_name_from_group_dict(group: dict[str, str]) -> str: + """ + Given a Gitlab group, return the corresponding GitGuardian team name + """ + return group["fullPath"] def synchronize_teams( teams_by_external_id: dict[str, Team], @@ -384,7 +401,7 @@ def synchronize_teams( gg_team = teams_by_external_id[team] group = groups_by_id[team] - if gg_team.name != group["fullName"]: + if gg_team.name != team_name_from_group(group): teams_by_external_id[group["id"]] = rename_team_from_group(gg_team, group) return list(teams_by_external_id.values()) From 33254440474153ec4b49e95ee7760dc594d9c8c7 Mon Sep 17 00:00:00 2001 From: karreg Date: Thu, 25 Sep 2025 14:38:00 +0200 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=A9=B9=20improve=20error=20message=20?= =?UTF-8?q?when=20renaming=20group=20fails?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- team-mapping-gitlab-gitguardian/sync_gitlab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/team-mapping-gitlab-gitguardian/sync_gitlab.py b/team-mapping-gitlab-gitguardian/sync_gitlab.py index f206c9e..1634961 100644 --- a/team-mapping-gitlab-gitguardian/sync_gitlab.py +++ b/team-mapping-gitlab-gitguardian/sync_gitlab.py @@ -350,7 +350,7 @@ def rename_team_from_group(team: Team, group: GitlabGroup) -> Team: if isinstance(response, Detail): raise RuntimeError( - f"Unable to rename team {team.name}: {response.detail}" + f"Unable to rename team {team.name} to {team_name}: {response.detail}" ) logger.info(f"Successfully renamed team from {team.name} to {team_name}") From dfe760860ce5514ab6f184d653a7a4cd24728514 Mon Sep 17 00:00:00 2001 From: karreg Date: Mon, 6 Oct 2025 11:31:51 +0200 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=8D=EF=B8=8F=20avoid=20code=20duplica?= =?UTF-8?q?tion=20in=20team=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sync_gitlab.py | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/team-mapping-gitlab-gitguardian/sync_gitlab.py b/team-mapping-gitlab-gitguardian/sync_gitlab.py index 1634961..ec492c2 100644 --- a/team-mapping-gitlab-gitguardian/sync_gitlab.py +++ b/team-mapping-gitlab-gitguardian/sync_gitlab.py @@ -1,6 +1,6 @@ import logging from collections import defaultdict -from typing import Iterable +from typing import Iterable, Literal from pygitguardian.models import ( CreateTeam, @@ -88,7 +88,7 @@ def list_group_members( for user in gitlab_users: for group in user["groups"]: - group_members[team_name_from_group_dict(group)].add(user["email"]) + group_members[get_team_name_from_gitlab_group(group)].add(user["email"]) return group_members @@ -316,7 +316,7 @@ def create_team_from_group(group: GitlabGroup) -> Team: Given a Gitlab group, create the corresponding GitGuardian team """ - team_name = team_name_from_group(group) + team_name = get_team_name_from_gitlab_group(group) payload = CreateTeam( team_name, @@ -339,7 +339,7 @@ def rename_team_from_group(team: Team, group: GitlabGroup) -> Team: match the Gitlab group full path """ - team_name = team_name_from_group(group) + team_name = get_team_name_from_gitlab_group(group) payload = UpdateTeam( team.id, @@ -356,17 +356,11 @@ def rename_team_from_group(team: Team, group: GitlabGroup) -> Team: logger.info(f"Successfully renamed team from {team.name} to {team_name}") return response -def team_name_from_group(group: GitlabGroup) -> str: +def get_team_name_from_gitlab_group(group_or_dict: GitlabGroup | dict[Literal["fullPath"],str]) -> str: """ Given a Gitlab group, return the corresponding GitGuardian team name """ - return group["fullPath"] - -def team_name_from_group_dict(group: dict[str, str]) -> str: - """ - Given a Gitlab group, return the corresponding GitGuardian team name - """ - return group["fullPath"] + return group_or_dict["fullPath"] def synchronize_teams( teams_by_external_id: dict[str, Team], @@ -401,7 +395,7 @@ def synchronize_teams( gg_team = teams_by_external_id[team] group = groups_by_id[team] - if gg_team.name != team_name_from_group(group): + if gg_team.name != get_team_name_from_gitlab_group(group): teams_by_external_id[group["id"]] = rename_team_from_group(gg_team, group) return list(teams_by_external_id.values())