2021-07-31 07:06:36 -04:00
|
|
|
"""Formatters for matrix webhook."""
|
|
|
|
|
2022-03-03 13:17:40 -05:00
|
|
|
import re
|
|
|
|
|
2021-07-31 07:06:36 -04:00
|
|
|
|
2021-08-27 14:05:08 -04:00
|
|
|
def grafana(data, headers):
|
2022-09-07 09:01:45 -04:00
|
|
|
"""Pretty-print a Grafana (version 8 and older) notification."""
|
2021-07-31 07:06:36 -04:00
|
|
|
text = ""
|
2022-09-07 09:01:45 -04:00
|
|
|
if "ruleName" not in data and "alerts" in data:
|
|
|
|
return grafana_9x(data, headers)
|
2021-07-31 07:06:36 -04:00
|
|
|
if "title" in data:
|
2021-09-13 11:03:57 -04:00
|
|
|
text = "#### " + data["title"] + "\n"
|
2021-07-31 07:06:36 -04:00
|
|
|
if "message" in data:
|
|
|
|
text = text + data["message"] + "\n\n"
|
|
|
|
if "evalMatches" in data:
|
|
|
|
for match in data["evalMatches"]:
|
|
|
|
text = text + "* " + match["metric"] + ": " + str(match["value"]) + "\n"
|
|
|
|
data["body"] = text
|
|
|
|
return data
|
2021-08-27 17:47:07 -04:00
|
|
|
|
|
|
|
|
2022-09-07 09:01:45 -04:00
|
|
|
def grafana_9x(data, headers):
|
|
|
|
"""Pretty-print a Grafana newer than v9.x notification."""
|
|
|
|
text = ""
|
|
|
|
if "title" in data:
|
|
|
|
text = "#### " + data["title"] + "\n"
|
|
|
|
if "message" in data:
|
|
|
|
text = text + data["message"].replace("\n", "\n\n") + "\n\n"
|
|
|
|
data["body"] = text
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2021-08-27 17:47:07 -04:00
|
|
|
def github(data, headers):
|
|
|
|
"""Pretty-print a github notification."""
|
|
|
|
# TODO: Write nice useful formatters. This is only an example.
|
|
|
|
if headers["X-GitHub-Event"] == "push":
|
2022-03-03 18:13:57 -05:00
|
|
|
pusher, ref, a, b, c = (
|
2021-08-27 17:47:07 -04:00
|
|
|
data[k] for k in ["pusher", "ref", "after", "before", "compare"]
|
2022-03-03 18:13:57 -05:00
|
|
|
)
|
2021-08-27 18:40:56 -04:00
|
|
|
pusher = f"[@{pusher['name']}](https://github.com/{pusher['name']})"
|
|
|
|
data["body"] = f"{pusher} pushed on {ref}: [{b} → {a}]({c}):\n\n"
|
2021-08-27 17:47:07 -04:00
|
|
|
for commit in data["commits"]:
|
|
|
|
data["body"] += f"- [{commit['message']}]({commit['url']})\n"
|
|
|
|
else:
|
|
|
|
data["body"] = "notification from github"
|
|
|
|
data["digest"] = headers["X-Hub-Signature-256"].replace("sha256=", "")
|
|
|
|
return data
|
2022-03-03 13:17:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
def gitlab_gchat(data, headers):
|
|
|
|
"""Pretty-print a gitlab notification preformatted for Google Chat."""
|
|
|
|
data["body"] = re.sub("<(.*?)\\|(.*?)>", "[\\2](\\1)", data["body"], re.MULTILINE)
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def gitlab_teams(data, headers):
|
|
|
|
"""Pretty-print a gitlab notification preformatted for Microsoft Teams."""
|
|
|
|
body = []
|
|
|
|
for section in data["sections"]:
|
|
|
|
if "text" in section.keys():
|
|
|
|
text = section["text"].split("\n\n")
|
|
|
|
text = ["* " + t for t in text]
|
|
|
|
body.append("\n" + " \n".join(text))
|
|
|
|
elif all(
|
|
|
|
k in section.keys()
|
|
|
|
for k in ("activityTitle", "activitySubtitle", "activityText")
|
|
|
|
):
|
|
|
|
text = section["activityTitle"] + " " + section["activitySubtitle"] + " → "
|
|
|
|
text += section["activityText"]
|
|
|
|
body.append(text)
|
|
|
|
|
|
|
|
data["body"] = " \n".join(body)
|
|
|
|
return data
|