Skip to content

Commit

Permalink
Adding Mattermost webhook support
Browse files Browse the repository at this point in the history
Signed-off-by: Loïc Dubard <loic.dubard@csgroup.eu>
  • Loading branch information
LawiK974 committed Jun 11, 2024
1 parent 89f0ede commit c4d7a07
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ Use [sample config](./config.sample.yaml) as an example.
- [ ] Notification services
- [x] Discord
- [x] Gotify
- [ ] Mattermost
- [x] Mattermost
- [ ] Slack
- [ ] Telegram
- [ ] Automation (GitHub Actions)
- [ ] Coverage tests
- [x] pre-commit
- [ ] Trivy security scan
- [x] Trivy security scan
- [x] Docker build & push (latest + releases)

## References
Expand Down
5 changes: 5 additions & 0 deletions config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ notifications:
webhook: toto
username: kube-notify
# avatar_url: changeme
mattermost:
webhook: https://chat.local/hooks/aaaaaaaaa
# username: kube-notify
# icon_url: https://kubernetes.io/images/k8s-10th-birthday.svg
# channel: Kube-notify
custom_group:
resources:
- name: customResources
Expand Down
30 changes: 30 additions & 0 deletions kube_notify/mattermost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import json

import requests

import kube_notify.logger as logger


def send_mattermost_message(
url: str,
title: str,
description: str,
fields: dict,
channel: str | None = None,
username: str | None = None,
icon_url: str | None = None,
) -> None:
# Construct the HTTP request for sending a message to Mattermost
headers = {"Content-Type": "application/json"}
message = f"### {title}\n\n"
message += f"**Description : {description}**\n\n"

for key, value in fields.items():
message += f"*{key} :*\n{value}\n\n"
data = {"text": message}
channel and data.update({"channel": channel})
username and data.update({"username": username})
icon_url and data.update({"icon_url": icon_url})
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code != 200:
logger.logger.error("Failed to send notification to Mattermost")
12 changes: 12 additions & 0 deletions kube_notify/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import kube_notify.discord as discord
import kube_notify.gotify as gotify
import kube_notify.logger as logger
import kube_notify.mattermost as mattermost
import kube_notify.selectors as selectors


Expand Down Expand Up @@ -72,4 +73,15 @@ async def handle_notify(
description,
fields,
)
if group_values := group.get("mattermost"):
notifs.append(f"{group_name}/mattermost")
mattermost.send_mattermost_message(
group_values["url"],
title,
description,
fields,
group_values.get("channel"),
group_values.get("username"),
group_values.get("icon_url"),
)
logger.logger.info(f"{event_info[0]} [{','.join(notifs)}] {description}")

0 comments on commit c4d7a07

Please sign in to comment.