diff --git a/CHANGELOG.md b/CHANGELOG.md index 132e7f8b..90449772 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. - Webhooks can use different req. bodies and headers for "up" and "down" events. (#249) - Show check's code instead of full URL on 992px - 1200px wide screens. (#253) - Add WhatsApp integration (uses Twilio same as the SMS integration) +- Webhooks support the $TAGS placeholder ### Bug Fixes - Fix badges for tags containing special characters (#240, #237) diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py index 0ce48d8d..caaf5230 100644 --- a/hc/api/tests/test_notify.py +++ b/hc/api/tests/test_notify.py @@ -33,7 +33,7 @@ class NotifyTestCase(BaseTestCase): self.channel.notify(self.check) mock_get.assert_called_with( "get", - u"http://example", + "http://example", headers={"User-Agent": "healthchecks.io"}, timeout=5, ) @@ -72,6 +72,19 @@ class NotifyTestCase(BaseTestCase): n = Notification.objects.get() self.assertEqual(n.error, "Received status code 500") + @patch("hc.api.transports.requests.request") + def test_webhooks_support_tags(self, mock_get): + template = "http://host/$TAGS" + self._setup_data("webhook", template) + self.check.tags = "foo bar" + self.check.save() + + self.channel.notify(self.check) + + args, kwargs = mock_get.call_args + self.assertEqual(args[0], "get") + self.assertEqual(args[1], "http://host/foo%20bar") + @patch("hc.api.transports.requests.request") def test_webhooks_support_variables(self, mock_get): template = "http://host/$CODE/$STATUS/$TAG1/$TAG2/?name=$NAME" @@ -82,7 +95,7 @@ class NotifyTestCase(BaseTestCase): self.channel.notify(self.check) - url = u"http://host/%s/down/foo/bar/?name=Hello%%20World" % self.check.code + url = "http://host/%s/down/foo/bar/?name=Hello%%20World" % self.check.code args, kwargs = mock_get.call_args self.assertEqual(args[0], "get") @@ -118,7 +131,7 @@ class NotifyTestCase(BaseTestCase): self.channel.notify(self.check) - url = u"http://host/%24TAG1" + url = "http://host/%24TAG1" mock_get.assert_called_with( "get", url, headers={"User-Agent": "healthchecks.io"}, timeout=5 ) @@ -135,7 +148,7 @@ class NotifyTestCase(BaseTestCase): @patch("hc.api.transports.requests.request") def test_webhooks_handle_unicode_post_body(self, mock_request): - template = u"http://example.com\n\n(╯°□°)╯︵ ┻━┻" + template = "http://example.com\n\n(╯°□°)╯︵ ┻━┻" self._setup_data("webhook", template) self.check.save() @@ -527,7 +540,7 @@ class NotifyTestCase(BaseTestCase): args, kwargs = mock_post.call_args payload = kwargs["data"] self.assertEqual(payload["To"], "+1234567890") - self.assertFalse(u"\xa0" in payload["Body"]) + self.assertFalse("\xa0" in payload["Body"]) # sent SMS counter should go up self.profile.refresh_from_db() diff --git a/hc/api/transports.py b/hc/api/transports.py index 07f92eac..9475a89f 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -13,7 +13,7 @@ def tmpl(template_name, **ctx): template_path = "integrations/%s" % template_name # \xa0 is non-breaking space. It causes SMS messages to use UCS2 encoding # and cost twice the money. - return render_to_string(template_path, ctx).strip().replace(u"\xa0", " ") + return render_to_string(template_path, ctx).strip().replace("\xa0", " ") class Transport(object): @@ -161,6 +161,9 @@ class Webhook(HttpTransport): if "$NAME" in result: result = result.replace("$NAME", safe(check.name)) + if "$TAGS" in result: + result = result.replace("$TAGS", safe(check.tags)) + if "$TAG" in result: for i, tag in enumerate(check.tags_list()): placeholder = "$TAG%d" % (i + 1) diff --git a/templates/integrations/add_webhook.html b/templates/integrations/add_webhook.html index e739fc16..d28a6e41 100644 --- a/templates/integrations/add_webhook.html +++ b/templates/integrations/add_webhook.html @@ -181,6 +181,10 @@
$STATUS
$TAGS
$TAG1, $TAG2, …