From 94e571a4f0c792513d9649c33302e19f2aa94f9e Mon Sep 17 00:00:00 2001 From: Kraiem Taha Yassine Date: Tue, 21 Nov 2023 17:57:47 +0100 Subject: [PATCH] Api v1.15.0 (#1689) * fix(chalice): fix create alert with MS Teams notification channel closes openreplay/openreplay#1677 * fix(chalice): fix MS Teams notifications * refactor(chalice): enhanced MS Teams notifications closes openreplay/openreplay#1681 (cherry picked from commit 265897f509a527246fc8422a811d3d693676160f) --- api/auth/auth_project.py | 3 +- api/chalicelib/core/alerts.py | 39 ++--- api/chalicelib/core/alerts_listener.py | 1 + api/chalicelib/core/alerts_processor.py | 5 +- api/chalicelib/core/collaboration_base.py | 6 +- api/chalicelib/core/collaboration_msteams.py | 167 ++++++++----------- api/chalicelib/core/collaboration_slack.py | 10 +- api/chalicelib/core/projects.py | 3 +- api/chalicelib/core/users.py | 5 - api/routers/core.py | 3 +- api/schemas/schemas.py | 5 +- api/schemas/transformers_validators.py | 4 + ee/api/auth/auth_project.py | 3 +- ee/api/chalicelib/core/alerts_listener.py | 1 + ee/api/chalicelib/core/alerts_processor.py | 3 + ee/api/chalicelib/core/projects.py | 3 +- 16 files changed, 123 insertions(+), 138 deletions(-) diff --git a/api/auth/auth_project.py b/api/auth/auth_project.py index 52ea4b2f3..a3ccc04e5 100644 --- a/api/auth/auth_project.py +++ b/api/auth/auth_project.py @@ -33,5 +33,6 @@ class ProjectAuthorizer: else: current_project = schemas.CurrentProjectContext(projectId=current_project["projectId"], projectKey=current_project["projectKey"], - platform=current_project["platform"]) + platform=current_project["platform"], + name=current_project["name"]) request.state.currentContext.project = current_project diff --git a/api/chalicelib/core/alerts.py b/api/chalicelib/core/alerts.py index e801ca789..d030ccf9d 100644 --- a/api/chalicelib/core/alerts.py +++ b/api/chalicelib/core/alerts.py @@ -188,27 +188,24 @@ def send_to_msteams_batch(notifications_list): if n.get("destination") not in webhookId_map: webhookId_map[n.get("destination")] = {"tenantId": n["notification"]["tenantId"], "batch": []} - link = f"[{n['notification']['buttonText']}]({config('SITE_URL')}{n['notification']['buttonUrl']})" - webhookId_map[n.get("destination")]["batch"].append({"type": "ColumnSet", - "style": "emphasis", - "separator": True, - "bleed": True, - "columns": [{ - "width": "stretch", - "items": [ - {"type": "TextBlock", - "text": n["notification"]["title"], - "style": "heading", - "size": "Large"}, - {"type": "TextBlock", - "spacing": "small", - "text": n["notification"]["description"], - "wrap": True}, - {"type": "TextBlock", - "spacing": "small", - "text": link} - ] - }]}) + link = f"{config('SITE_URL')}{n['notification']['buttonUrl']}" + # for MSTeams, the batch is the list of `sections` + webhookId_map[n.get("destination")]["batch"].append( + { + "activityTitle": n["notification"]["title"], + "activitySubtitle": f"On Project *{n['projectName']}*", + "facts": [ + { + "name": "Target:", + "value": link + }, + { + "name": "Description:", + "value": n["notification"]["description"] + }], + "markdown": True + } + ) for batch in webhookId_map.keys(): MSTeams.send_batch(tenant_id=webhookId_map[batch]["tenantId"], webhook_id=batch, attachments=webhookId_map[batch]["batch"]) diff --git a/api/chalicelib/core/alerts_listener.py b/api/chalicelib/core/alerts_listener.py index 1a0a75a28..e8ab9d4eb 100644 --- a/api/chalicelib/core/alerts_listener.py +++ b/api/chalicelib/core/alerts_listener.py @@ -6,6 +6,7 @@ def get_all_alerts(): query = """SELECT -1 AS tenant_id, alert_id, projects.project_id, + projects.name AS project_name, detection_method, query, options, diff --git a/api/chalicelib/core/alerts_processor.py b/api/chalicelib/core/alerts_processor.py index efb344785..79c2ea8af 100644 --- a/api/chalicelib/core/alerts_processor.py +++ b/api/chalicelib/core/alerts_processor.py @@ -5,8 +5,9 @@ from decouple import config from pydantic_core._pydantic_core import ValidationError import schemas +from chalicelib.core import alerts from chalicelib.core import alerts_listener -from chalicelib.core import sessions, alerts +from chalicelib.core import sessions from chalicelib.utils import pg_client from chalicelib.utils.TimeUTC import TimeUTC @@ -241,6 +242,8 @@ def generate_notification(alert, result): "buttonText": "Check metrics for more details", "buttonUrl": f"/{alert['projectId']}/metrics", "imageUrl": None, + "projectId": alert["projectId"], + "projectName": alert["projectName"], "options": {"source": "ALERT", "sourceId": alert["alertId"], "sourceMeta": alert["detectionMethod"], "message": alert["options"]["message"], "projectId": alert["projectId"], diff --git a/api/chalicelib/core/collaboration_base.py b/api/chalicelib/core/collaboration_base.py index b0aa2c9bb..454082ccc 100644 --- a/api/chalicelib/core/collaboration_base.py +++ b/api/chalicelib/core/collaboration_base.py @@ -26,17 +26,17 @@ class BaseCollaboration(ABC): @classmethod @abstractmethod - def __share(cls, tenant_id, integration_id, attachments): + def __share(cls, tenant_id, integration_id, attachments, extra=None): pass @classmethod @abstractmethod - def share_session(cls, tenant_id, project_id, session_id, user, comment, integration_id=None): + def share_session(cls, tenant_id, project_id, session_id, user, comment, project_name=None, integration_id=None): pass @classmethod @abstractmethod - def share_error(cls, tenant_id, project_id, error_id, user, comment, integration_id=None): + def share_error(cls, tenant_id, project_id, error_id, user, comment, project_name=None, integration_id=None): pass @classmethod diff --git a/api/chalicelib/core/collaboration_msteams.py b/api/chalicelib/core/collaboration_msteams.py index d6e7b6c64..289da889a 100644 --- a/api/chalicelib/core/collaboration_msteams.py +++ b/api/chalicelib/core/collaboration_msteams.py @@ -1,4 +1,4 @@ -import json +import logging import requests from decouple import config @@ -8,6 +8,8 @@ import schemas from chalicelib.core import webhook from chalicelib.core.collaboration_base import BaseCollaboration +logger = logging.getLogger(__name__) + class MSTeams(BaseCollaboration): @classmethod @@ -22,8 +24,6 @@ class MSTeams(BaseCollaboration): name=data.name) return None - # https://messagecardplayground.azurewebsites.net - # https://adaptivecards.io/designer/ @classmethod def say_hello(cls, url): r = requests.post( @@ -31,12 +31,12 @@ class MSTeams(BaseCollaboration): json={ "@type": "MessageCard", "@context": "https://schema.org/extensions", - "summary": "Hello message", + "summary": "Welcome to OpenReplay", "title": "Welcome to OpenReplay" }) if r.status_code != 200: - print("MSTeams integration failed") - print(r.text) + logging.warning("MSTeams integration failed") + logging.warning(r.text) return False return True @@ -51,15 +51,15 @@ class MSTeams(BaseCollaboration): json=body, timeout=5) if r.status_code != 200: - print(f"!! issue sending msteams raw; webhookId:{webhook_id} code:{r.status_code}") - print(r.text) + logging.warning(f"!! issue sending msteams raw; webhookId:{webhook_id} code:{r.status_code}") + logging.warning(r.text) return None except requests.exceptions.Timeout: - print(f"!! Timeout sending msteams raw webhookId:{webhook_id}") + logging.warning(f"!! Timeout sending msteams raw webhookId:{webhook_id}") return None except Exception as e: - print(f"!! Issue sending msteams raw webhookId:{webhook_id}") - print(str(e)) + logging.warning(f"!! Issue sending msteams raw webhookId:{webhook_id}") + logging.warning(e) return None return {"data": r.text} @@ -68,116 +68,87 @@ class MSTeams(BaseCollaboration): 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)}") - for i in range(0, len(attachments), 100): - print(json.dumps({"type": "message", - "attachments": [ - {"contentType": "application/vnd.microsoft.card.adaptive", - "contentUrl": None, - "content": { - "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", - "type": "AdaptiveCard", - "version": "1.2", - "body": attachments[i:i + 100]}} - ]})) - r = requests.post( - url=integration["endpoint"], - json={"type": "message", - "attachments": [ - {"contentType": "application/vnd.microsoft.card.adaptive", - "contentUrl": None, - "content": { - "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", - "type": "AdaptiveCard", - "version": "1.2", - "body": attachments[i:i + 100]}} - ]}) + logging.debug(f"====> sending msteams batch notification: {len(attachments)}") + for i in range(0, len(attachments), 50): + part = attachments[i:i + 50] + for j in range(1, len(part), 2): + part.insert(j, {"text": "***"}) + + r = requests.post(url=integration["endpoint"], + json={ + "@type": "MessageCard", + "@context": "http://schema.org/extensions", + "summary": part[0]["activityTitle"], + "sections": part + }) if r.status_code != 200: - print("!!!! something went wrong") - print(r) - print(r.text) + logging.warning("!!!! something went wrong") + logging.warning(r.text) @classmethod - def __share(cls, tenant_id, integration_id, attachement): + def __share(cls, tenant_id, integration_id, attachement, extra=None): + if extra is None: + extra = {} 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( url=integration["endpoint"], - json={"type": "message", - "attachments": [ - {"contentType": "application/vnd.microsoft.card.adaptive", - "contentUrl": None, - "content": { - "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", - "type": "AdaptiveCard", - "version": "1.5", - "body": [attachement]}} - ] - }) + json={ + "@type": "MessageCard", + "@context": "http://schema.org/extensions", + "sections": [attachement], + **extra + }) return r.text @classmethod - def share_session(cls, tenant_id, project_id, session_id, user, comment, integration_id=None): - title = f"[{user}](mailto:{user}) has shared the below session!" + def share_session(cls, tenant_id, project_id, session_id, user, comment, project_name=None, integration_id=None): + title = f"*{user}* has shared the below session!" link = f"{config('SITE_URL')}/{project_id}/session/{session_id}" - link = f"[{link}]({link})" - args = {"type": "ColumnSet", - "style": "emphasis", - "separator": True, - "bleed": True, - "columns": [{ - "width": "stretch", - "items": [ - {"type": "TextBlock", - "text": title, - "style": "heading", - "size": "Large"}, - {"type": "TextBlock", - "spacing": "small", - "text": link} - ] - }]} + args = { + "activityTitle": title, + "facts": [ + { + "name": "Session:", + "value": link + }], + "markdown": True + } + if project_name and len(project_name) > 0: + args["activitySubtitle"] = f"On Project *{project_name}*" if comment and len(comment) > 0: - args["columns"][0]["items"].append({ - "type": "TextBlock", - "spacing": "small", - "text": comment + args["facts"].append({ + "name": "Comment:", + "value": comment }) - data = cls.__share(tenant_id, integration_id, attachement=args) + data = cls.__share(tenant_id, integration_id, attachement=args, extra={"summary": title}) if "errors" in data: return data return {"data": data} @classmethod - def share_error(cls, tenant_id, project_id, error_id, user, comment, integration_id=None): - title = f"[{user}](mailto:{user}) has shared the below error!" + def share_error(cls, tenant_id, project_id, error_id, user, comment, project_name=None, integration_id=None): + title = f"*{user}* has shared the below error!" link = f"{config('SITE_URL')}/{project_id}/errors/{error_id}" - link = f"[{link}]({link})" - args = {"type": "ColumnSet", - "style": "emphasis", - "separator": True, - "bleed": True, - "columns": [{ - "width": "stretch", - "items": [ - {"type": "TextBlock", - "text": title, - "style": "heading", - "size": "Large"}, - {"type": "TextBlock", - "spacing": "small", - "text": link} - ] - }]} + args = { + "activityTitle": title, + "facts": [ + { + "name": "Session:", + "value": link + }], + "markdown": True + } + if project_name and len(project_name) > 0: + args["activitySubtitle"] = f"On Project *{project_name}*" if comment and len(comment) > 0: - args["columns"][0]["items"].append({ - "type": "TextBlock", - "spacing": "small", - "text": comment + args["facts"].append({ + "name": "Comment:", + "value": comment }) - data = cls.__share(tenant_id, integration_id, attachement=args) + data = cls.__share(tenant_id, integration_id, attachement=args, extra={"summary": title}) if "errors" in data: return data return {"data": data} diff --git a/api/chalicelib/core/collaboration_slack.py b/api/chalicelib/core/collaboration_slack.py index f75f3ffd7..551068df5 100644 --- a/api/chalicelib/core/collaboration_slack.py +++ b/api/chalicelib/core/collaboration_slack.py @@ -80,16 +80,18 @@ class Slack(BaseCollaboration): print(r.text) @classmethod - def __share(cls, tenant_id, integration_id, attachement): + def __share(cls, tenant_id, integration_id, attachement, extra=None): + if extra is None: + extra = {} 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() - r = requests.post(url=integration["endpoint"], json={"attachments": [attachement]}) + r = requests.post(url=integration["endpoint"], json={"attachments": [attachement], **extra}) return r.text @classmethod - def share_session(cls, tenant_id, project_id, session_id, user, comment, integration_id=None): + def share_session(cls, tenant_id, project_id, session_id, user, comment, project_name=None, integration_id=None): args = {"fallback": f"{user} has shared the below session!", "pretext": f"{user} has shared the below session!", "title": f"{config('SITE_URL')}/{project_id}/session/{session_id}", @@ -101,7 +103,7 @@ class Slack(BaseCollaboration): return {"data": data} @classmethod - def share_error(cls, tenant_id, project_id, error_id, user, comment, integration_id=None): + def share_error(cls, tenant_id, project_id, error_id, user, comment, project_name=None, integration_id=None): args = {"fallback": f"{user} has shared the below error!", "pretext": f"{user} has shared the below error!", "title": f"{config('SITE_URL')}/{project_id}/errors/{error_id}", diff --git a/api/chalicelib/core/projects.py b/api/chalicelib/core/projects.py index 7cbb2378d..42877cf8b 100644 --- a/api/chalicelib/core/projects.py +++ b/api/chalicelib/core/projects.py @@ -201,7 +201,8 @@ def get_by_project_key(project_key): with pg_client.PostgresClient() as cur: query = cur.mogrify("""SELECT project_id, project_key, - platform + platform, + name FROM public.projects WHERE project_key =%(project_key)s AND deleted_at ISNULL;""", diff --git a/api/chalicelib/core/users.py b/api/chalicelib/core/users.py index 242f0bee1..9dfbbbf14 100644 --- a/api/chalicelib/core/users.py +++ b/api/chalicelib/core/users.py @@ -114,10 +114,6 @@ def reset_member(tenant_id, editor_id, user_id_to_update): def update(tenant_id, user_id, changes, output=True): - print("---------") - print(tenant_id) - print(user_id) - print(changes) AUTH_KEYS = ["password", "invitationToken", "invitedAt", "changePwdExpireAt", "changePwdToken"] if len(changes.keys()) == 0: return None @@ -142,7 +138,6 @@ def update(tenant_id, user_id, changes, output=True): WHERE users.user_id = %(user_id)s;""", {"user_id": user_id, **changes}) cur.execute(query) - print(query) if len(sub_query_bauth) > 0: query = cur.mogrify(f"""\ UPDATE public.basic_authentication diff --git a/api/routers/core.py b/api/routers/core.py index 161ca3eff..2073aa09b 100644 --- a/api/routers/core.py +++ b/api/routers/core.py @@ -70,7 +70,8 @@ def integration_notify(projectId: int, integration: str, webhookId: int, source: args = {"tenant_id": context.tenant_id, "user": context.email, "comment": comment, "project_id": projectId, - "integration_id": webhookId} + "integration_id": webhookId, + "project_name": context.project.name} if integration == schemas.WebhookType.slack: if source == "sessions": return Slack.share_session(session_id=sourceId, **args) diff --git a/api/schemas/schemas.py b/api/schemas/schemas.py index 498aab74d..72db75b58 100644 --- a/api/schemas/schemas.py +++ b/api/schemas/schemas.py @@ -7,7 +7,7 @@ from pydantic import field_validator, model_validator, computed_field from chalicelib.utils.TimeUTC import TimeUTC from .overrides import BaseModel, Enum, ORUnion from .transformers_validators import transform_email, remove_whitespace, remove_duplicate_values, single_to_list, \ - force_is_event, NAME_PATTERN + force_is_event, NAME_PATTERN, int_to_string def transform_old_filter_type(cls, values): @@ -112,6 +112,7 @@ class CreateProjectSchema(BaseModel): class CurrentProjectContext(BaseModel): project_id: int = Field(..., gt=0) project_key: str = Field(...) + name: str = Field(...) platform: Literal["web", "ios"] = Field(...) @@ -365,6 +366,8 @@ class _AlertMessageSchema(BaseModel): type: str = Field(...) value: str = Field(...) + _transform_value = field_validator('value', mode='before')(int_to_string) + class AlertDetectionType(str, Enum): percent = "percent" diff --git a/api/schemas/transformers_validators.py b/api/schemas/transformers_validators.py index 5bca604a2..dd9363382 100644 --- a/api/schemas/transformers_validators.py +++ b/api/schemas/transformers_validators.py @@ -9,6 +9,10 @@ def transform_email(email: str) -> str: return email.lower().strip() if isinstance(email, str) else email +def int_to_string(value: int) -> str: + return str(value) if isinstance(value, int) else int + + def remove_whitespace(value: str) -> str: return " ".join(value.split()) if isinstance(value, str) else value diff --git a/ee/api/auth/auth_project.py b/ee/api/auth/auth_project.py index ad5375034..2ddb8b32b 100644 --- a/ee/api/auth/auth_project.py +++ b/ee/api/auth/auth_project.py @@ -42,5 +42,6 @@ class ProjectAuthorizer: else: current_project = schemas.CurrentProjectContext(projectId=current_project["projectId"], projectKey=current_project["projectKey"], - platform=current_project["platform"]) + platform=current_project["platform"], + name=current_project["name"]) request.state.currentContext.project = current_project diff --git a/ee/api/chalicelib/core/alerts_listener.py b/ee/api/chalicelib/core/alerts_listener.py index ebd9afa56..3f216a420 100644 --- a/ee/api/chalicelib/core/alerts_listener.py +++ b/ee/api/chalicelib/core/alerts_listener.py @@ -6,6 +6,7 @@ def get_all_alerts(): query = """SELECT tenant_id, alert_id, projects.project_id, + projects.name AS project_name, detection_method, query, options, diff --git a/ee/api/chalicelib/core/alerts_processor.py b/ee/api/chalicelib/core/alerts_processor.py index 692786119..59e7831d3 100644 --- a/ee/api/chalicelib/core/alerts_processor.py +++ b/ee/api/chalicelib/core/alerts_processor.py @@ -123,6 +123,7 @@ def Build(a): logging.warning("Validation error for:") logging.warning(a["filter"]) raise + full_args, query_part = sessions.search_query_parts(data=data, error_status=None, errors_only=False, issue=None, project_id=a["projectId"], user_id=None, favorite_only=False) @@ -245,6 +246,8 @@ def generate_notification(alert, result): "buttonText": "Check metrics for more details", "buttonUrl": f"/{alert['projectId']}/metrics", "imageUrl": None, + "projectId": alert["projectId"], + "projectName": alert["projectName"], "options": {"source": "ALERT", "sourceId": alert["alertId"], "sourceMeta": alert["detectionMethod"], "message": alert["options"]["message"], "projectId": alert["projectId"], diff --git a/ee/api/chalicelib/core/projects.py b/ee/api/chalicelib/core/projects.py index 886162dde..3d33160bd 100644 --- a/ee/api/chalicelib/core/projects.py +++ b/ee/api/chalicelib/core/projects.py @@ -241,7 +241,8 @@ def get_by_project_key(project_key): with pg_client.PostgresClient() as cur: query = cur.mogrify("""SELECT project_id, project_key, - platform + platform, + name FROM public.projects WHERE project_key =%(project_key)s AND deleted_at ISNULL;""",