From 8080a9e03c0e2a4a2b68378f1f8139c5f76399ed Mon Sep 17 00:00:00 2001 From: Paul Scheunemann Date: Mon, 18 Nov 2024 15:48:27 +0100 Subject: [PATCH] Change: Allow for colored messages Ref: DEVOPS-1269 --- mattermost_notify/post.py | 41 +++++++++++++++++++++++++++++++++++++-- tests/test_post.py | 12 ++++++++---- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/mattermost_notify/post.py b/mattermost_notify/post.py index 28b39a4..94c1759 100644 --- a/mattermost_notify/post.py +++ b/mattermost_notify/post.py @@ -2,14 +2,51 @@ # # SPDX-License-Identifier: GPL-3.0-or-later +from enum import Enum import httpx from pontos.typing import SupportsStr from mattermost_notify.errors import MattermostNotifyError +class Colors(Enum): + """ + Colors namespace for mattermost notifications. + The colors are borrowed from bootstrap 5. + """ + + SECONDARY = "#6c757d" + SUCCESS = "#28a745" + WARNING = "#e0a800" + DANGER = "#c82333" + +def post( + url: str, + channel: str, + text: SupportsStr, + color: str = Colors.SECONDARY +) -> None: + """ + Post a message to a Mattermost channel. + + Args: + url (str): The Mattermost webhook URL. + channel (str): The channel name to post the message to. + text (SupportsStr): The message content, markdown formatted. + color (str, optional): The color of the message, visible as a + border on the left. Defaults to Colors.SECONDARY. + + Raises: + MattermostNotifyError: If the HTTP request fails. + """ + + response = httpx.post(url=url, json={"channel": channel, "attachments": [ + { + "color": color, + "text": text, + "fallback": text, + } + ]}) -def post(url: str, channel: str, text: SupportsStr) -> None: - response = httpx.post(url=url, json={"channel": channel, "text": text}) if not response.is_success: raise MattermostNotifyError( "Failed to post on Mattermost. HTTP status was " diff --git a/tests/test_post.py b/tests/test_post.py index 6b26d6f..d821316 100644 --- a/tests/test_post.py +++ b/tests/test_post.py @@ -6,7 +6,7 @@ from unittest.mock import MagicMock, patch from mattermost_notify.errors import MattermostNotifyError -from mattermost_notify.post import post +from mattermost_notify.post import Colors, post class PostTestCase(unittest.TestCase): @@ -15,11 +15,13 @@ def test_success(self, post_mock: MagicMock): response = post_mock.return_value response.is_success = True - post("https://some.mattermost.url", "FooChannel", "Some Message") + post("https://some.mattermost.url", "FooChannel", "Some Message", color=Colors.SUCCESS) post_mock.assert_called_once_with( url="https://some.mattermost.url", - json={"channel": "FooChannel", "text": "Some Message"}, + json={'channel': 'FooChannel', 'attachments': [ + {'color': '#28a745', 'text': 'Some Message', 'fallback': 'Some Message'} + ]}, ) @patch("mattermost_notify.post.httpx.post", autospec=True) @@ -36,5 +38,7 @@ def test_failure(self, post_mock: MagicMock): post_mock.assert_called_once_with( url="https://some.mattermost.url", - json={"channel": "FooChannel", "text": "Some Message"}, + json={'channel': 'FooChannel', 'attachments': [ + {'color': '#6c757d', 'text': 'Some Message', 'fallback': 'Some Message'} + ]}, )