From 46c51787bbb3c9306e45290dda8c0b34555577b6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?= <cuu508@gmail.com>
Date: Wed, 18 Dec 2024 08:50:58 +0200
Subject: [PATCH] Update PushBullet notification template to include failure
 reason

cc: #1069
---
 hc/api/tests/test_notify_pushbullet.py        | 24 +++++++++++++------
 hc/api/transports.py                          |  2 +-
 .../integrations/pushbullet_message.html      |  6 ++---
 3 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/hc/api/tests/test_notify_pushbullet.py b/hc/api/tests/test_notify_pushbullet.py
index 5def5daa..ffdf2acc 100644
--- a/hc/api/tests/test_notify_pushbullet.py
+++ b/hc/api/tests/test_notify_pushbullet.py
@@ -1,4 +1,3 @@
-
 from __future__ import annotations
 
 from datetime import timedelta as td
@@ -37,6 +36,7 @@ class NotifyPushbulletTestCase(BaseTestCase):
         self.flip.created = now()
         self.flip.old_status = "new"
         self.flip.new_status = "up"
+        self.flip.reason = "timeout"
 
     @patch("hc.api.transports.curl.request", autospec=True)
     def test_it_works(self, mock_post: Mock) -> None:
@@ -50,8 +50,20 @@ class NotifyPushbulletTestCase(BaseTestCase):
         self.assertEqual(kwargs["headers"]["Access-Token"], "fake-token")
         payload = kwargs["json"]
         self.assertEqual(payload["type"], "note")
-        self.assertIn("""The check "Foo" is DOWN.""", payload["body"])
-        self.assertIn("""Last ping was 10 minutes ago.""", payload["body"])
+        self.assertIn("""The check "Foo" is DOWN""", payload["body"])
+        self.assertIn("grace time passed", payload["body"])
+
+    @patch("hc.api.transports.curl.request", autospec=True)
+    def test_it_handles_reason_fail(self, mock_post: Mock) -> None:
+        self.flip.new_status = "down"
+        self.flip.reason = "fail"
+        mock_post.return_value.status_code = 200
+
+        self.channel.notify(self.flip)
+
+        _, kwargs = mock_post.call_args
+        payload = kwargs["json"]
+        self.assertIn("received a failure signal", payload["body"])
 
     @patch("hc.api.transports.curl.request", autospec=True)
     def test_it_handles_up(self, mock_post: Mock) -> None:
@@ -62,9 +74,7 @@ class NotifyPushbulletTestCase(BaseTestCase):
 
         _, kwargs = mock_post.call_args
         self.assertEqual(kwargs["json"]["type"], "note")
-        self.assertEqual(
-            kwargs["json"]["body"], 'The check "Foo" received a ping and is now UP.'
-        )
+        self.assertEqual(kwargs["json"]["body"], 'The check "Foo" is UP.')
         self.assertEqual(kwargs["headers"]["Access-Token"], "fake-token")
 
     @patch("hc.api.transports.curl.request", autospec=True)
@@ -78,7 +88,7 @@ class NotifyPushbulletTestCase(BaseTestCase):
         _, kwargs = mock_post.call_args
         self.assertEqual(
             kwargs["json"]["body"],
-            'The check "Foo & Bar" received a ping and is now UP.',
+            'The check "Foo & Bar" is UP.',
         )
 
     @patch("hc.api.transports.curl.request", autospec=True)
diff --git a/hc/api/transports.py b/hc/api/transports.py
index 31bd92d5..6b385b69 100644
--- a/hc/api/transports.py
+++ b/hc/api/transports.py
@@ -665,9 +665,9 @@ class Pushbullet(HttpTransport):
         }
         text = tmpl(
             "pushbullet_message.html",
+            flip=flip,
             check=flip.owner,
             status=flip.new_status,
-            ping=self.last_ping(flip),
         )
         payload = {"type": "note", "title": settings.SITE_NAME, "body": text}
         self.post(url, json=payload, headers=headers)
diff --git a/templates/integrations/pushbullet_message.html b/templates/integrations/pushbullet_message.html
index b98a8590..632954dd 100644
--- a/templates/integrations/pushbullet_message.html
+++ b/templates/integrations/pushbullet_message.html
@@ -1,7 +1,5 @@
-{% load humanize %}
-
 {% if status == "down" %}
-The check "{{ check.name_then_code|safe }}" is DOWN.{% if ping %} Last ping was {{ ping.created|naturaltime }}.{% endif %}
+The check "{{ check.name_then_code|safe }}" is DOWN{% if flip.reason %} ({{ flip.reason_long }}){% endif %}.
 {% else %}
-The check "{{ check.name_then_code|safe }}" received a ping and is now UP.
+The check "{{ check.name_then_code|safe }}" is UP.
 {% endif %}