Skip to content

Commit

Permalink
[mattermost] Handle config defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Jan 3, 2025
1 parent cd383bd commit 4974381
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 6 additions & 6 deletions law/contrib/mattermost/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ def notify_mattermost(

# get default settings
if not hook_url:
hook_url = cfg.get_expanded("notifications", "mattermost_hook_url")
hook_url = cfg.get_expanded("notifications", "mattermost_hook_url", default=None)
if not channel:
channel = cfg.get_expanded("notifications", "mattermost_channel")
channel = cfg.get_expanded("notifications", "mattermost_channel", default=None)
if not user:
user = cfg.get_expanded("notifications", "mattermost_user")
user = cfg.get_expanded("notifications", "mattermost_user", default=None)
if not mention_user:
mention_user = cfg.get_expanded("notifications", "mattermost_mention_user")
mention_user = cfg.get_expanded("notifications", "mattermost_mention_user", default=None)
if not icon_url:
icon_url = cfg.get_expanded("notifications", "mattermost_icon_url")
icon_url = cfg.get_expanded("notifications", "mattermost_icon_url", default=None)
if not icon_emoji:
icon_emoji = cfg.get_expanded("notifications", "mattermost_icon_emoji")
icon_emoji = cfg.get_expanded("notifications", "mattermost_icon_emoji", default=None)

if not hook_url:
logger.warning(f"cannot send Mattermost notification, hook_url ({hook_url}) empty")
Expand Down
8 changes: 6 additions & 2 deletions law/contrib/mattermost/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def notify(cls, success: bool, title: str, content: dict[str, Any], **kwargs) ->

# overwrite title
cfg = Config.instance()
header = cfg.get_expanded("notifications", "mattermost_header")
header = cfg.get_expanded("notifications", "mattermost_header", default=None)
task_block = f"```\n{content['Task']}\n```"
title = f"{header}\n{task_block}" if header else task_block
del content["Task"]
Expand All @@ -46,7 +46,11 @@ def notify(cls, success: bool, title: str, content: dict[str, Any], **kwargs) ->
# prepend the status text to the message content
cfg = Config.instance()
status_text = "success" if success else "failure"
status_emoji = cfg.get_expanded("notifications", f"mattermost_{status_text}_emoji")
status_emoji = cfg.get_expanded(
"notifications",
f"mattermost_{status_text}_emoji",
default=None,
)
if status_emoji:
status_text += " " + status_emoji
content["Status"] = status_text
Expand Down

0 comments on commit 4974381

Please sign in to comment.