diff --git a/CHANGELOG.md b/CHANGELOG.md index a132e58b..3483bb48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. ## Unreleased +### Improvements +- Set Pushover alert priorities for "down" and "up" events separately + + ### Bug Fixes - Fix after-login redirects for users landing in the "Add Slack" page diff --git a/hc/api/models.py b/hc/api/models.py index 358031e2..f87ac01d 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -339,11 +339,11 @@ class Channel(models.Model): return 'img/integrations/%s.png' % self.kind @property - def po_value(self): + def po_priority(self): assert self.kind == "po" - user_key, prio = self.value.split("|") - prio = int(prio) - return user_key, prio, PO_PRIORITIES[prio] + parts = self.value.split("|") + prio = int(parts[1]) + return PO_PRIORITIES[prio] @property def url_down(self): diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py index c145f3d5..b92ba911 100644 --- a/hc/api/tests/test_notify.py +++ b/hc/api/tests/test_notify.py @@ -405,6 +405,21 @@ class NotifyTestCase(BaseTestCase): payload = kwargs["data"] self.assertIn("DOWN", payload["title"]) + @patch("hc.api.transports.requests.request") + def test_pushover_up_priority(self, mock_post): + self._setup_data("po", "123|0|2", status="up") + mock_post.return_value.status_code = 200 + + self.channel.notify(self.check) + assert Notification.objects.count() == 1 + + args, kwargs = mock_post.call_args + payload = kwargs["data"] + self.assertIn("UP", payload["title"]) + self.assertEqual(payload["priority"], 2) + self.assertIn("retry", payload) + self.assertIn("expire", payload) + @patch("hc.api.transports.requests.request") def test_victorops(self, mock_post): self._setup_data("victorops", "123") diff --git a/hc/api/transports.py b/hc/api/transports.py index fad1ffd8..6e67546a 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -304,7 +304,13 @@ class Pushover(HttpTransport): } text = tmpl("pushover_message.html", **ctx) title = tmpl("pushover_title.html", **ctx) - user_key, prio = self.channel.value.split("|") + + pieces = self.channel.value.split("|") + user_key, prio = pieces[0], pieces[1] + # The third element, if present, is the priority for "up" events + if len(pieces) == 3 and check.status == "up": + prio = pieces[2] + payload = { "token": settings.PUSHOVER_API_TOKEN, "user": user_key, diff --git a/hc/front/tests/test_add_pushover.py b/hc/front/tests/test_add_pushover.py index 6f6d6be7..17ce471f 100644 --- a/hc/front/tests/test_add_pushover.py +++ b/hc/front/tests/test_add_pushover.py @@ -39,13 +39,13 @@ class AddPushoverTestCase(BaseTestCase): session["pushover"] = "foo" session.save() - params = "pushover_user_key=a&state=foo&prio=0" + params = "pushover_user_key=a&state=foo&prio=0&prio_up=-1" r = self.client.get("/integrations/add_pushover/?%s" % params) self.assertEqual(r.status_code, 302) channels = list(Channel.objects.all()) assert len(channels) == 1 - assert channels[0].value == "a|0" + assert channels[0].value == "a|0|-1" def test_it_validates_priority(self): self.client.login(username="alice@example.org", password="password") @@ -58,6 +58,17 @@ class AddPushoverTestCase(BaseTestCase): r = self.client.get("/integrations/add_pushover/?%s" % params) self.assertEqual(r.status_code, 400) + def test_it_validates_priority_up(self): + self.client.login(username="alice@example.org", password="password") + + session = self.client.session + session["pushover"] = "foo" + session.save() + + params = "pushover_user_key=a&state=foo&prio_up=abc" + r = self.client.get("/integrations/add_pushover/?%s" % params) + self.assertEqual(r.status_code, 400) + def test_it_validates_state(self): self.client.login(username="alice@example.org", password="password") diff --git a/hc/front/views.py b/hc/front/views.py index 826e33c7..72f14392 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -873,6 +873,7 @@ def add_pushover(request): success_url = settings.SITE_ROOT + reverse("hc-add-pushover") + "?" + urlencode({ "state": state, "prio": request.POST.get("po_priority", "0"), + "prio_up": request.POST.get("po_priority_up", "0") }) subscription_url = settings.PUSHOVER_SUBSCRIPTION_URL + "?" + urlencode({ "success": success_url, @@ -892,6 +893,10 @@ def add_pushover(request): if prio not in ("-2", "-1", "0", "1", "2"): return HttpResponseBadRequest() + prio_up = request.GET.get("prio_up") + if prio_up not in ("-2", "-1", "0", "1", "2"): + return HttpResponseBadRequest() + if request.GET.get("pushover_unsubscribed") == "1": # Unsubscription: delete all Pushover channels for this user Channel.objects.filter(user=request.user, kind="po").delete() @@ -899,7 +904,7 @@ def add_pushover(request): # Subscription channel = Channel(user=request.team.user, kind="po") - channel.value = "%s|%s" % (key, prio) + channel.value = "%s|%s|%s" % (key, prio, prio_up) channel.save() channel.assign_all_checks() diff --git a/static/css/add_pushover.css b/static/css/add_pushover.css index db0268e0..ef49591f 100644 --- a/static/css/add_pushover.css +++ b/static/css/add_pushover.css @@ -1,8 +1,3 @@ -#add-pushover .radio { - margin-bottom: 1em; -} - #add-pushover .help { - display: block; - color: #888; + opacity: 0.6; } \ No newline at end of file diff --git a/static/img/integrations/setup_pushover_1.png b/static/img/integrations/setup_pushover_1.png index 0daaf912..e72902ad 100644 Binary files a/static/img/integrations/setup_pushover_1.png and b/static/img/integrations/setup_pushover_1.png differ diff --git a/templates/front/channels.html b/templates/front/channels.html index da95ce31..8f8e9371 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -54,7 +54,7 @@ {% elif ch.kind == "victorops" %} VictorOps {% elif ch.kind == "po" %} - Pushover ({{ ch.po_value|last }} priority) + Pushover ({{ ch.po_priority }} priority) {% elif ch.kind == "slack" %} Slack {% if ch.slack_team %} diff --git a/templates/integrations/add_pushover.html b/templates/integrations/add_pushover.html index 2aa10899..dd0c1ad4 100644 --- a/templates/integrations/add_pushover.html +++ b/templates/integrations/add_pushover.html @@ -1,5 +1,5 @@ {% extends "base.html" %} -{% load humanize static hc_extras %} +{% load compress humanize static hc_extras %} {% block title %}Add Pushover - {% site_name %}{% endblock %} @@ -56,7 +56,7 @@
After logging in, go to "Integrations → Add Pushover". Pushover supports different notification priorities from - silent to "Emergency". Select your preferred priority + silent to "Emergency". Select your preferred priorities and click "Subscribe with Pushover".
@@ -106,65 +106,72 @@ {% else %}