diff --git a/integrations/gitlab/gitlab_integration/events/hooks/base.py b/integrations/gitlab/gitlab_integration/events/hooks/base.py index 66a2c1d9a0..daca370827 100644 --- a/integrations/gitlab/gitlab_integration/events/hooks/base.py +++ b/integrations/gitlab/gitlab_integration/events/hooks/base.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import List, Any, Dict +from typing import List, Any, Dict, Optional import typing from loguru import logger from gitlab.v4.objects import Project, Group @@ -66,12 +66,13 @@ async def on_hook(self, event: str, body: dict[str, Any]) -> None: logger.info(f"Finished handling {event} for group {group_path}") @abstractmethod - async def _on_hook(self, body: dict[str, Any], gitlab_group: Group) -> None: + async def _on_hook( + self, body: dict[str, Any], gitlab_group: Optional[Group] + ) -> None: pass async def _register_group(self, kind: str, gitlab_group: Dict[str, Any]) -> None: - if self.gitlab_service.should_run_for_path(gitlab_group["full_path"]): - await ocean.register_raw(kind, [gitlab_group]) + await ocean.register_raw(kind, [gitlab_group]) async def _register_group_with_members( self, kind: str, gitlab_group: Group diff --git a/integrations/gitlab/gitlab_integration/events/hooks/group.py b/integrations/gitlab/gitlab_integration/events/hooks/group.py index 086759b529..cf8c3305d0 100644 --- a/integrations/gitlab/gitlab_integration/events/hooks/group.py +++ b/integrations/gitlab/gitlab_integration/events/hooks/group.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, Optional from loguru import logger @@ -12,20 +12,32 @@ class Groups(GroupHandler): events = ["Subgroup Hook"] system_events = ["group_destroy", "group_create", "group_rename"] - async def _on_hook(self, body: dict[str, Any], gitlab_group: Group) -> None: + async def _on_hook( + self, body: dict[str, Any], gitlab_group: Optional[Group] + ) -> None: logger.info(f"Handling {body['event_name']} for group {body['group_id']}") - if body["event_name"] in ("subgroup_destroy", "group_destroy"): + group_full_path = body.get("full_path") + if gitlab_group: + await self._register_group( + ObjectKind.GROUP, + gitlab_group.asdict(), + ) + await self._register_group_with_members( + ObjectKind.GROUPWITHMEMBERS, gitlab_group + ) + logger.info(f"Registered group {body['group_id']}") + elif ( + group_full_path + and self.gitlab_service.should_run_for_path(group_full_path) + and body["event_name"] in ("subgroup_destroy", "group_destroy") + ): await ocean.unregister_raw(ObjectKind.GROUP, [body]) await ocean.unregister_raw(ObjectKind.GROUPWITHMEMBERS, [body]) logger.info(f"Unregistered group {body['group_id']}") return - await self._register_group( - ObjectKind.GROUP, - gitlab_group.asdict(), - ) - await self._register_group_with_members( - ObjectKind.GROUPWITHMEMBERS, gitlab_group - ) - logger.info(f"Registered group {body['group_id']}") + else: + logger.info( + f"Group {body['group_id']} was filtered for event {body['event_name']}. Skipping..." + ) diff --git a/integrations/gitlab/gitlab_integration/events/hooks/members.py b/integrations/gitlab/gitlab_integration/events/hooks/members.py index f7ab1b3e8b..6e699132bc 100644 --- a/integrations/gitlab/gitlab_integration/events/hooks/members.py +++ b/integrations/gitlab/gitlab_integration/events/hooks/members.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, Optional from loguru import logger from gitlab_integration.utils import ObjectKind @@ -14,9 +14,16 @@ class Members(GroupHandler): "user_add_to_group", ] - async def _on_hook(self, body: dict[str, Any], gitlab_group: Group) -> None: - event_name, user_username = (body["event_name"], body["user_username"]) - logger.info(f"Handling {event_name} for group member {user_username}") - await self._register_group_with_members( - ObjectKind.GROUPWITHMEMBERS, gitlab_group - ) + async def _on_hook( + self, body: dict[str, Any], gitlab_group: Optional[Group] + ) -> None: + if gitlab_group: + event_name, user_username = (body["event_name"], body["user_username"]) + logger.info(f"Handling {event_name} for group member {user_username}") + await self._register_group_with_members( + ObjectKind.GROUPWITHMEMBERS, gitlab_group + ) + else: + logger.info( + f"Group member's group {body['group_id']} was filtered for event {body['event_name']}. Skipping..." + ) diff --git a/integrations/gitlab/gitlab_integration/gitlab_service.py b/integrations/gitlab/gitlab_integration/gitlab_service.py index 44a6237406..ca12783adb 100644 --- a/integrations/gitlab/gitlab_integration/gitlab_service.py +++ b/integrations/gitlab/gitlab_integration/gitlab_service.py @@ -455,13 +455,13 @@ async def get_project(self, project_id: int) -> Project | None: else: return None - async def get_group(self, group_id: int) -> Group: - logger.info(f"Fetching group with ID: {group_id}") - group_response = await AsyncFetcher.fetch_single( - self.gitlab_client.groups.get, group_id - ) - group: Group = typing.cast(Group, group_response) - return group + async def get_group(self, group_id: int) -> Group | None: + logger.info(f"fetching group {group_id}") + group = await AsyncFetcher.fetch_single(self.gitlab_client.groups.get, group_id) + if isinstance(group, Group) and self.should_run_for_group(group): + return group + else: + return None @cache_iterator_result() async def get_all_groups( diff --git a/integrations/gitlab/gitlab_integration/ocean.py b/integrations/gitlab/gitlab_integration/ocean.py index 4daca5769c..c6be6a745a 100644 --- a/integrations/gitlab/gitlab_integration/ocean.py +++ b/integrations/gitlab/gitlab_integration/ocean.py @@ -130,8 +130,6 @@ async def resync_groups(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE: yield [group.asdict() for group in groups_batch] -# from memory_profiler import profile -# @profile @ocean.on_resync(ObjectKind.GROUPWITHMEMBERS) async def resync_groups_with_members(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE: gitlab_resource_config: GitlabGroupWithMembersResourceConfig = typing.cast(