diff --git a/matrix_webhook/formatters/__pycache__/github.cpython-310.pyc b/matrix_webhook/formatters/__pycache__/github.cpython-310.pyc new file mode 100644 index 0000000..ac481ee Binary files /dev/null and b/matrix_webhook/formatters/__pycache__/github.cpython-310.pyc differ diff --git a/matrix_webhook/formatters/github.py b/matrix_webhook/formatters/github.py new file mode 100644 index 0000000..9f6226e --- /dev/null +++ b/matrix_webhook/formatters/github.py @@ -0,0 +1,19 @@ +import plugins + + +@plugins.register +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 diff --git a/matrix_webhook/formatters/grafana.py b/matrix_webhook/formatters/grafana.py new file mode 100644 index 0000000..e78344f --- /dev/null +++ b/matrix_webhook/formatters/grafana.py @@ -0,0 +1,16 @@ +import plugins + + +@plugins.register +def grafana(data, headers): + """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 diff --git a/matrix_webhook/formatters.py b/matrix_webhook/formatters/pingdom.py similarity index 65% rename from matrix_webhook/formatters.py rename to matrix_webhook/formatters/pingdom.py index 48deaee..1bffb2f 100644 --- a/matrix_webhook/formatters.py +++ b/matrix_webhook/formatters/pingdom.py @@ -1,38 +1,7 @@ -"""Formatters for matrix webhook.""" -from datetime import datetime - - -def grafana(data, headers): - """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 - - -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 +import plugins +@plugin.register def pingdom(data, headers): """Pretty-print a pingdom notification.""" #JSON data formatting was obtained from https://www.pingdom.com/resources/webhooks/ diff --git a/matrix_webhook/handler.py b/matrix_webhook/handler.py index 3460810..b3c81df 100644 --- a/matrix_webhook/handler.py +++ b/matrix_webhook/handler.py @@ -4,6 +4,7 @@ import json import logging from http import HTTPStatus from hmac import HMAC +import plugins from markdown import markdown @@ -18,6 +19,7 @@ async def matrix_webhook(request): This one handles a POST, checks its content, and forwards it to the matrix room. """ + formatters = plugins.names_factory(__package__) LOGGER.debug(f"Handling {request=}") data_b = await request.read()