From 419d96da7a4d1ad1478ee05f618a1ccd709035fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?=
Date: Fri, 29 Jan 2021 14:21:02 +0200
Subject: [PATCH] Add the PAGERTREE_ENABLED setting
---
CHANGELOG.md | 1 +
docker/.env | 1 +
hc/api/tests/test_notify.py | 12 -----
hc/api/tests/test_notify_pagertree.py | 44 +++++++++++++++++++
hc/api/transports.py | 3 ++
hc/front/tests/test_add_pagertree.py | 7 +++
hc/front/views.py | 3 ++
hc/settings.py | 3 ++
templates/docs/self_hosted_configuration.html | 5 ++-
templates/docs/self_hosted_configuration.md | 6 +++
templates/front/channels.html | 2 +
templates/front/welcome.html | 2 +
12 files changed, 76 insertions(+), 13 deletions(-)
create mode 100644 hc/api/tests/test_notify_pagertree.py
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 896e5472..47be1acc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file.
- Add the MSTEAMS_ENABLED setting (#471)
- Add the OPSGENIE_ENABLED setting (#471)
- Add the PD_ENABLED setting (#471)
+- Add the PAGERTREE_ENABLED setting (#471)
## Bug Fixes
- Fix unwanted HTML escaping in SMS and WhatsApp notifications
diff --git a/docker/.env b/docker/.env
index 3cc181a6..4756b19e 100644
--- a/docker/.env
+++ b/docker/.env
@@ -28,6 +28,7 @@ MATRIX_USER_ID=
MATTERMOST_ENABLED=True
MSTEAMS_ENABLED=True
OPSGENIE_ENABLED=True
+PAGERTREE_ENABLED=True
PD_ENABLED=True
PD_VENDOR_KEY=
PING_BODY_LIMIT=10000
diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py
index dd94a433..7997fbc8 100644
--- a/hc/api/tests/test_notify.py
+++ b/hc/api/tests/test_notify.py
@@ -25,18 +25,6 @@ class NotifyTestCase(BaseTestCase):
self.channel.save()
self.channel.checks.add(self.check)
- @patch("hc.api.transports.requests.request")
- def test_pagertree(self, mock_post):
- self._setup_data("pagertree", "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["event_type"], "trigger")
-
@patch("hc.api.transports.requests.request")
def test_pagerteam(self, mock_post):
self._setup_data("pagerteam", "123")
diff --git a/hc/api/tests/test_notify_pagertree.py b/hc/api/tests/test_notify_pagertree.py
new file mode 100644
index 00000000..9ccccee5
--- /dev/null
+++ b/hc/api/tests/test_notify_pagertree.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 = "pagertree"
+ 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_pagertree(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["event_type"], "trigger")
+
+ @override_settings(PAGERTREE_ENABLED=False)
+ def test_it_requires_pagertree_enabled(self):
+ self._setup_data("123")
+ self.channel.notify(self.check)
+
+ n = Notification.objects.get()
+ self.assertEqual(n.error, "PagerTree notifications are not enabled.")
diff --git a/hc/api/transports.py b/hc/api/transports.py
index c0bda0ce..c516f110 100644
--- a/hc/api/transports.py
+++ b/hc/api/transports.py
@@ -336,6 +336,9 @@ class PagerDuty(HttpTransport):
class PagerTree(HttpTransport):
def notify(self, check):
+ if not settings.PAGERTREE_ENABLED:
+ return "PagerTree notifications are not enabled."
+
url = self.channel.value
headers = {"Conent-Type": "application/json"}
payload = {
diff --git a/hc/front/tests/test_add_pagertree.py b/hc/front/tests/test_add_pagertree.py
index 04b61325..769b8d91 100644
--- a/hc/front/tests/test_add_pagertree.py
+++ b/hc/front/tests/test_add_pagertree.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 AddPagerTreeTestCase(BaseTestCase):
self.client.login(username="bob@example.org", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 403)
+
+ @override_settings(PAGERTREE_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 da6c54fb..7e503088 100644
--- a/hc/front/views.py
+++ b/hc/front/views.py
@@ -298,6 +298,7 @@ def index(request):
"enable_mattermost": settings.MATTERMOST_ENABLED is True,
"enable_msteams": settings.MSTEAMS_ENABLED is True,
"enable_opsgenie": settings.OPSGENIE_ENABLED is True,
+ "enable_pagertree": settings.PAGERTREE_ENABLED is True,
"enable_pd": settings.PD_ENABLED is True,
"enable_pdc": settings.PD_VENDOR_KEY is not None,
"enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None,
@@ -770,6 +771,7 @@ def channels(request, code):
"enable_mattermost": settings.MATTERMOST_ENABLED is True,
"enable_msteams": settings.MSTEAMS_ENABLED is True,
"enable_opsgenie": settings.OPSGENIE_ENABLED is True,
+ "enable_pagertree": settings.PAGERTREE_ENABLED is True,
"enable_pd": settings.PD_ENABLED is True,
"enable_pdc": settings.PD_VENDOR_KEY is not None,
"enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None,
@@ -1106,6 +1108,7 @@ def add_pdc_complete(request, code, state):
return redirect("hc-channels", project.code)
+@require_setting("PAGERTREE_ENABLED")
@login_required
def add_pagertree(request, code):
project = _get_rw_project_for_user(request, code)
diff --git a/hc/settings.py b/hc/settings.py
index 3ba0e986..6b559a75 100644
--- a/hc/settings.py
+++ b/hc/settings.py
@@ -208,6 +208,9 @@ MSTEAMS_ENABLED = envbool("MSTEAMS_ENABLED", "True")
# Opsgenie
OPSGENIE_ENABLED = envbool("OPSGENIE_ENABLED", "True")
+# PagerTree
+PAGERTREE_ENABLED = envbool("PAGERTREE_ENABLED", "True")
+
# PagerDuty
PD_ENABLED = envbool("PD_ENABLED", "True")
PD_VENDOR_KEY = os.getenv("PD_VENDOR_KEY")
diff --git a/templates/docs/self_hosted_configuration.html b/templates/docs/self_hosted_configuration.html
index 2935e1cd..770df9b1 100644
--- a/templates/docs/self_hosted_configuration.html
+++ b/templates/docs/self_hosted_configuration.html
@@ -149,9 +149,12 @@ integration.
OPSGENIE_ENABLED
Default: True
A boolean that turns on/off the Opsgenie integration. Enabled by default.
+
+Default: True
+A boolean that turns on/off the PagerTree integration. Enabled by default.
PD_ENABLED
Default: True
-A boolean that turns on/off the Pagerduty integration. Enabled by default.
+A boolean that turns on/off the PagerDuty integration. Enabled by default.
PD_VENDOR_KEY
Default: None
PagerDuty vendor key, used by the PagerDuty integration.
diff --git a/templates/docs/self_hosted_configuration.md b/templates/docs/self_hosted_configuration.md
index 6c24c070..5d295357 100644
--- a/templates/docs/self_hosted_configuration.md
+++ b/templates/docs/self_hosted_configuration.md
@@ -248,6 +248,12 @@ Default: `True`
A boolean that turns on/off the Opsgenie integration. Enabled by default.
+## `PAGERTREE_ENABLED` {: #PAGERTREE_ENABLED }
+
+Default: `True`
+
+A boolean that turns on/off the PagerTree integration. Enabled by default.
+
## `PD_ENABLED` {: #PD_ENABLED }
Default: `True`
diff --git a/templates/front/channels.html b/templates/front/channels.html
index ff6f165b..eae148cc 100644
--- a/templates/front/channels.html
+++ b/templates/front/channels.html
@@ -314,6 +314,7 @@
{% endif %}
+ {% if enable_pagertree %}
@@ -322,6 +323,7 @@
DevOps Incident Management - On-Call Schedules, Alerts, & Notifications.
Add Integration
+ {% endif %}
{% if enable_call %}
diff --git a/templates/front/welcome.html b/templates/front/welcome.html
index 1366dffe..7d141ecc 100644
--- a/templates/front/welcome.html
+++ b/templates/front/welcome.html
@@ -518,6 +518,7 @@
{% endif %}
+ {% if enable_pagertree %}
@@ -527,6 +528,7 @@
+ {% endif %}
{% if enable_call %}