Browse Source

Nicer slack messages.

pull/40/head
Pēteris Caune 9 years ago
parent
commit
63cc186fa3
6 changed files with 78 additions and 17 deletions
  1. +1
    -1
      hc/api/admin.py
  2. +3
    -8
      hc/api/models.py
  3. +15
    -0
      hc/api/tests/test_check_model.py
  4. +14
    -0
      hc/api/tests/test_notify.py
  5. +0
    -8
      templates/integrations/slack_message.html
  6. +45
    -0
      templates/integrations/slack_message.json

+ 1
- 1
hc/api/admin.py View File

@ -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", )


+ 3
- 8
hc/api/models.py View File

@ -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/[email protected]"
}
payload = json.loads(text)
r = requests.post(self.value, json=payload, timeout=5)
n.status = r.status_code


+ 15
- 0
hc/api/tests/test_check_model.py View File

@ -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(), [])

+ 14
- 0
hc/api/tests/test_notify.py View File

@ -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")

+ 0
- 8
templates/integrations/slack_message.html View File

@ -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 %}

+ 45
- 0
templates/integrations/slack_message.json View File

@ -0,0 +1,45 @@
{% load hc_extras humanize %}
{
"username": "healthchecks.io",
"icon_url": "https://healthchecks.io/static/img/[email protected]",
"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
}
]
}]
}

Loading…
Cancel
Save