-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Integration][Gitlab] Added support for gitlab member ingestion (#767)
- Loading branch information
Showing
14 changed files
with
525 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
51 changes: 30 additions & 21 deletions
51
integrations/gitlab/gitlab_integration/events/hooks/group.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,48 @@ | ||
from typing import Any | ||
from typing import Any, Optional | ||
|
||
from loguru import logger | ||
|
||
from gitlab_integration.events.hooks.base import HookHandler | ||
from gitlab_integration.utils import ObjectKind | ||
from gitlab_integration.events.hooks.base import GroupHandler | ||
from port_ocean.context.ocean import ocean | ||
from gitlab.v4.objects import Group | ||
|
||
|
||
class GroupHook(HookHandler): | ||
class Groups(GroupHandler): | ||
events = ["Subgroup Hook"] | ||
system_events = [ | ||
"group_destroy", | ||
"group_create", | ||
"group_rename", | ||
] | ||
system_events = ["group_destroy", "group_create", "group_rename"] | ||
|
||
async def on_hook(self, event: str, body: dict[str, Any]) -> None: | ||
event_name = body["event_name"] | ||
|
||
logger.info(f"Handling {event_name} for {event}") | ||
|
||
group_id = body["group_id"] if "group_id" in body else body["group"]["id"] | ||
|
||
logger.info(f"Handling hook {event} for group {group_id}") | ||
|
||
group = await self.gitlab_service.get_group(group_id) | ||
async def _on_hook( | ||
self, body: dict[str, Any], gitlab_group: Optional[Group] | ||
) -> None: | ||
|
||
group_id = body.get("group_id") | ||
group_full_path = body.get("full_path") | ||
if group: | ||
await ocean.register_raw(ObjectKind.GROUP, [group.asdict()]) | ||
event_name = body["event_name"] | ||
|
||
logger.info( | ||
f"Handling event '{event_name}' for group with ID '{group_id}' and full path '{group_full_path}'" | ||
) | ||
if gitlab_group: | ||
await self._register_group( | ||
ObjectKind.GROUP, | ||
gitlab_group.asdict(), | ||
) | ||
await self._register_object_with_members( | ||
ObjectKind.GROUPWITHMEMBERS, gitlab_group | ||
) | ||
logger.info(f"Registered group {group_id}") | ||
elif ( | ||
group_full_path | ||
and self.gitlab_service.should_run_for_path(group_full_path) | ||
and 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 {group_id}") | ||
return | ||
|
||
else: | ||
logger.info(f"Group {group_id} was filtered for event {event}. Skipping...") | ||
logger.info( | ||
f"Group {group_id} was filtered for event {event_name}. Skipping..." | ||
) |
29 changes: 29 additions & 0 deletions
29
integrations/gitlab/gitlab_integration/events/hooks/members.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Any, Optional | ||
from loguru import logger | ||
|
||
from gitlab_integration.utils import ObjectKind | ||
from gitlab_integration.events.hooks.base import GroupHandler | ||
from gitlab.v4.objects import Group | ||
|
||
|
||
class Members(GroupHandler): | ||
events = ["Member Hook"] | ||
system_events = [ | ||
"user_remove_from_group", | ||
"user_update_for_group", | ||
"user_add_to_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_object_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..." | ||
) |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.