From 3d287cfc5846be57b11855458ef540a2148acafb Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Fri, 27 Jan 2023 13:48:40 +0100 Subject: [PATCH] feat(chalice): fixed update slack feat(chalice): changed get /integrations response --- api/chalicelib/core/collaboration_base.py | 2 +- api/chalicelib/core/collaboration_msteams.py | 8 ++++---- api/chalicelib/core/collaboration_slack.py | 8 ++++---- api/chalicelib/core/integrations_global.py | 5 ++++- api/routers/core.py | 8 ++++---- api/routers/core_dynamic.py | 4 +++- api/schemas.py | 1 + ee/api/chalicelib/core/integrations_global.py | 5 ++++- ee/api/routers/core_dynamic.py | 4 +++- 9 files changed, 28 insertions(+), 17 deletions(-) diff --git a/api/chalicelib/core/collaboration_base.py b/api/chalicelib/core/collaboration_base.py index 8b008d784..b0aa2c9bb 100644 --- a/api/chalicelib/core/collaboration_base.py +++ b/api/chalicelib/core/collaboration_base.py @@ -41,5 +41,5 @@ class BaseCollaboration(ABC): @classmethod @abstractmethod - def __get(cls, tenant_id, integration_id=None): + def get_integration(cls, tenant_id, integration_id=None): pass diff --git a/api/chalicelib/core/collaboration_msteams.py b/api/chalicelib/core/collaboration_msteams.py index 4dc3d276a..5a0c0d227 100644 --- a/api/chalicelib/core/collaboration_msteams.py +++ b/api/chalicelib/core/collaboration_msteams.py @@ -38,7 +38,7 @@ class MSTeams(BaseCollaboration): @classmethod def send_raw(cls, tenant_id, webhook_id, body): - integration = cls.__get(tenant_id=tenant_id, integration_id=webhook_id) + integration = cls.get_integration(tenant_id=tenant_id, integration_id=webhook_id) if integration is None: return {"errors": ["msteams integration not found"]} try: @@ -61,7 +61,7 @@ class MSTeams(BaseCollaboration): @classmethod def send_batch(cls, tenant_id, webhook_id, attachments): - integration = cls.__get(tenant_id=tenant_id, integration_id=webhook_id) + integration = cls.get_integration(tenant_id=tenant_id, integration_id=webhook_id) if integration is None: return {"errors": ["msteams integration not found"]} print(f"====> sending msteams batch notification: {len(attachments)}") @@ -95,7 +95,7 @@ class MSTeams(BaseCollaboration): @classmethod def __share(cls, tenant_id, integration_id, attachement): - integration = cls.__get(tenant_id=tenant_id, integration_id=integration_id) + integration = cls.get_integration(tenant_id=tenant_id, integration_id=integration_id) if integration is None: return {"errors": ["Microsoft Teams integration not found"]} r = requests.post( @@ -179,7 +179,7 @@ class MSTeams(BaseCollaboration): return {"data": data} @classmethod - def __get(cls, tenant_id, integration_id=None): + def get_integration(cls, tenant_id, integration_id=None): if integration_id is not None: return webhook.get_webhook(tenant_id=tenant_id, webhook_id=integration_id, webhook_type=schemas.WebhookType.msteams) diff --git a/api/chalicelib/core/collaboration_slack.py b/api/chalicelib/core/collaboration_slack.py index 8c156ac51..3cd5565c1 100644 --- a/api/chalicelib/core/collaboration_slack.py +++ b/api/chalicelib/core/collaboration_slack.py @@ -37,7 +37,7 @@ class Slack(BaseCollaboration): @classmethod def send_raw(cls, tenant_id, webhook_id, body): - integration = cls.__get(tenant_id=tenant_id, integration_id=webhook_id) + integration = cls.get_integration(tenant_id=tenant_id, integration_id=webhook_id) if integration is None: return {"errors": ["slack integration not found"]} try: @@ -60,7 +60,7 @@ class Slack(BaseCollaboration): @classmethod def send_batch(cls, tenant_id, webhook_id, attachments): - integration = cls.__get(tenant_id=tenant_id, integration_id=webhook_id) + integration = cls.get_integration(tenant_id=tenant_id, integration_id=webhook_id) if integration is None: return {"errors": ["slack integration not found"]} print(f"====> sending slack batch notification: {len(attachments)}") @@ -76,7 +76,7 @@ class Slack(BaseCollaboration): @classmethod def __share(cls, tenant_id, integration_id, attachement): - integration = cls.__get(tenant_id=tenant_id, integration_id=integration_id) + integration = cls.get_integration(tenant_id=tenant_id, integration_id=integration_id) if integration is None: return {"errors": ["slack integration not found"]} attachement["ts"] = datetime.now().timestamp() @@ -108,7 +108,7 @@ class Slack(BaseCollaboration): return {"data": data} @classmethod - def __get(cls, tenant_id, integration_id=None): + def get_integration(cls, tenant_id, integration_id=None): if integration_id is not None: return webhook.get_webhook(tenant_id=tenant_id, webhook_id=integration_id, webhook_type=schemas.WebhookType.slack) diff --git a/api/chalicelib/core/integrations_global.py b/api/chalicelib/core/integrations_global.py index 454d2a5e0..f69fa99c0 100644 --- a/api/chalicelib/core/integrations_global.py +++ b/api/chalicelib/core/integrations_global.py @@ -51,7 +51,10 @@ def get_global_integrations_status(tenant_id, user_id, project_id): AND provider='elasticsearch')) AS {schemas.IntegrationType.elasticsearch.value}, EXISTS((SELECT 1 FROM public.webhooks - WHERE type='slack')) AS {schemas.IntegrationType.slack.value};""", + WHERE type='slack')) AS {schemas.IntegrationType.slack.value}, + EXISTS((SELECT 1 + FROM public.webhooks + WHERE type='msteams')) AS {schemas.IntegrationType.ms_teams.value};""", {"user_id": user_id, "tenant_id": tenant_id, "project_id": project_id}) ) current_integrations = cur.fetchone() diff --git a/api/routers/core.py b/api/routers/core.py index 28c13a737..53bbf54dd 100644 --- a/api/routers/core.py +++ b/api/routers/core.py @@ -768,8 +768,7 @@ def get_slack_channels(context: schemas.CurrentContext = Depends(OR_context)): @app.get('/integrations/slack/{webhookId}', tags=["integrations"]) def get_slack_webhook(webhookId: int, context: schemas.CurrentContext = Depends(OR_context)): - return {"data": webhook.get_webhook(tenant_id=context.tenant_id, webhook_id=webhookId, - webhook_type=schemas.WebhookType.slack)} + return {"data": Slack.get_integration(tenant_id=context.tenant_id, integration_id=webhookId)} @app.delete('/integrations/slack/{webhookId}', tags=["integrations"]) @@ -879,8 +878,9 @@ def add_msteams_integration(data: schemas.AddCollaborationSchema, def edit_msteams_integration(webhookId: int, data: schemas.EditCollaborationSchema = Body(...), context: schemas.CurrentContext = Depends(OR_context)): if len(data.url) > 0: - old = webhook.get_webhook(tenant_id=context.tenant_id, webhook_id=webhookId, - webhook_type=schemas.WebhookType.msteams) + old = MSTeams.get_integration(tenant_id=context.tenant_id, integration_id=webhookId) + if not old: + return {"errors": ["MsTeams integration not found."]} if old["endpoint"] != data.url: if not MSTeams.say_hello(data.url): return { diff --git a/api/routers/core_dynamic.py b/api/routers/core_dynamic.py index 290d98183..96397ce34 100644 --- a/api/routers/core_dynamic.py +++ b/api/routers/core_dynamic.py @@ -75,7 +75,9 @@ def add_slack_integration(data: schemas.AddCollaborationSchema, context: schemas def edit_slack_integration(integrationId: int, data: schemas.EditCollaborationSchema = Body(...), context: schemas.CurrentContext = Depends(OR_context)): if len(data.url) > 0: - old = webhook.get_webhook(tenant_id=context.tenant_id, webhook_id=integrationId) + old = Slack.get_integration(tenant_id=context.tenant_id, integration_id=integrationId) + if not old: + return {"errors": ["Slack integration not found."]} if old["endpoint"] != data.url: if not Slack.say_hello(data.url): return { diff --git a/api/schemas.py b/api/schemas.py index aeacc0b54..0c4fdde77 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -1219,6 +1219,7 @@ class IntegrationType(str, Enum): github = "GITHUB" jira = "JIRA" slack = "SLACK" + ms_teams = "MSTEAMS" sentry = "SENTRY" bugsnag = "BUGSNAG" rollbar = "ROLLBAR" diff --git a/ee/api/chalicelib/core/integrations_global.py b/ee/api/chalicelib/core/integrations_global.py index cf40aedb7..8de504f09 100644 --- a/ee/api/chalicelib/core/integrations_global.py +++ b/ee/api/chalicelib/core/integrations_global.py @@ -51,7 +51,10 @@ def get_global_integrations_status(tenant_id, user_id, project_id): AND provider='elasticsearch')) AS {schemas.IntegrationType.elasticsearch.value}, EXISTS((SELECT 1 FROM public.webhooks - WHERE type='slack' AND tenant_id=%(tenant_id)s)) AS {schemas.IntegrationType.slack.value};""", + WHERE type='slack' AND tenant_id=%(tenant_id)s)) AS {schemas.IntegrationType.slack.value}, + EXISTS((SELECT 1 + FROM public.webhooks + WHERE type='msteams' AND tenant_id=%(tenant_id)s)) AS {schemas.IntegrationType.ms_teams.value};""", {"user_id": user_id, "tenant_id": tenant_id, "project_id": project_id}) ) current_integrations = cur.fetchone() diff --git a/ee/api/routers/core_dynamic.py b/ee/api/routers/core_dynamic.py index bc4bf0582..d483bb646 100644 --- a/ee/api/routers/core_dynamic.py +++ b/ee/api/routers/core_dynamic.py @@ -79,7 +79,9 @@ def add_slack_client(data: schemas.AddCollaborationSchema, context: schemas.Curr def edit_slack_integration(integrationId: int, data: schemas.EditCollaborationSchema = Body(...), context: schemas.CurrentContext = Depends(OR_context)): if len(data.url) > 0: - old = webhook.get_webhook(tenant_id=context.tenant_id, webhook_id=integrationId) + old = Slack.get_integration(tenant_id=context.tenant_id, integration_id=integrationId) + if not old: + return {"errors": ["Slack integration not found."]} if old["endpoint"] != data.url: if not Slack.say_hello(data.url): return {