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

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

diff --git a/hc/api/tests/test_notify_gotify.py b/hc/api/tests/test_notify_gotify.py
index 9dc665c2..29bc7128 100644
--- a/hc/api/tests/test_notify_gotify.py
+++ b/hc/api/tests/test_notify_gotify.py
@@ -1,4 +1,3 @@
-
 from __future__ import annotations
 
 import json
@@ -38,6 +37,7 @@ class NotifyGotidyTestCase(BaseTestCase):
         self.flip.created = now()
         self.flip.old_status = "new"
         self.flip.new_status = "down"
+        self.flip.reason = "timeout"
 
     @patch("hc.api.transports.curl.request", autospec=True)
     def test_it_works(self, mock_post: Mock) -> None:
@@ -52,7 +52,17 @@ class NotifyGotidyTestCase(BaseTestCase):
         payload = mock_post.call_args.kwargs["json"]
         self.assertEqual(payload["title"], "Foo is DOWN")
         self.assertIn(self.check.cloaked_url(), payload["message"])
-        self.assertIn("Last ping was 10 minutes ago.", payload["message"])
+        self.assertIn("grace time passed", payload["message"])
+
+    @patch("hc.api.transports.curl.request", autospec=True)
+    def test_it_handles_reason_fail(self, mock_post: Mock) -> None:
+        mock_post.return_value.status_code = 200
+
+        self.flip.reason = "fail"
+        self.channel.notify(self.flip)
+
+        payload = mock_post.call_args.kwargs["json"]
+        self.assertIn("received a failure signal", payload["message"])
 
     @patch("hc.api.transports.curl.request", autospec=True)
     def test_it_handles_subpath(self, mock_post: Mock) -> None:
diff --git a/hc/api/transports.py b/hc/api/transports.py
index 8bfb99e6..2262de56 100644
--- a/hc/api/transports.py
+++ b/hc/api/transports.py
@@ -1565,9 +1565,9 @@ class Gotify(HttpTransport):
         url += "?" + urlencode({"token": self.channel.gotify.token})
 
         ctx = {
+            "flip": flip,
             "check": flip.owner,
             "status": flip.new_status,
-            "ping": self.last_ping(flip),
             "down_checks": self.down_checks(flip.owner),
         }
         payload = {
diff --git a/templates/integrations/gotify_message.html b/templates/integrations/gotify_message.html
index b3c682d3..d8900e97 100644
--- a/templates/integrations/gotify_message.html
+++ b/templates/integrations/gotify_message.html
@@ -1,6 +1,6 @@
 {% load humanize linemode %}{% linemode %}
 {% if status == "down" %}
-    {% line %}🔴 The check [{{ check.name_then_code }}]({{ check.cloaked_url }}) is **DOWN**.{% if ping %} Last ping was {{ ping.created|naturaltime }}.{% endif %}{% endline %}
+    {% line %}🔴 The check [{{ check.name_then_code }}]({{ check.cloaked_url }}) is **DOWN**{% if flip.reason %} ({{ flip.reason_long }}){% endif %}.{% endline %}
 {% else %}
     {% line %}🟢 The check [{{ check.name_then_code }}]({{ check.cloaked_url }}) is now **UP**.{% endline %}
 {% endif %}
@@ -20,4 +20,4 @@
         {% line %}All the other checks are up.{% endline %}
     {% endif %}
 {% endif %}
-{% endlinemode %}
\ No newline at end of file
+{% endlinemode %}