Channel support for Mattermost

based on the work in 62fdfb7623, tested for both channels and users

Fix #281
This commit is contained in:
Klaas van Schelven
2025-11-26 09:48:28 +01:00
parent 2a90d6ab1e
commit 164a1b5c5c
2 changed files with 17 additions and 6 deletions

View File

@@ -24,8 +24,6 @@ def url_valid_according_to_discord(url):
class DiscordConfigForm(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):

View File

@@ -13,9 +13,11 @@ 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)
channel = forms.CharField(
required=False,
help_text='Optional: Override channel (e.g., "town-square" or "@username" for DMs)',
)
def __init__(self, *args, **kwargs):
config = kwargs.pop("config", None)
@@ -23,10 +25,12 @@ class MattermostConfigForm(forms.Form):
super().__init__(*args, **kwargs)
if config:
self.fields["webhook_url"].initial = config.get("webhook_url", "")
self.fields["channel"].initial = config.get("channel", "")
def get_config(self):
return {
"webhook_url": self.cleaned_data.get("webhook_url"),
"channel": self.cleaned_data.get("channel"),
}
@@ -85,7 +89,7 @@ def _store_success_info(service_config_id):
@shared_task
def mattermost_backend_send_test_message(webhook_url, project_name, display_name, service_config_id):
def mattermost_backend_send_test_message(webhook_url, project_name, display_name, service_config_id, channel=None):
# See https://developers.mattermost.com/integrate/reference/message-attachments/
data = {"text": "### Test message by Bugsink to test the webhook setup.",
@@ -106,6 +110,9 @@ def mattermost_backend_send_test_message(webhook_url, project_name, display_name
}
]}
if channel:
data["channel"] = channel
try:
result = requests.post(
webhook_url,
@@ -127,7 +134,8 @@ def mattermost_backend_send_test_message(webhook_url, project_name, display_name
@shared_task
def mattermost_backend_send_alert(
webhook_url, issue_id, state_description, alert_article, alert_reason, service_config_id, unmute_reason=None):
webhook_url, issue_id, state_description, alert_article, alert_reason, service_config_id, unmute_reason=None,
channel=None):
issue = Issue.objects.get(id=issue_id)
@@ -162,6 +170,9 @@ def mattermost_backend_send_alert(
data["attachments"][0]["fields"] += fields
if channel:
data["channel"] = channel
try:
result = requests.post(
webhook_url,
@@ -196,6 +207,7 @@ class MattermostBackend:
self.service_config.project.name,
self.service_config.display_name,
self.service_config.id,
channel=config.get("channel"),
)
def send_alert(self, issue_id, state_description, alert_article, alert_reason, **kwargs):
@@ -207,5 +219,6 @@ class MattermostBackend:
alert_article,
alert_reason,
self.service_config.id,
channel=config.get("channel"),
**kwargs,
)