From 14b692674a1b60cb662114d8c24a07e210b25e0b Mon Sep 17 00:00:00 2001 From: Ildar Iskhakov Date: Tue, 2 May 2023 16:57:52 +0800 Subject: [PATCH] Fix bugs in web title and message templates rendering and visual representation (#1747) # What this PR does ## Which issue(s) this PR fixes ## Checklist - [ ] Unit, integration, and e2e (if applicable) tests updated - [ ] Documentation added (or `pr:no public docs` PR label added if not required) - [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required) --- CHANGELOG.md | 4 + .../alerts/tests/test_default_templates.py | 4 + engine/common/utils.py | 11 ++- engine/config_integrations/alertmanager.py | 27 +++--- engine/config_integrations/grafana.py | 28 +++--- .../config_integrations/grafana_alerting.py | 91 ------------------- engine/requirements.txt | 1 + grafana-plugin/src/utils/sanitize.ts | 2 +- 8 files changed, 41 insertions(+), 127 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e5ee2c7..27f5060c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add filter descriptions to web ui by @iskhakov ([1845](https://github.com/grafana/oncall/pull/1845)) +### Fixed + +- Fix bugs in web title and message templates rendering and visual representation ([1747](https://github.com/grafana/oncall/pull/1747)) + ## v1.2.16 (2023-04-27) ### Added diff --git a/engine/apps/alerts/tests/test_default_templates.py b/engine/apps/alerts/tests/test_default_templates.py index 50c1ecd6..eda1bcf3 100644 --- a/engine/apps/alerts/tests/test_default_templates.py +++ b/engine/apps/alerts/tests/test_default_templates.py @@ -63,6 +63,10 @@ def test_default_templates( ) rendered_attr = getattr(rendered_alert, attr) + print(f"{alert_receive_channel}'s {notification_channel} {attr} ") + print(rendered_attr) + print() + print("====================================") assert rendered_attr == expected, ( f"{alert_receive_channel}'s {notification_channel} {attr} " f"is not equal to expected" ) diff --git a/engine/common/utils.py b/engine/common/utils.py index 7c3ab5c0..4f69df3f 100644 --- a/engine/common/utils.py +++ b/engine/common/utils.py @@ -168,13 +168,14 @@ def convert_md_to_html(text): # This gives us
 and  tags for ```-fenced blocks
             "fenced-code-blocks",
             "pyshell",
+            "nl2br",
+            "target-blank-links",
+            "nofollow",
+            "pymdownx.emoji",
+            "pymdownx.magiclink",
+            "tables",
         ],
     ).strip()
-    # Special handling cases for lists
-    text = text.replace("\n\n
    ", "
      ") - text = text.replace("\n
    • ", "
    • ") - # Special handling cases for newlines - text = text.replace("\n", "
      ") return text diff --git a/engine/config_integrations/alertmanager.py b/engine/config_integrations/alertmanager.py index b2ff3de0..560d7311 100644 --- a/engine/config_integrations/alertmanager.py +++ b/engine/config_integrations/alertmanager.py @@ -149,21 +149,18 @@ tests = { }, "web": { "title": "KubeJobCompletion", - "message": ( - "

      " - "Status: firing
      " - "Labels:
      " - "job: kube-state-metrics
      " - "instance: 10.143.139.7:8443
      " - "job_name: email-tracking-perform-initialization-1.0.50
      " - "severity: warning
      alertname: KubeJobCompletion
      " - "namespace: default
      prometheus: monitoring/k8s
      " - "Annotations:
      " - "message: Job default/email-tracking-perform-initialization-1.0.50 is taking more than one hour to complete.
      " - "runbook_url: " - 'here' - "

      " - ), + "message": """

      Status: firing +Labels: +job: kube-state-metrics +instance: 10.143.139.7:8443 +job_name: email-tracking-perform-initialization-1.0.50 +severity: warning +alertname: KubeJobCompletion +namespace: default +prometheus: monitoring/k8s +Annotations: +message: Job default/email-tracking-perform-initialization-1.0.50 is taking more than one hour to complete. +runbook_url: here

      """, # noqa "image_url": None, }, "sms": { diff --git a/engine/config_integrations/grafana.py b/engine/config_integrations/grafana.py index cc35a778..0e120d24 100644 --- a/engine/config_integrations/grafana.py +++ b/engine/config_integrations/grafana.py @@ -169,21 +169,19 @@ tests = { }, "web": { "title": "KubeJobCompletion", - "message": ( - "

      " - "Status: firing
      " - "Labels:
      " - "job: kube-state-metrics
      " - "instance: 10.143.139.7:8443
      " - "job_name: email-tracking-perform-initialization-1.0.50
      " - "severity: warning
      alertname: KubeJobCompletion
      " - "namespace: default
      prometheus: monitoring/k8s
      " - "Annotations:
      " - "message: Job default/email-tracking-perform-initialization-1.0.50 is taking more than one hour to complete.
      " - "runbook_url: " - 'here' - "

      " - ), + "message": """\ +

      Status: firing +Labels: +job: kube-state-metrics +instance: 10.143.139.7:8443 +job_name: email-tracking-perform-initialization-1.0.50 +severity: warning +alertname: KubeJobCompletion +namespace: default +prometheus: monitoring/k8s +Annotations: +message: Job default/email-tracking-perform-initialization-1.0.50 is taking more than one hour to complete. +runbook_url: here

      """, # noqa "image_url": None, }, "sms": { diff --git a/engine/config_integrations/grafana_alerting.py b/engine/config_integrations/grafana_alerting.py index e44aaaae..0b72ff20 100644 --- a/engine/config_integrations/grafana_alerting.py +++ b/engine/config_integrations/grafana_alerting.py @@ -104,97 +104,6 @@ resolve_condition = """\ acknowledge_condition = None -tests = { - "payload": { - "endsAt": "0001-01-01T00:00:00Z", - "labels": { - "job": "kube-state-metrics", - "instance": "10.143.139.7:8443", - "job_name": "email-tracking-perform-initialization-1.0.50", - "severity": "warning", - "alertname": "KubeJobCompletion", - "namespace": "default", - "prometheus": "monitoring/k8s", - }, - "status": "firing", - "startsAt": "2019-12-13T08:57:35.095800493Z", - "annotations": { - "message": "Job default/email-tracking-perform-initialization-1.0.50 is taking more than one hour to complete.", - "runbook_url": "https://github.com/kubernetes-monitoring/kubernetes-mixin/tree/master/runbook.md#alert-name-kubejobcompletion", - }, - "generatorURL": ( - "https://localhost/prometheus/graph?g0.expr=kube_job_spec_completions%7Bjob%3D%22kube-state-metrics%22%7D+-+" - "kube_job_status_succeeded%7Bjob%3D%22kube-state-metrics%22%7D+%3E+0&g0.tab=1" - ), - }, - "slack": { - "title": ( - "*<{web_link}|#1 KubeJobCompletion>* via {integration_name} (*<" - "https://localhost/prometheus/graph?g0.expr=kube_job_spec_completions%7Bjob%3D%22kube-state-metrics%22%7D+-+" - "kube_job_status_succeeded%7Bjob%3D%22kube-state-metrics%22%7D+%3E+0&g0.tab=1" - "|source>*)" - ), - "message": ( - "*Status*: firing\n" - "*Labels:* \n" - "job: kube-state-metrics\n" - "instance: 10.143.139.7:8443\n" - "job_name: email-tracking-perform-initialization-1.0.50\n" - "severity: warning\n" - "alertname: KubeJobCompletion\n" - "namespace: default\n" - "prometheus: monitoring/k8s\n" - "*Annotations:*\n" - "message: Job default/email-tracking-perform-initialization-1.0.50 is taking more than one hour to complete. \n" - "runbook_url: " - ), - "image_url": None, - }, - "web": { - "title": "KubeJobCompletion", - "message": ( - "

      " - "Status: firing
      " - "Labels:
      " - "job: kube-state-metrics
      " - "instance: 10.143.139.7:8443
      " - "job_name: email-tracking-perform-initialization-1.0.50
      " - "severity: warning
      alertname: KubeJobCompletion
      " - "namespace: default
      prometheus: monitoring/k8s
      " - "Annotations:
      " - "message: Job default/email-tracking-perform-initialization-1.0.50 is taking more than one hour to complete.
      " - "runbook_url: " - 'here' - "

      " - ), - "image_url": None, - }, - "sms": { - "title": "KubeJobCompletion", - }, - "phone_call": { - "title": "KubeJobCompletion", - }, - "telegram": { - "title": "KubeJobCompletion", - "message": ( - "Status: firing\n" - "Labels: \n" - "job: kube-state-metrics\n" - "instance: 10.143.139.7:8443\n" - "job_name: email-tracking-perform-initialization-1.0.50\n" - "severity: warning\n" - "alertname: KubeJobCompletion\n" - "namespace: default\n" - "prometheus: monitoring/k8s\n" - "Annotations:\n" - "message: Job default/email-tracking-perform-initialization-1.0.50 is taking more than one hour to complete.\n\n" - "runbook_url: https://github.com/kubernetes-monitoring/kubernetes-mixin/tree/master/runbook.md#alert-name-kubejobcompletion\n" - ), - "image_url": None, - }, -} - example_payload = { "receiver": "amixr", "status": "firing", diff --git a/engine/requirements.txt b/engine/requirements.txt index bc2769f4..5190c3e1 100644 --- a/engine/requirements.txt +++ b/engine/requirements.txt @@ -52,3 +52,4 @@ django-dbconn-retry==0.1.7 django-ipware==4.0.2 django-anymail==8.6 django-deprecate-fields==0.1.1 +pymdown-extensions==9.11 diff --git a/grafana-plugin/src/utils/sanitize.ts b/grafana-plugin/src/utils/sanitize.ts index 23d2a3b1..498bcdea 100644 --- a/grafana-plugin/src/utils/sanitize.ts +++ b/grafana-plugin/src/utils/sanitize.ts @@ -3,7 +3,7 @@ import dompurify from 'dompurify'; export default function sanitize(str: string): string { return dompurify.sanitize(str, { USE_PROFILES: { html: true }, - FORBID_TAGS: ['form', 'input', 'pre'], + FORBID_TAGS: ['form', 'input'], ADD_ATTR: ['target'], }); }