Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tankilevitch committed Sep 20, 2024
1 parent 58e0838 commit dd19227
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 123 deletions.
14 changes: 8 additions & 6 deletions integrations/gitlab/gitlab_integration/events/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,20 @@ def on(self, events: list[str], observer: Observer) -> None:
self._observers[event].append(observer)

async def _notify(self, event_id: str, body: dict[str, Any]) -> None:
observers = asyncio.gather(
observers_list = self._observers.get(event_id, [])
if not observers_list:
logger.info(
f"event: {event_id} has no matching handler. the handlers available are for events: {self._observers.keys()}"
)
return

await asyncio.gather(
*(
observer(event_id, body)
for observer in self._observers.get(event_id, [])
)
)

if not observers:
logger.info(
f"event: {event_id} has no matching handler. the handlers available are for events: {self._observers.keys()}"
)


class SystemEventHandler(BaseEventHandler):
def __init__(self) -> None:
Expand Down
19 changes: 13 additions & 6 deletions integrations/gitlab/gitlab_integration/events/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def setup_system_listeners(gitlab_clients: list[GitlabService]) -> None:
system_event_handler.add_client(gitlab_service)


def create_webhooks_by_client(
async def create_webhooks_by_client(
gitlab_host: str,
app_host: str,
token: str,
Expand All @@ -160,16 +160,20 @@ def create_webhooks_by_client(
gitlab_client = Gitlab(gitlab_host, token)
gitlab_service = GitlabService(gitlab_client, app_host, group_mapping)

groups_for_webhooks = gitlab_service.get_filtered_groups_for_webhooks(
groups_for_webhooks = await gitlab_service.get_filtered_groups_for_webhooks(
list(groups_hooks_events_override.keys())
if groups_hooks_events_override
else None
)

webhooks_ids: list[str] = []

all_groups = []
async for groups_batch in gitlab_service.get_all_groups():
all_groups.extend(groups_batch)

for group in groups_for_webhooks:
webhook_id = gitlab_service.create_webhook(
webhook_id = await gitlab_service.create_webhook(
group,
(
groups_hooks_events_override.get(
Expand All @@ -186,7 +190,7 @@ def create_webhooks_by_client(
return gitlab_service, webhooks_ids


def setup_application(
async def setup_application(
token_mapping: dict[str, list[str]],
gitlab_host: str,
app_host: str,
Expand All @@ -196,13 +200,15 @@ def setup_application(
validate_token_mapping(token_mapping)

if use_system_hook:
logger.info("Using system hook")
validate_use_system_hook(token_mapping)
token, group_mapping = list(token_mapping.items())[0]
gitlab_client = Gitlab(gitlab_host, token)
gitlab_service = GitlabService(gitlab_client, app_host, group_mapping)
setup_system_listeners([gitlab_service])

else:
logger.info("Using group hooks")
validate_hooks_override_config(
token_mapping, token_group_override_hooks_mapping
)
Expand All @@ -212,7 +218,7 @@ def setup_application(
for token, group_mapping in token_mapping.items():
if not token_group_override_hooks_mapping:
client_to_webhooks.append(
create_webhooks_by_client(
await create_webhooks_by_client(
gitlab_host,
app_host,
token,
Expand All @@ -221,12 +227,13 @@ def setup_application(
)
)
else:
logger.info("Using token group hooks override mapping")
groups = token_group_override_hooks_mapping.tokens.get(
token, WebhookTokenConfig(groups=[])
).groups
if groups:
client_to_webhooks.append(
create_webhooks_by_client(
await create_webhooks_by_client(
gitlab_host,
app_host,
token,
Expand Down
Loading

0 comments on commit dd19227

Please sign in to comment.