diff --git a/CHANGELOG.md b/CHANGELOG.md index 3db5d2d7..85be1a2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file. - Add rate limiting for Pushover notifications (6 notifications / user / minute) - Add the WEBHOOKS_ENABLED setting (#471) - Add the SLACK_ENABLED setting (#471) +- Add the MATTERMOST_ENABLED setting (#471) ## Bug Fixes - Fix unwanted HTML escaping in SMS and WhatsApp notifications diff --git a/docker/.env b/docker/.env index adf2e164..1d0310d7 100644 --- a/docker/.env +++ b/docker/.env @@ -25,6 +25,7 @@ MASTER_BADGE_LABEL=Mychecks MATRIX_ACCESS_TOKEN= MATRIX_HOMESERVER= MATRIX_USER_ID= +MATTERMOST_ENABLED=True PD_VENDOR_KEY= PING_BODY_LIMIT=10000 PING_EMAIL_DOMAIN=localhost diff --git a/hc/api/tests/test_notify_mattermost.py b/hc/api/tests/test_notify_mattermost.py new file mode 100644 index 00000000..bb49903d --- /dev/null +++ b/hc/api/tests/test_notify_mattermost.py @@ -0,0 +1,31 @@ +# coding: utf-8 + +from datetime import timedelta as td + +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 = "mattermost" + self.channel.value = value + self.channel.email_verified = email_verified + self.channel.save() + self.channel.checks.add(self.check) + + @override_settings(MATTERMOST_ENABLED=False) + def test_it_requires_mattermost_enabled(self): + self._setup_data("123") + self.channel.notify(self.check) + + n = Notification.objects.get() + self.assertEqual(n.error, "Mattermost notifications are not enabled.") diff --git a/hc/api/transports.py b/hc/api/transports.py index 39826256..e93b968d 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -263,9 +263,12 @@ class Webhook(HttpTransport): class Slack(HttpTransport): def notify(self, check): - if not settings.SLACK_ENABLED: + if self.channel.kind == "slack" and not settings.SLACK_ENABLED: return "Slack notifications are not enabled." + if self.channel.kind == "mattermost" and not settings.MATTERMOST_ENABLED: + return "Mattermost notifications are not enabled." + text = tmpl("slack_message.json", check=check) payload = json.loads(text) return self.post(self.channel.slack_webhook_url, json=payload) diff --git a/hc/front/tests/test_add_mattermost.py b/hc/front/tests/test_add_mattermost.py index 7b04e98e..842e3c7f 100644 --- a/hc/front/tests/test_add_mattermost.py +++ b/hc/front/tests/test_add_mattermost.py @@ -1,3 +1,4 @@ +from django.test.utils import override_settings from hc.api.models import Channel from hc.test import BaseTestCase @@ -31,3 +32,9 @@ class AddMattermostTestCase(BaseTestCase): self.client.login(username="bob@example.org", password="password") r = self.client.get(self.url) self.assertEqual(r.status_code, 403) + + @override_settings(MATTERMOST_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 9b6b9171..7a301654 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -295,6 +295,7 @@ def index(request): "enable_discord": settings.DISCORD_CLIENT_ID is not None, "enable_linenotify": settings.LINENOTIFY_CLIENT_ID is not None, "enable_matrix": settings.MATRIX_ACCESS_TOKEN is not None, + "enable_mattermost": settings.MATTERMOST_ENABLED is True, "enable_pdc": settings.PD_VENDOR_KEY is not None, "enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None, "enable_pushover": settings.PUSHOVER_API_TOKEN is not None, @@ -763,6 +764,7 @@ def channels(request, code): "enable_discord": settings.DISCORD_CLIENT_ID is not None, "enable_linenotify": settings.LINENOTIFY_CLIENT_ID is not None, "enable_matrix": settings.MATRIX_ACCESS_TOKEN is not None, + "enable_mattermost": settings.MATTERMOST_ENABLED is True, "enable_pdc": settings.PD_VENDOR_KEY is not None, "enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None, "enable_pushover": settings.PUSHOVER_API_TOKEN is not None, @@ -1210,6 +1212,7 @@ def add_slack_complete(request): return redirect("hc-channels", project.code) +@require_setting("MATTERMOST_ENABLED") @login_required def add_mattermost(request, code): project = _get_rw_project_for_user(request, code) diff --git a/hc/settings.py b/hc/settings.py index 4d053aba..65e00961 100644 --- a/hc/settings.py +++ b/hc/settings.py @@ -199,6 +199,9 @@ MATRIX_HOMESERVER = os.getenv("MATRIX_HOMESERVER") MATRIX_USER_ID = os.getenv("MATRIX_USER_ID") MATRIX_ACCESS_TOKEN = os.getenv("MATRIX_ACCESS_TOKEN") +# Mattermost +MATTERMOST_ENABLED = envbool("MATTERMOST_ENABLED", "True") + # PagerDuty 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 c8249f85..3ac83246 100644 --- a/templates/docs/self_hosted_configuration.html +++ b/templates/docs/self_hosted_configuration.html @@ -140,6 +140,9 @@ integration.

MATRIX_USER_ID

Default: None

The Matrix bot's user identifier, required by the Matrix integration.

+

MATTERMOST_ENABLED

+

Default: True

+

A boolean that turns on/off the Mattermost integration. Enabled by default.

PD_VENDOR_KEY

Default: None

PagerDuty vendor key, @@ -284,7 +287,7 @@ and create a Slack app. When setting up the Slack app, make sure to:

SLACK_CLIENT_SECRET

Default: None

-

The Slack Client Secret, required if SLACK_CLIENT_ID is set. +

The Slack Client Secret. Required if SLACK_CLIENT_ID is set. Look it up at https://api.slack.com/apps/.

SLACK_ENABLED

Default: True

diff --git a/templates/docs/self_hosted_configuration.md b/templates/docs/self_hosted_configuration.md index 722e6d43..cc66d165 100644 --- a/templates/docs/self_hosted_configuration.md +++ b/templates/docs/self_hosted_configuration.md @@ -230,6 +230,12 @@ Default: `None` The Matrix bot's user identifier, required by the Matrix integration. +## `MATTERMOST_ENABLED` {: #MATTERMOST_ENABLED } + +Default: `True` + +A boolean that turns on/off the Mattermost integration. Enabled by default. + ## `PD_VENDOR_KEY` {: #PD_VENDOR_KEY } Default: `None` diff --git a/templates/front/channels.html b/templates/front/channels.html index b3497a94..fa32c652 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -265,6 +265,7 @@ {% endif %} + {% if enable_mattermost %}
  • Mattermost icon @@ -273,6 +274,7 @@

    High Trust Messaging for the Enterprise.

    Add Integration
  • + {% endif %}
  • {% endif %} + {% if enable_mattermost %}
    @@ -469,6 +470,7 @@
    + {% endif %}