Skip to content

Commit

Permalink
Change: Allow for colored messages
Browse files Browse the repository at this point in the history
Ref: DEVOPS-1269
  • Loading branch information
ailox authored and greenbonebot committed Nov 19, 2024
1 parent 1573422 commit 8080a9e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
41 changes: 39 additions & 2 deletions mattermost_notify/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
12 changes: 8 additions & 4 deletions tests/test_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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'}
]},
)

0 comments on commit 8080a9e

Please sign in to comment.