From ebe5d2d91d4512d9c11266293b49d20039fafd9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 1 Oct 2015 23:47:03 +0300 Subject: [PATCH] HipChat integration --- hc/api/admin.py | 5 +++++ hc/api/migrations/0013_auto_20151001_2029.py | 19 +++++++++++++++++++ hc/api/models.py | 14 +++++++++++++- static/js/channels.js | 7 ++++--- templates/front/channels.html | 11 +++++++++-- templates/front/welcome.html | 5 +++-- templates/hipchat_message.html | 8 ++++++++ 7 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 hc/api/migrations/0013_auto_20151001_2029.py create mode 100644 templates/hipchat_message.html diff --git a/hc/api/admin.py b/hc/api/admin.py index f43e8c15..2ed84e53 100644 --- a/hc/api/admin.py +++ b/hc/api/admin.py @@ -67,6 +67,7 @@ class ChannelsAdmin(admin.ModelAdmin): list_select_related = ("user", ) list_display = ("id", "code", "email", "formatted_kind", "value", "num_notifications") + list_filter = ("kind", ) def email(self, obj): return obj.user.email if obj.user else None @@ -76,6 +77,10 @@ class ChannelsAdmin(admin.ModelAdmin): return "PagerDuty" elif obj.kind == "webhook": return "Webhook" + elif obj.kind == "slack": + return "Slack" + elif obj.kind == "hipchat": + return "HipChat" elif obj.kind == "email" and obj.email_verified: return "Email" elif obj.kind == "email" and not obj.email_verified: diff --git a/hc/api/migrations/0013_auto_20151001_2029.py b/hc/api/migrations/0013_auto_20151001_2029.py new file mode 100644 index 00000000..8a045cd4 --- /dev/null +++ b/hc/api/migrations/0013_auto_20151001_2029.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0012_auto_20150930_1922'), + ] + + operations = [ + migrations.AlterField( + model_name='channel', + name='kind', + field=models.CharField(max_length=20, choices=[('email', 'Email'), ('webhook', 'Webhook'), ('hipchat', 'HipChat'), ('slack', 'Slack'), ('pd', 'PagerDuty')]), + ), + ] diff --git a/hc/api/models.py b/hc/api/models.py index b6f5b7c9..6fe4bd51 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -20,6 +20,7 @@ STATUSES = (("up", "Up"), ("down", "Down"), ("new", "New")) DEFAULT_TIMEOUT = td(days=1) DEFAULT_GRACE = td(hours=1) CHANNEL_KINDS = (("email", "Email"), ("webhook", "Webhook"), + ("hipchat", "HipChat"), ("slack", "Slack"), ("pd", "PagerDuty")) @@ -132,7 +133,18 @@ class Channel(models.Model): "icon_url": "https://healthchecks.io/static/img/logo@2x.png" } - r = requests.post(self.value, data=json.dumps(payload)) + r = requests.post(self.value, json=payload) + + n.status = r.status_code + n.save() + elif self.kind == "hipchat": + text = render_to_string("hipchat_message.html", {"check": check}) + payload = { + "message": text, + "color": "green" if check.status == "up" else "red", + } + + r = requests.post(self.value, json=payload) n.status = r.status_code n.save() diff --git a/static/js/channels.js b/static/js/channels.js index 7ed85e34..fa2264a4 100644 --- a/static/js/channels.js +++ b/static/js/channels.js @@ -3,16 +3,17 @@ $(function() { email: "address@example.org", webhook: "http://", slack: "https://hooks.slack.com/...", + hipchat: "https://api.hipchat.com/...", pd: "service key" } - $("#add-check-kind").change(function() { + $("#add-channel-kind").change(function() { $(".channels-add-help p").hide(); - var v = $("#add-check-kind").val(); + var v = $("#add-channel-kind").val(); $(".channels-add-help p." + v).show(); - $("#add-check-value").attr("placeholder", placeholders[v]); + $("#add-channel-value").attr("placeholder", placeholders[v]); }); $(".edit-checks").click(function() { diff --git a/templates/front/channels.html b/templates/front/channels.html index d975a4f6..77167aaf 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -22,6 +22,7 @@ {% if ch.kind == "email" %} Email {% endif %} {% if ch.kind == "webhook" %} Webhook {% endif %} {% if ch.kind == "slack" %} Slack {% endif %} + {% if ch.kind == "hipchat" %} HipChat {% endif %} {% if ch.kind == "pd" %} PagerDuty {% endif %} @@ -66,17 +67,18 @@
- + {% csrf_token %} up or down.

+

+ Healthchecks.io will post to a HipChat channel when + a check goes + up or down. +

diff --git a/templates/front/welcome.html b/templates/front/welcome.html index 03ba065a..26c0af4a 100644 --- a/templates/front/welcome.html +++ b/templates/front/welcome.html @@ -223,8 +223,9 @@

You can specify additional email addresses, webhooks, - PagerDuty - and Slack + PagerDuty, + Slack + and HipChat accounts to send notifications to.

diff --git a/templates/hipchat_message.html b/templates/hipchat_message.html new file mode 100644 index 00000000..cfdd8afa --- /dev/null +++ b/templates/hipchat_message.html @@ -0,0 +1,8 @@ +{% 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 %}