Skip to content

Commit

Permalink
ref(sync-gitlab): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-devv committed Nov 21, 2024
1 parent cf622e6 commit 6375f16
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Any, AsyncGenerator, Optional, Dict
from httpx import HTTPStatusError, HTTPError, Response
from loguru import logger
import re
from port_ocean.context.ocean import ocean
from port_ocean.context.event import event
from port_ocean.utils import http_async_client
Expand Down
26 changes: 7 additions & 19 deletions integrations/async-gitlab/gitlab/webhook_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from port_ocean.context.ocean import ocean
from port_ocean.context.event import event
from gitlab.helpers.utils import ObjectKind
from gitlab.client import GitLabClient
from gitlab.gitlab_client import GitLabClient


class WebhookHandler:
Expand Down Expand Up @@ -50,42 +50,30 @@ def create_from_ocean_config(cls) -> "WebhookHandler":
return webhook_client

def verify_token(self, token: str):
if token != self.webhook_secret:
return False
else:
return True
return token == self.webhook_secret

async def handle_event(self, payload: Dict[str, Any], is_system_hook: bool = False):
if is_system_hook:
event_type = payload.get("event_name")
event_type = payload.get("event_name")

if is_system_hook:
if event_type in self.system_actions:
project_id = payload.get("project_id")
group_id = payload.get("group_id")

if project_id:
payload.update({
'project': {
'id': project_id
}
})
payload["project"] = {"id": project_id}
await self._update_resource(ObjectKind.PROJECT, payload)
elif group_id:
payload.update({
'group': {
'id': group_id
}
})
payload["group"] = {"id": group_id}
await self._update_resource(ObjectKind.GROUP, payload)
else:
logger.warning(f"skipping event type: {event_type}, because it doesn't have a handler")
else:
logger.warning(f"skipping event type: {event_type}, because it doesn't have a handler")
else:
object_kind = payload.get("object_kind")
event_type = payload.get("event_name")

kind = self.event_handlers.get(object_kind)

if kind:
await self._update_resource(kind, payload)
else:
Expand Down
20 changes: 6 additions & 14 deletions integrations/async-gitlab/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi import Request
from port_ocean.context.ocean import ocean
from port_ocean.core.ocean_types import ASYNC_GENERATOR_RESYNC_TYPE
from gitlab.client import GitLabClient
from gitlab.gitlab_client import GitLabClient
from gitlab.webhook_handler import WebhookHandler
from gitlab.helpers.utils import ObjectKind, ResourceKindsHandledViaWebhooks

Expand All @@ -14,18 +14,15 @@ async def on_resources_resync(kind: str) -> None:
if kind == ObjectKind.PROJECT:
return

gitlab_client = GitLabClient.create_from_ocean_config()

async for resources in gitlab_client.get_resources(kind):
async for resources in GitLabClient.create_from_ocean_config().get_resources(kind):
logger.info(f"Re-syncing {len(resources)} {kind}")
yield resources

@ocean.on_resync(ObjectKind.PROJECT)
async def on_project_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
logger.info("Project Re-sync req received")
gitlab_client = GitLabClient.create_from_ocean_config()

async for projects in gitlab_client.get_resources(ObjectKind.PROJECT, {"owned": "yes"}):
async for projects in GitLabClient.create_from_ocean_config().get_resources(ObjectKind.PROJECT, {"owned": "yes"}):
logger.info(f"Re-syncing {len(projects)} projects")
yield projects

Expand All @@ -37,16 +34,12 @@ async def on_webhook_alert(request: Request) -> dict[str, Any]:

webhook_handler = WebhookHandler.create_from_ocean_config()

if webhook_handler.verify_token(token) is not True:
if not webhook_handler.verify_token(token):
return {"status": "error"}

payload = await request.json()

event = request.headers.get("X-Gitlab-Event")
if event == "System Hook":
await webhook_handler.handle_event(payload, True)
else:
await webhook_handler.handle_event(payload)
await webhook_handler.handle_event(payload, request.headers.get("X-Gitlab-Event") == "System Hook")

return {"status": "success"}

Expand All @@ -55,5 +48,4 @@ async def on_start() -> None:
logger.info("Starting async-gitlab integration...")

logger.info("Initializing webhook setup...")
webhook_handler = WebhookHandler.create_from_ocean_config()
await webhook_handler.setup()
await WebhookHandler.create_from_ocean_config().setup()
2 changes: 1 addition & 1 deletion integrations/async-gitlab/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import httpx
from unittest.mock import AsyncMock
from gitlab.helpers.utils import ObjectKind
from gitlab.client import GitLabClient
from gitlab.gitlab_client import GitLabClient

@pytest.fixture
def gitlab_client():
Expand Down

0 comments on commit 6375f16

Please sign in to comment.