2021-07-31 07:06:36 -04:00
|
|
|
"""Formatters for matrix webhook."""
|
|
|
|
|
|
|
|
|
2021-08-27 14:05:08 -04:00
|
|
|
def grafana(data, headers):
|
2021-07-31 07:06:36 -04:00
|
|
|
"""Pretty-print a grafana notification."""
|
|
|
|
text = ""
|
|
|
|
if "title" in data:
|
|
|
|
text = "### " + data["title"] + "\n"
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
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":
|
|
|
|
pusher, ref, a, b, c = [
|
|
|
|
data[k] for k in ["pusher", "ref", "after", "before", "compare"]
|
|
|
|
]
|
|
|
|
pusher = f"[{pusher['name']}](https://github.com/{pusher['name']})"
|
|
|
|
data["body"] = f"@{pusher} pushed on {ref}: [{b} → {a}]({c}):\n\n"
|
|
|
|
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
|