Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions team-mapping-gitlab-gitguardian/sync_gitlab.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from collections import defaultdict
from typing import Iterable
from typing import Iterable, Literal

from pygitguardian.models import (
CreateTeam,
Expand Down Expand Up @@ -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[get_team_name_from_gitlab_group(group)].add(user["email"])

return group_members

Expand Down Expand Up @@ -315,18 +315,21 @@ def create_team_from_group(group: GitlabGroup) -> Team:
"""
Given a Gitlab group, create the corresponding GitGuardian team
"""

team_name = get_team_name_from_gitlab_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


Expand All @@ -335,21 +338,29 @@ 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 = get_team_name_from_gitlab_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} to {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 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_or_dict["fullPath"]

def synchronize_teams(
teams_by_external_id: dict[str, Team],
Expand Down Expand Up @@ -384,7 +395,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 != 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())
Expand Down