Merge pull request #278 from bugsink/hijacked-pr-253

Mattermost Alert Backend
This commit is contained in:
Klaas van Schelven
2025-11-25 12:47:39 +01:00
committed by GitHub
4 changed files with 259 additions and 7 deletions

View File

@@ -0,0 +1,20 @@
import alerts.models
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("alerts", "0003_messagingserviceconfig_last_failure_error_message_and_more"),
]
# This is the "once and for all" migration since we depend on kinds_choices rather than a list now
operations = [
migrations.AlterField(
model_name="messagingserviceconfig",
name="kind",
field=models.CharField(
choices=alerts.models.kind_choices, default="slack", max_length=20
),
),
]

View File

@@ -2,6 +2,16 @@ from django.db import models
from projects.models import Project
from .service_backends.slack import SlackBackend
from .service_backends.mattermost import MattermostBackend
def kind_choices():
# As a callable to avoid non-DB-affecting migrations for adding new kinds.
# Messaging backends don't need translations since they are brand names.
return [
("slack", "Slack"),
("mattermost", "Mattermost"),
]
class MessagingServiceConfig(models.Model):
@@ -9,7 +19,7 @@ class MessagingServiceConfig(models.Model):
display_name = models.CharField(max_length=100, blank=False,
help_text='For display in the UI, e.g. "#general on company Slack"')
kind = models.CharField(choices=[("slack", "Slack (or compatible)"), ], max_length=20, default="slack")
kind = models.CharField(choices=kind_choices, max_length=20, default="slack")
config = models.TextField(blank=False)
@@ -28,8 +38,12 @@ class MessagingServiceConfig(models.Model):
help_text="Error message from the exception")
def get_backend(self):
# once we have multiple backends: lookup by kind.
return SlackBackend(self)
if self.kind == "slack":
return SlackBackend(self)
elif self.kind == "mattermost":
return MattermostBackend(self)
else:
raise ValueError(f"Unknown backend kind: {self.kind}")
def clear_failure_status(self):
"""Clear all failure tracking fields on successful operation"""

View File

@@ -0,0 +1,210 @@
import json
import requests
from django.utils import timezone
from django import forms
from django.template.defaultfilters import truncatechars
from snappea.decorators import shared_task
from bugsink.app_settings import get_settings
from bugsink.transaction import immediate_atomic
from issues.models import Issue
class MattermostConfigForm(forms.Form):
# NOTE: As of yet this code isn't plugged into the UI (because it requires dynamic loading of the config-specific
# form)
webhook_url = forms.URLField(required=True)
def __init__(self, *args, **kwargs):
config = kwargs.pop("config", None)
super().__init__(*args, **kwargs)
if config:
self.fields["webhook_url"].initial = config.get("webhook_url", "")
def get_config(self):
return {
"webhook_url": self.cleaned_data.get("webhook_url"),
}
def _safe_markdown(text):
# Mattermost uses similar markdown escaping as Slack
return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("*", "\\*").replace("_", "\\_")
def _store_failure_info(service_config_id, exception, response=None):
"""Store failure information in the MessagingServiceConfig with immediate_atomic"""
from alerts.models import MessagingServiceConfig
with immediate_atomic(only_if_needed=True):
try:
config = MessagingServiceConfig.objects.get(id=service_config_id)
config.last_failure_timestamp = timezone.now()
config.last_failure_error_type = type(exception).__name__
config.last_failure_error_message = str(exception)
# Handle requests-specific errors
if response is not None:
config.last_failure_status_code = response.status_code
config.last_failure_response_text = response.text[:2000] # Limit response text size
# Check if response is JSON
try:
json.loads(response.text)
config.last_failure_is_json = True
except (json.JSONDecodeError, ValueError):
config.last_failure_is_json = False
else:
# Non-HTTP errors
config.last_failure_status_code = None
config.last_failure_response_text = None
config.last_failure_is_json = None
config.save()
except MessagingServiceConfig.DoesNotExist:
# Config was deleted while task was running
pass
def _store_success_info(service_config_id):
"""Clear failure information on successful operation"""
from alerts.models import MessagingServiceConfig
with immediate_atomic(only_if_needed=True):
try:
config = MessagingServiceConfig.objects.get(id=service_config_id)
config.clear_failure_status()
config.save()
except MessagingServiceConfig.DoesNotExist:
# Config was deleted while task was running
pass
@shared_task
def mattermost_backend_send_test_message(webhook_url, project_name, display_name, service_config_id):
# See https://developers.mattermost.com/integrate/reference/message-attachments/
data = {"text": "### Test message by Bugsink to test the webhook setup.",
"attachments": [
{
"title": "TEST issue",
"text": "Test message by Bugsink to test the webhook setup.",
"fields": [
{
"title": "project",
"value": _safe_markdown(project_name),
},
{
"title": "message backend",
"value": _safe_markdown(display_name),
},
]
}
]}
try:
result = requests.post(
webhook_url,
data=json.dumps(data),
headers={"Content-Type": "application/json"},
timeout=5,
)
result.raise_for_status()
_store_success_info(service_config_id)
except requests.RequestException as e:
response = getattr(e, 'response', None)
_store_failure_info(service_config_id, e, response)
except Exception as e:
_store_failure_info(service_config_id, e)
@shared_task
def mattermost_backend_send_alert(
webhook_url, issue_id, state_description, alert_article, alert_reason, service_config_id, unmute_reason=None):
issue = Issue.objects.get(id=issue_id)
issue_url = get_settings().BASE_URL + issue.get_absolute_url()
link = f"<{issue_url}|" + _safe_markdown(truncatechars(issue.title().replace("|", ""), 200)) + ">"
data = {"text": "### " + _safe_markdown(truncatechars(issue.title().replace("|", ""), 200)),
"attachments": [
{
"title": f"{alert_reason} issue",
"text": link,
"fields": [],
}
]}
if unmute_reason:
data["attachments"][0]["text"] += "\n\n" + _safe_markdown(unmute_reason)
# assumption: visavis email, project.name is of less importance, because in slack-like things you may (though not
# always) do one-channel per project. more so for site_title (if you have multiple Bugsinks, you'll surely have
# multiple slack channels)
fields = [{
"title": "Project",
"value": _safe_markdown(issue.project.name),
}]
# left as a (possible) TODO, because the amount of refactoring (passing event to this function) is too big for now
# if event.release:
# fields.append({"title": "Release", "value": _safe_markdown(event.release)})
# if event.environment:
# fields.append("title": "Environment", "value": _safe_markdown(event.environment)})
data["attachments"][0]["fields"] += fields
try:
result = requests.post(
webhook_url,
data=json.dumps(data),
headers={"Content-Type": "application/json"},
timeout=5,
)
result.raise_for_status()
_store_success_info(service_config_id)
except requests.RequestException as e:
response = getattr(e, 'response', None)
_store_failure_info(service_config_id, e, response)
except Exception as e:
_store_failure_info(service_config_id, e)
class MattermostBackend:
def __init__(self, service_config):
self.service_config = service_config
def get_form_class(self):
return MattermostConfigForm
def send_test_message(self):
config = json.loads(self.service_config.config)
mattermost_backend_send_test_message.delay(
config["webhook_url"],
self.service_config.project.name,
self.service_config.display_name,
self.service_config.id,
)
def send_alert(self, issue_id, state_description, alert_article, alert_reason, **kwargs):
config = json.loads(self.service_config.config)
mattermost_backend_send_alert.delay(
config["webhook_url"],
issue_id,
state_description,
alert_article,
alert_reason,
self.service_config.id,
**kwargs,
)

View File

@@ -151,6 +151,7 @@ def slack_backend_send_alert(
"type": "header",
"text": {
"type": "plain_text",
# TODO arguably issue.title() should get this location; "later" because I don't have a test env.
"text": f"{alert_reason} issue",
},
},
@@ -218,7 +219,6 @@ def slack_backend_send_alert(
class SlackBackend:
def __init__(self, service_config):
self.service_config = service_config
@@ -226,14 +226,22 @@ class SlackBackend:
return SlackConfigForm
def send_test_message(self):
config = json.loads(self.service_config.config)
slack_backend_send_test_message.delay(
json.loads(self.service_config.config)["webhook_url"],
config["webhook_url"],
self.service_config.project.name,
self.service_config.display_name,
self.service_config.id,
)
def send_alert(self, issue_id, state_description, alert_article, alert_reason, **kwargs):
config = json.loads(self.service_config.config)
slack_backend_send_alert.delay(
json.loads(self.service_config.config)["webhook_url"],
issue_id, state_description, alert_article, alert_reason, self.service_config.id, **kwargs)
config["webhook_url"],
issue_id,
state_description,
alert_article,
alert_reason,
self.service_config.id,
**kwargs,
)