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
This commit is contained in:
Kraiem Taha Yassine 2023-11-21 17:57:47 +01:00 committed by GitHub
parent 4a4b027bc5
commit 265897f509
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 123 additions and 138 deletions

View file

@ -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

View file

@ -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"])

View file

@ -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,

View file

@ -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"],

View file

@ -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

View file

@ -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}

View file

@ -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}",

View file

@ -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;""",

View file

@ -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

View file

@ -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)

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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,

View file

@ -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"],

View file

@ -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;""",