Browse Source

Webhooks support the $TAGS placeholder

pull/272/head
Pēteris Caune 6 years ago
parent
commit
479208abf0
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
4 changed files with 27 additions and 6 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +18
    -5
      hc/api/tests/test_notify.py
  3. +4
    -1
      hc/api/transports.py
  4. +4
    -0
      templates/integrations/add_webhook.html

+ 1
- 0
CHANGELOG.md View File

@ -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) - 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) - Show check's code instead of full URL on 992px - 1200px wide screens. (#253)
- Add WhatsApp integration (uses Twilio same as the SMS integration) - Add WhatsApp integration (uses Twilio same as the SMS integration)
- Webhooks support the $TAGS placeholder
### Bug Fixes ### Bug Fixes
- Fix badges for tags containing special characters (#240, #237) - Fix badges for tags containing special characters (#240, #237)


+ 18
- 5
hc/api/tests/test_notify.py View File

@ -33,7 +33,7 @@ class NotifyTestCase(BaseTestCase):
self.channel.notify(self.check) self.channel.notify(self.check)
mock_get.assert_called_with( mock_get.assert_called_with(
"get", "get",
u"http://example",
"http://example",
headers={"User-Agent": "healthchecks.io"}, headers={"User-Agent": "healthchecks.io"},
timeout=5, timeout=5,
) )
@ -72,6 +72,19 @@ class NotifyTestCase(BaseTestCase):
n = Notification.objects.get() n = Notification.objects.get()
self.assertEqual(n.error, "Received status code 500") 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") @patch("hc.api.transports.requests.request")
def test_webhooks_support_variables(self, mock_get): def test_webhooks_support_variables(self, mock_get):
template = "http://host/$CODE/$STATUS/$TAG1/$TAG2/?name=$NAME" template = "http://host/$CODE/$STATUS/$TAG1/$TAG2/?name=$NAME"
@ -82,7 +95,7 @@ class NotifyTestCase(BaseTestCase):
self.channel.notify(self.check) 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 args, kwargs = mock_get.call_args
self.assertEqual(args[0], "get") self.assertEqual(args[0], "get")
@ -118,7 +131,7 @@ class NotifyTestCase(BaseTestCase):
self.channel.notify(self.check) self.channel.notify(self.check)
url = u"http://host/%24TAG1"
url = "http://host/%24TAG1"
mock_get.assert_called_with( mock_get.assert_called_with(
"get", url, headers={"User-Agent": "healthchecks.io"}, timeout=5 "get", url, headers={"User-Agent": "healthchecks.io"}, timeout=5
) )
@ -135,7 +148,7 @@ class NotifyTestCase(BaseTestCase):
@patch("hc.api.transports.requests.request") @patch("hc.api.transports.requests.request")
def test_webhooks_handle_unicode_post_body(self, mock_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._setup_data("webhook", template)
self.check.save() self.check.save()
@ -527,7 +540,7 @@ class NotifyTestCase(BaseTestCase):
args, kwargs = mock_post.call_args args, kwargs = mock_post.call_args
payload = kwargs["data"] payload = kwargs["data"]
self.assertEqual(payload["To"], "+1234567890") self.assertEqual(payload["To"], "+1234567890")
self.assertFalse(u"\xa0" in payload["Body"])
self.assertFalse("\xa0" in payload["Body"])
# sent SMS counter should go up # sent SMS counter should go up
self.profile.refresh_from_db() self.profile.refresh_from_db()


+ 4
- 1
hc/api/transports.py View File

@ -13,7 +13,7 @@ def tmpl(template_name, **ctx):
template_path = "integrations/%s" % template_name template_path = "integrations/%s" % template_name
# \xa0 is non-breaking space. It causes SMS messages to use UCS2 encoding # \xa0 is non-breaking space. It causes SMS messages to use UCS2 encoding
# and cost twice the money. # 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): class Transport(object):
@ -161,6 +161,9 @@ class Webhook(HttpTransport):
if "$NAME" in result: if "$NAME" in result:
result = result.replace("$NAME", safe(check.name)) result = result.replace("$NAME", safe(check.name))
if "$TAGS" in result:
result = result.replace("$TAGS", safe(check.tags))
if "$TAG" in result: if "$TAG" in result:
for i, tag in enumerate(check.tags_list()): for i, tag in enumerate(check.tags_list()):
placeholder = "$TAG%d" % (i + 1) placeholder = "$TAG%d" % (i + 1)


+ 4
- 0
templates/integrations/add_webhook.html View File

@ -181,6 +181,10 @@
<th><code>$STATUS</code></th> <th><code>$STATUS</code></th>
<td>Check's current status ("up" or "down")</td> <td>Check's current status ("up" or "down")</td>
</tr> </tr>
<tr>
<th><code>$TAGS</code></th>
<td>Check's tags, separated by spaces</td>
</tr>
<tr> <tr>
<th><code>$TAG1, $TAG2, …</code></th> <th><code>$TAG1, $TAG2, …</code></th>
<td>Value of the first tag, the second tag, …</td> <td>Value of the first tag, the second tag, …</td>


Loading…
Cancel
Save