From e2c90c05b89e5a38f9e7e8ac2ca1baa102ffa9e5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?=
Date: Wed, 3 Feb 2021 09:00:28 +0200
Subject: [PATCH] Add the VICTOROPS_ENABLED setting
---
CHANGELOG.md | 1 +
docker/.env | 1 +
hc/api/tests/test_notify.py | 12 -----
hc/api/tests/test_notify_victorops.py | 44 +++++++++++++++++++
hc/api/transports.py | 3 ++
hc/front/tests/test_add_victorops.py | 7 +++
hc/front/views.py | 3 ++
hc/settings.py | 3 ++
templates/docs/self_hosted_configuration.html | 4 ++
templates/docs/self_hosted_configuration.md | 7 +++
templates/front/channels.html | 2 +
templates/front/welcome.html | 2 +
12 files changed, 77 insertions(+), 12 deletions(-)
create mode 100644 hc/api/tests/test_notify_victorops.py
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2994fc53..0dde0aa1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,7 @@ All notable changes to this project will be documented in this file.
- Add the PAGERTREE_ENABLED setting (#471)
- Add the PROMETHEUS_ENABLED setting (#471)
- Add the SPIKE_ENABLED setting (#471)
+- Add the VICTOROPS_ENABLED setting (#471)
## Bug Fixes
- Fix unwanted HTML escaping in SMS and WhatsApp notifications
diff --git a/docker/.env b/docker/.env
index ec3033be..5b572903 100644
--- a/docker/.env
+++ b/docker/.env
@@ -61,4 +61,5 @@ TWILIO_AUTH=
TWILIO_FROM=
TWILIO_USE_WHATSAPP=False
USE_PAYMENTS=False
+VICTOROPS_ENABLED=True
WEBHOOKS_ENABLED=True
\ No newline at end of file
diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py
index 7997fbc8..e3c49bd2 100644
--- a/hc/api/tests/test_notify.py
+++ b/hc/api/tests/test_notify.py
@@ -41,18 +41,6 @@ class NotifyTestCase(BaseTestCase):
self.assertFalse(mock_post.called)
self.assertEqual(Notification.objects.count(), 0)
- @patch("hc.api.transports.requests.request")
- def test_victorops(self, mock_post):
- self._setup_data("victorops", "123")
- mock_post.return_value.status_code = 200
-
- self.channel.notify(self.check)
- assert Notification.objects.count() == 1
-
- args, kwargs = mock_post.call_args
- payload = kwargs["json"]
- self.assertEqual(payload["message_type"], "CRITICAL")
-
@patch("hc.api.transports.requests.request")
def test_discord(self, mock_post):
v = json.dumps({"webhook": {"url": "123"}})
diff --git a/hc/api/tests/test_notify_victorops.py b/hc/api/tests/test_notify_victorops.py
new file mode 100644
index 00000000..ab84c4ec
--- /dev/null
+++ b/hc/api/tests/test_notify_victorops.py
@@ -0,0 +1,44 @@
+# coding: utf-8
+
+from datetime import timedelta as td
+from unittest.mock import patch
+
+from django.utils.timezone import now
+from hc.api.models import Channel, Check, Notification
+from hc.test import BaseTestCase
+from django.test.utils import override_settings
+
+
+class NotifyTestCase(BaseTestCase):
+ def _setup_data(self, value, status="down", email_verified=True):
+ self.check = Check(project=self.project)
+ self.check.status = status
+ self.check.last_ping = now() - td(minutes=61)
+ self.check.save()
+
+ self.channel = Channel(project=self.project)
+ self.channel.kind = "victorops"
+ self.channel.value = value
+ self.channel.email_verified = email_verified
+ self.channel.save()
+ self.channel.checks.add(self.check)
+
+ @patch("hc.api.transports.requests.request")
+ def test_victorops(self, mock_post):
+ self._setup_data("123")
+ mock_post.return_value.status_code = 200
+
+ self.channel.notify(self.check)
+ assert Notification.objects.count() == 1
+
+ args, kwargs = mock_post.call_args
+ payload = kwargs["json"]
+ self.assertEqual(payload["message_type"], "CRITICAL")
+
+ @override_settings(VICTOROPS_ENABLED=False)
+ def test_it_requires_victorops_enabled(self):
+ self._setup_data("123")
+ self.channel.notify(self.check)
+
+ n = Notification.objects.get()
+ self.assertEqual(n.error, "VictorOps notifications are not enabled.")
diff --git a/hc/api/transports.py b/hc/api/transports.py
index cd5c00e6..6945df02 100644
--- a/hc/api/transports.py
+++ b/hc/api/transports.py
@@ -413,6 +413,9 @@ class Pushover(HttpTransport):
class VictorOps(HttpTransport):
def notify(self, check):
+ if not settings.VICTOROPS_ENABLED:
+ return "VictorOps notifications are not enabled."
+
description = tmpl("victorops_description.html", check=check)
mtype = "CRITICAL" if check.status == "down" else "RECOVERY"
payload = {
diff --git a/hc/front/tests/test_add_victorops.py b/hc/front/tests/test_add_victorops.py
index 47565cc8..88e6d89c 100644
--- a/hc/front/tests/test_add_victorops.py
+++ b/hc/front/tests/test_add_victorops.py
@@ -1,3 +1,4 @@
+from django.test.utils import override_settings
from hc.api.models import Channel
from hc.test import BaseTestCase
@@ -38,3 +39,9 @@ class AddVictorOpsTestCase(BaseTestCase):
self.client.login(username="bob@example.org", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 403)
+
+ @override_settings(VICTOROPS_ENABLED=False)
+ def test_it_handles_disabled_integration(self):
+ self.client.login(username="alice@example.org", password="password")
+ r = self.client.get(self.url)
+ self.assertEqual(r.status_code, 404)
diff --git a/hc/front/views.py b/hc/front/views.py
index 62314027..ad60f183 100644
--- a/hc/front/views.py
+++ b/hc/front/views.py
@@ -312,6 +312,7 @@ def index(request):
"enable_spike": settings.SPIKE_ENABLED is True,
"enable_telegram": settings.TELEGRAM_TOKEN is not None,
"enable_trello": settings.TRELLO_APP_KEY is not None,
+ "enable_victorops": settings.VICTOROPS_ENABLED is True,
"enable_webhooks": settings.WEBHOOKS_ENABLED is True,
"enable_whatsapp": settings.TWILIO_USE_WHATSAPP,
"registration_open": settings.REGISTRATION_OPEN,
@@ -787,6 +788,7 @@ def channels(request, code):
"enable_spike": settings.SPIKE_ENABLED is True,
"enable_telegram": settings.TELEGRAM_TOKEN is not None,
"enable_trello": settings.TRELLO_APP_KEY is not None,
+ "enable_victorops": settings.VICTOROPS_ENABLED is True,
"enable_webhooks": settings.WEBHOOKS_ENABLED is True,
"enable_whatsapp": settings.TWILIO_USE_WHATSAPP,
"use_payments": settings.USE_PAYMENTS,
@@ -1469,6 +1471,7 @@ def add_opsgenie(request, code):
return render(request, "integrations/add_opsgenie.html", ctx)
+@require_setting("VICTOROPS_ENABLED")
@login_required
def add_victorops(request, code):
project = _get_rw_project_for_user(request, code)
diff --git a/hc/settings.py b/hc/settings.py
index fb71720f..aba9d009 100644
--- a/hc/settings.py
+++ b/hc/settings.py
@@ -255,6 +255,9 @@ TWILIO_USE_WHATSAPP = envbool("TWILIO_USE_WHATSAPP", "False")
# Trello
TRELLO_APP_KEY = os.getenv("TRELLO_APP_KEY")
+# VictorOps
+VICTOROPS_ENABLED = envbool("VICTOROPS_ENABLED", "True")
+
# Webhooks
WEBHOOKS_ENABLED = envbool("WEBHOOKS_ENABLED", "True")
diff --git a/templates/docs/self_hosted_configuration.html b/templates/docs/self_hosted_configuration.html
index 739b114c..255917ec 100644
--- a/templates/docs/self_hosted_configuration.html
+++ b/templates/docs/self_hosted_configuration.html
@@ -349,6 +349,10 @@ scheme.
USE_PAYMENTS
Default: False
A boolean that turns on/off billing features.
+VICTOROPS_ENABLED
+Default: True
+A boolean that turns on/off the Splunk On-Call (VictorOps) integration.
+Enabled by default.
WEBHOOKS_ENABLED
Default: True
A boolean that turns on/off the Webhooks integration. Enabled by default.
\ No newline at end of file
diff --git a/templates/docs/self_hosted_configuration.md b/templates/docs/self_hosted_configuration.md
index 3d89085c..1ca0e5f9 100644
--- a/templates/docs/self_hosted_configuration.md
+++ b/templates/docs/self_hosted_configuration.md
@@ -561,6 +561,13 @@ Default: `False`
A boolean that turns on/off billing features.
+## `VICTOROPS_ENABLED` {: #VICTOROPS_ENABLED }
+
+Default: `True`
+
+A boolean that turns on/off the Splunk On-Call (VictorOps) integration.
+Enabled by default.
+
## `WEBHOOKS_ENABLED` {: #WEBHOOKS_ENABLED }
Default: `True`
diff --git a/templates/front/channels.html b/templates/front/channels.html
index 582ea8eb..90654cec 100644
--- a/templates/front/channels.html
+++ b/templates/front/channels.html
@@ -435,6 +435,7 @@
{% endif %}
+ {% if enable_victorops %}
@@ -443,6 +444,7 @@
On-call scheduling, alerting, and incident tracking.
Add Integration
+ {% endif %}
{% if enable_whatsapp %}
diff --git a/templates/front/welcome.html b/templates/front/welcome.html
index 76a713ca..05ae6bb5 100644
--- a/templates/front/welcome.html
+++ b/templates/front/welcome.html
@@ -650,6 +650,7 @@
{% endif %}
+ {% if enable_victorops %}
@@ -659,6 +660,7 @@
+ {% endif %}
{% if enable_whatsapp %}