Skip to content

Commit

Permalink
revert get_group function, stick with optional response
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-armah committed Nov 8, 2024
1 parent 82bfc94 commit c81ab10
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
9 changes: 5 additions & 4 deletions integrations/gitlab/gitlab_integration/events/hooks/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
34 changes: 23 additions & 11 deletions integrations/gitlab/gitlab_integration/events/hooks/group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Optional

from loguru import logger

Expand All @@ -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..."
)
21 changes: 14 additions & 7 deletions integrations/gitlab/gitlab_integration/events/hooks/members.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Optional
from loguru import logger

from gitlab_integration.utils import ObjectKind
Expand All @@ -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..."
)
14 changes: 7 additions & 7 deletions integrations/gitlab/gitlab_integration/gitlab_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 0 additions & 2 deletions integrations/gitlab/gitlab_integration/ocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c81ab10

Please sign in to comment.