diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fae2e0..b1b5fe4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- add github-release-notifier formatter + in [#93](https://github.com/nim65s/matrix-webhook/pull/93) + by [@nrekretep](https://github.com/nrekretep) + ## [v3.8.0] - 2023-04-08 - add a healthcheck for load balancers diff --git a/README.md b/README.md index 59bc724..79b19a5 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,10 @@ Add a Google Chat integration with an URL ending with `?formatter=gitlab_gchat&k Add a Microsoft Teams integration with an URL ending with `?formatter=gitlab_teams&key=API_KEY` +#### Github Release Notifier + +To receiver notifications about new releases of projects hosted at github.com you can add a matrix webhook ending with `?formatter=grn&key=API_KEY` to [Github Release Notifier (grn)](https://github.com/femtopixel/github-release-notifier). + ## Test room [#matrix-webhook:tetaneutral.net](https://matrix.to/#/!DPrUlnwOhBEfYwsDLh:matrix.org) diff --git a/matrix_webhook/formatters.py b/matrix_webhook/formatters.py index 9ed5a01..8ca321b 100644 --- a/matrix_webhook/formatters.py +++ b/matrix_webhook/formatters.py @@ -71,3 +71,17 @@ def gitlab_teams(data, headers): data["body"] = " \n".join(body) return data + + +def grn(data, headers): + """Pretty-print a github release notifier (grn) notification.""" + version, title, author, package = ( + data[k] for k in ["version", "title", "author", "package_name"] + ) + data["body"] = ( + f"### {package} - {version}\n\n{title}\n\n" + f"[{author} released new version **{version}** for **{package}**]" + f"(https://github.com/{package}/releases/tag/{version}).\n\n" + ) + + return data diff --git a/tests/example_grn.json b/tests/example_grn.json new file mode 100644 index 0000000..eb9bc65 --- /dev/null +++ b/tests/example_grn.json @@ -0,0 +1,9 @@ +{ + "date": [2017, 11, 13, 19, 46, 35, 0, 317, 0], + "version": "0.7.2", + "title": "Fixes split modules", + "content": "", + "media": "https://avatars0.githubusercontent.com/u/14236493?s=60&v=4", + "author": "jaymoulin", + "package_name": "jaymoulin/google-music-manager" +} diff --git a/tests/test_grn.py b/tests/test_grn.py new file mode 100644 index 0000000..d69ce59 --- /dev/null +++ b/tests/test_grn.py @@ -0,0 +1,44 @@ +"""Test module for Github Release Notifier (grn) formatter. + +ref https://github.com/femtopixel/github-release-notifier +""" + +import unittest +from pathlib import Path + +import httpx +import nio + +from .start import BOT_URL, FULL_ID, KEY, MATRIX_ID, MATRIX_PW, MATRIX_URL + + +class GithubReleaseNotifierFormatterTest(unittest.IsolatedAsyncioTestCase): + """GRN formatter test class.""" + + async def test_grn_body(self): + """Send a markdown message, and check the result.""" + messages = [] + client = nio.AsyncClient(MATRIX_URL, MATRIX_ID) + + await client.login(MATRIX_PW) + room = await client.room_create() + + with Path("tests/example_grn.json").open() as f: + example_grn_request = f.read() + self.assertEqual( + httpx.post( + f"{BOT_URL}/{room.room_id}", + params={"formatter": "grn", "key": KEY}, + content=example_grn_request, + ).json(), + {"status": 200, "ret": "OK"}, + ) + + sync = await client.sync() + messages = await client.room_messages(room.room_id, sync.next_batch) + await client.close() + + message = messages.chunk[0] + self.assertEqual(message.sender, FULL_ID) + self.assertIn("Fixes split modules", message.body) + self.assertIn("jaymoulin/google-music-manager", message.body)