Skip to content

Commit

Permalink
Fix: Formatting
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 7943353 commit 1ee82be
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
6 changes: 4 additions & 2 deletions mattermost_notify/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
"{highlight}"
)

SHORT_TEMPLATE = ("{status_emoji} {status_text}: {workflow} | {repository} "
"(b {branch}) {highlight}")
SHORT_TEMPLATE = (
"{status_emoji} {status_text}: {workflow} | {repository} "
"(b {branch}) {highlight}"
)

DEFAULT_GIT = "https://github.com"

Expand Down
32 changes: 18 additions & 14 deletions mattermost_notify/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#
# 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):

class Colors:
"""
Colors namespace for mattermost notifications.
The colors are borrowed from bootstrap 5.
Expand All @@ -19,11 +19,9 @@ class Colors(Enum):
WARNING = "#e0a800"
DANGER = "#c82333"


def post(
url: str,
channel: str,
text: SupportsStr,
color: str = Colors.SECONDARY
url: str, channel: str, text: SupportsStr, color: str = Colors.SECONDARY
) -> None:
"""
Post a message to a Mattermost channel.
Expand All @@ -38,14 +36,20 @@ def post(
Raises:
MattermostNotifyError: If the HTTP request fails.
"""

response = httpx.post(url=url, json={"channel": channel, "attachments": [
{
"color": color,
"text": text,
"fallback": text,
}
]})

response = httpx.post(
url=url,
json={
"channel": channel,
"attachments": [
{
"color": color,
"text": text,
"fallback": text,
}
],
},
)

if not response.is_success:
raise MattermostNotifyError(
Expand Down
33 changes: 26 additions & 7 deletions tests/test_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@ def test_success(self, post_mock: MagicMock):
response = post_mock.return_value
response.is_success = True

post("https://some.mattermost.url", "FooChannel", "Some Message", color=Colors.SUCCESS)
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', 'attachments': [
{'color': '#28a745', 'text': 'Some Message', 'fallback': 'Some Message'}
]},
json={
"channel": "FooChannel",
"attachments": [
{
"color": "#28a745",
"text": "Some Message",
"fallback": "Some Message",
}
],
},
)

@patch("mattermost_notify.post.httpx.post", autospec=True)
Expand All @@ -38,7 +50,14 @@ def test_failure(self, post_mock: MagicMock):

post_mock.assert_called_once_with(
url="https://some.mattermost.url",
json={'channel': 'FooChannel', 'attachments': [
{'color': '#6c757d', 'text': 'Some Message', 'fallback': 'Some Message'}
]},
json={
"channel": "FooChannel",
"attachments": [
{
"color": "#6c757d",
"text": "Some Message",
"fallback": "Some Message",
}
],
},
)

0 comments on commit 1ee82be

Please sign in to comment.