From cd70e88c52951fc174af24285f766f11cdcb1013 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?= <cuu508@gmail.com>
Date: Tue, 17 Dec 2024 08:23:01 +0200
Subject: [PATCH] Update Pushover notification template to include failure
 reason

cc: #1069
---
 hc/api/tests/test_notify_pushover.py         | 14 +++++++++++++-
 hc/api/transports.py                         |  1 +
 templates/integrations/pushover_message.html |  2 +-
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/hc/api/tests/test_notify_pushover.py b/hc/api/tests/test_notify_pushover.py
index 075ce551..eabc803d 100644
--- a/hc/api/tests/test_notify_pushover.py
+++ b/hc/api/tests/test_notify_pushover.py
@@ -1,4 +1,3 @@
-
 from __future__ import annotations
 
 from datetime import timedelta as td
@@ -42,6 +41,7 @@ class NotifyPushoverTestCase(BaseTestCase):
         self.flip.created = now()
         self.flip.old_status = "new"
         self.flip.new_status = status
+        self.flip.reason = "timeout"
 
     @patch("hc.api.transports.curl.request", autospec=True)
     def test_it_works(self, mock_post: Mock) -> None:
@@ -59,12 +59,24 @@ class NotifyPushoverTestCase(BaseTestCase):
         self.assertEqual(payload["url"], self.check.cloaked_url())
         self.assertIn("112233", payload["message"])
         self.assertIn("10 minutes ago", payload["message"])
+        self.assertIn("grace time passed", payload["message"])
 
         # Only one check in the project, so there should be no note about
         # other checks:
         self.assertNotIn("All the other checks are up.", payload["message"])
         self.assertEqual(payload["tags"], self.check.unique_key)
 
+    @patch("hc.api.transports.curl.request", autospec=True)
+    def test_it_handles_reason_fail(self, mock_post: Mock) -> None:
+        self._setup_data("123|0")
+        mock_post.return_value.status_code = 200
+
+        self.flip.reason = "fail"
+        self.channel.notify(self.flip)
+
+        payload = mock_post.call_args.kwargs["data"]
+        self.assertIn("received a failure signal", payload["message"])
+
     @patch("hc.api.transports.curl.request", autospec=True)
     def test_it_shows_cron_schedule(self, mock_post: Mock) -> None:
         self._setup_data("123|0")
diff --git a/hc/api/transports.py b/hc/api/transports.py
index c354c881..9a01cc7f 100644
--- a/hc/api/transports.py
+++ b/hc/api/transports.py
@@ -730,6 +730,7 @@ class Pushover(HttpTransport):
             self.post(url, data=cancel_payload)
 
         ctx = {
+            "flip": flip,
             "check": check,
             "status": flip.new_status,
             "ping": self.last_ping(flip),
diff --git a/templates/integrations/pushover_message.html b/templates/integrations/pushover_message.html
index 776604d1..bd99d930 100644
--- a/templates/integrations/pushover_message.html
+++ b/templates/integrations/pushover_message.html
@@ -1,6 +1,6 @@
 {% load hc_extras humanize linemode %}{% linemode %}
 {% if status == "down" %}
-    {% line %}The check <b>{{ check.name_then_code }}</b> is <b>DOWN</b>.{% endline %}
+    {% line %}The check <b>{{ check.name_then_code }}</b> is <b>DOWN</b>{% if flip.reason %} ({{ flip.reason_long }}){% endif %}.{% endline %}
 {% else %}
     {% line %}The check <b>{{ check.name_then_code }}</b> is now <b>UP</b>.{% endline %}
 {% endif %}