Files
bugsink/alerts/models.py
Klaas van Schelven 167fe8bbc9 Mattermost Alert Backend
W.r.t. the user-contributed version, this:

* removes commented-out code
* removes channels (not supported at the UI level)
* removes all other things

Said differently: the mattermost Alert Service Backend is simply the Slack version
with edits to make it mattermost-specific (and nothing else).

(In a few places I've edited the slack backend to make comparing easier).

Fix #277
See #253
2025-11-25 12:40:18 +01:00

60 lines
2.7 KiB
Python

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):
project = models.ForeignKey(Project, on_delete=models.DO_NOTHING, related_name="service_configs")
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=kind_choices, max_length=20, default="slack")
config = models.TextField(blank=False)
# Alert backend failure tracking
last_failure_timestamp = models.DateTimeField(null=True, blank=True,
help_text="When the last failure occurred")
last_failure_status_code = models.IntegerField(null=True, blank=True,
help_text="HTTP status code of the failed request")
last_failure_response_text = models.TextField(null=True, blank=True,
help_text="Response text from the failed request")
last_failure_is_json = models.BooleanField(null=True, blank=True,
help_text="Whether the response was valid JSON")
last_failure_error_type = models.CharField(max_length=100, null=True, blank=True,
help_text="Type of error that occurred (e.g., 'requests.HTTPError')")
last_failure_error_message = models.TextField(null=True, blank=True,
help_text="Error message from the exception")
def get_backend(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"""
self.last_failure_timestamp = None
self.last_failure_status_code = None
self.last_failure_response_text = None
self.last_failure_is_json = None
self.last_failure_error_type = None
self.last_failure_error_message = None
def has_recent_failure(self):
"""Check if this config has a recent failure"""
return self.last_failure_timestamp is not None