diff --git a/hc/api/admin.py b/hc/api/admin.py index 37b9e7e1..cde781dc 100644 --- a/hc/api/admin.py +++ b/hc/api/admin.py @@ -27,7 +27,7 @@ class ChecksAdmin(admin.ModelAdmin): 'all': ('css/admin/checks.css',) } - search_fields = ["name", "user__email"] + search_fields = ["name", "user__email", "code"] list_display = ("id", "name_tags", "created", "code", "status", "email", "last_ping", "n_pings") list_select_related = ("user", ) diff --git a/hc/api/models.py b/hc/api/models.py index 46a524af..bfab573a 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -92,7 +92,7 @@ class Check(models.Model): self.channel_set.add(*channels) def tags_list(self): - return self.tags.split(" ") + return [t.strip() for t in self.tags.split(" ") if t.strip()] class Ping(models.Model): @@ -148,14 +148,9 @@ class Channel(models.Model): n.save() elif self.kind == "slack": - tmpl = "integrations/slack_message.html" + tmpl = "integrations/slack_message.json" text = render_to_string(tmpl, {"check": check}) - payload = { - "text": text, - "username": "healthchecks.io", - "icon_url": "https://healthchecks.io/static/img/logo@2x.png" - } - + payload = json.loads(text) r = requests.post(self.value, json=payload, timeout=5) n.status = r.status_code diff --git a/hc/api/tests/test_check_model.py b/hc/api/tests/test_check_model.py new file mode 100644 index 00000000..af940f6c --- /dev/null +++ b/hc/api/tests/test_check_model.py @@ -0,0 +1,15 @@ +from django.test import TestCase + +from hc.api.models import Check + + +class CheckModelTestCase(TestCase): + + def test_it_strips_tags(self): + check = Check() + + check.tags = " foo bar " + self.assertEquals(check.tags_list(), ["foo", "bar"]) + + check.tags = " " + self.assertEquals(check.tags_list(), []) diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py index c7ae3818..1b3b0484 100644 --- a/hc/api/tests/test_notify.py +++ b/hc/api/tests/test_notify.py @@ -61,3 +61,17 @@ class NotifyTestCase(BaseTestCase): args, kwargs = mock_post.call_args assert "trigger" in kwargs["data"] + + @patch("hc.api.models.requests.post") + def test_slack(self, mock_post): + self._setup_data("slack", "123") + mock_post.return_value.status_code = 200 + + self.channel.notify(self.check) + assert Notification.objects.count() == 1 + + args, kwargs = mock_post.call_args + json = kwargs["json"] + attachment = json["attachments"][0] + fields = {f["title"]: f["value"] for f in attachment["fields"]} + self.assertEqual(fields["Last Ping"], "Never") diff --git a/templates/integrations/slack_message.html b/templates/integrations/slack_message.html deleted file mode 100644 index cfdd8afa..00000000 --- a/templates/integrations/slack_message.html +++ /dev/null @@ -1,8 +0,0 @@ -{% load humanize %} - -{% if check.status == "down" %} -The check "{{ check.name_then_code }}" is DOWN. -Last ping was {{ check.last_ping|naturaltime }} -{% else %} -The check "{{ check.name_then_code }}" received a ping and is now UP. -{% endif %} diff --git a/templates/integrations/slack_message.json b/templates/integrations/slack_message.json new file mode 100644 index 00000000..86beea01 --- /dev/null +++ b/templates/integrations/slack_message.json @@ -0,0 +1,45 @@ +{% load hc_extras humanize %} +{ + "username": "healthchecks.io", + "icon_url": "https://healthchecks.io/static/img/logo@2x.png", + "attachments": [{ + {% if check.status == "up" %} + "color": "good", + {% else %} + "color": "danger", + {% endif %} + + "fallback": "The check \"{{ check.name_then_code|escapejs }}\" is {{ check.status|upper }}.", + "mrkdwn_in": ["fields"], + "text": "“{{ check.name_then_code|escapejs }}” is {{ check.status|upper }}.", + "fields": [ + { + "title": "Period", + "value": "{{ check.timeout|hc_duration }}", + "short": true + }, + { + "title": "Last Ping", + {% if check.last_ping %} + "value": "{{ check.last_ping|naturaltime }}", + {% else %} + "value": "Never", + {% endif %} + "short": true + }, + {% if check.tags_list %} + { + "title": "Tags", + "value": "{% for tag in check.tags_list %}`{{ tag|escapejs }}` {% endfor %}", + "short": true + }, + {% endif %} + { + "title": "Total Pings", + "value": {{ check.n_pings }}, + "short": true + } + ] + }] +} +