forked from GithubBackups/healthchecks
Nicer slack messages.
This commit is contained in:
parent
e67d0f58c0
commit
63cc186fa3
@ -27,7 +27,7 @@ class ChecksAdmin(admin.ModelAdmin):
|
|||||||
'all': ('css/admin/checks.css',)
|
'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",
|
list_display = ("id", "name_tags", "created", "code", "status", "email",
|
||||||
"last_ping", "n_pings")
|
"last_ping", "n_pings")
|
||||||
list_select_related = ("user", )
|
list_select_related = ("user", )
|
||||||
|
@ -92,7 +92,7 @@ class Check(models.Model):
|
|||||||
self.channel_set.add(*channels)
|
self.channel_set.add(*channels)
|
||||||
|
|
||||||
def tags_list(self):
|
def tags_list(self):
|
||||||
return self.tags.split(" ")
|
return [t.strip() for t in self.tags.split(" ") if t.strip()]
|
||||||
|
|
||||||
|
|
||||||
class Ping(models.Model):
|
class Ping(models.Model):
|
||||||
@ -148,14 +148,9 @@ class Channel(models.Model):
|
|||||||
|
|
||||||
n.save()
|
n.save()
|
||||||
elif self.kind == "slack":
|
elif self.kind == "slack":
|
||||||
tmpl = "integrations/slack_message.html"
|
tmpl = "integrations/slack_message.json"
|
||||||
text = render_to_string(tmpl, {"check": check})
|
text = render_to_string(tmpl, {"check": check})
|
||||||
payload = {
|
payload = json.loads(text)
|
||||||
"text": text,
|
|
||||||
"username": "healthchecks.io",
|
|
||||||
"icon_url": "https://healthchecks.io/static/img/logo@2x.png"
|
|
||||||
}
|
|
||||||
|
|
||||||
r = requests.post(self.value, json=payload, timeout=5)
|
r = requests.post(self.value, json=payload, timeout=5)
|
||||||
|
|
||||||
n.status = r.status_code
|
n.status = r.status_code
|
||||||
|
15
hc/api/tests/test_check_model.py
Normal file
15
hc/api/tests/test_check_model.py
Normal 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(), [])
|
@ -61,3 +61,17 @@ class NotifyTestCase(BaseTestCase):
|
|||||||
|
|
||||||
args, kwargs = mock_post.call_args
|
args, kwargs = mock_post.call_args
|
||||||
assert "trigger" in kwargs["data"]
|
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")
|
||||||
|
@ -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
templates/integrations/slack_message.json
Normal file
45
templates/integrations/slack_message.json
Normal file
@ -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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user