From 9bf937245e9422211713e0fdf8df5ea51f8295dd Mon Sep 17 00:00:00 2001 From: Peter Kern Date: Sun, 11 Jun 2023 10:24:35 +0200 Subject: [PATCH 01/10] Adds formatter for "github release notifier" notifications --- matrix_webhook/formatters.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/matrix_webhook/formatters.py b/matrix_webhook/formatters.py index 9ed5a01..be3ad33 100644 --- a/matrix_webhook/formatters.py +++ b/matrix_webhook/formatters.py @@ -71,3 +71,12 @@ 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[{author} released new version **{version}** for **{package}**](https://github.com/{package}/releases/tag/{version}).\n\n" + return data From 0ca6115b98f50626e0dd73f78b8a354f9a030930 Mon Sep 17 00:00:00 2001 From: Peter Kern Date: Sun, 11 Jun 2023 10:29:18 +0200 Subject: [PATCH 02/10] Adds documentation for github release notifier integration to README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 59bc724..d1f6d93 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) From 929fc79c2ed642cd6278461e12ce090a36245e1d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 11 Jun 2023 08:35:24 +0000 Subject: [PATCH 03/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- README.md | 2 +- matrix_webhook/formatters.py | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d1f6d93..79b19a5 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ Add a Microsoft Teams integration with an URL ending with `?formatter=gitlab_tea #### 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). +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 diff --git a/matrix_webhook/formatters.py b/matrix_webhook/formatters.py index be3ad33..25c5cd2 100644 --- a/matrix_webhook/formatters.py +++ b/matrix_webhook/formatters.py @@ -75,8 +75,10 @@ def gitlab_teams(data, headers): 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"] + version, title, author, package = ( + data[k] for k in ["version", "title", "author", "package_name"] ) - data["body"] = f"### {package} - {version}\n\n{title}\n\n[{author} released new version **{version}** for **{package}**](https://github.com/{package}/releases/tag/{version}).\n\n" + data[ + "body" + ] = f"### {package} - {version}\n\n{title}\n\n[{author} released new version **{version}** for **{package}**](https://github.com/{package}/releases/tag/{version}).\n\n" return data From 9ac2fc97e3f1085c13459b0f12aa0cc7d09b15a4 Mon Sep 17 00:00:00 2001 From: Peter Kern Date: Sun, 11 Jun 2023 10:41:46 +0200 Subject: [PATCH 04/10] Create example_grn.json --- tests/example_grn.json | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/example_grn.json diff --git a/tests/example_grn.json b/tests/example_grn.json new file mode 100644 index 0000000..1cf8307 --- /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" +} From 236f1aeefe76d60257a46ec7256442194fcbe39e Mon Sep 17 00:00:00 2001 From: Peter Kern Date: Sun, 11 Jun 2023 10:45:11 +0200 Subject: [PATCH 05/10] Create test_grn.py --- tests/test_grn.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tests/test_grn.py diff --git a/tests/test_grn.py b/tests/test_grn.py new file mode 100644 index 0000000..3d5cd69 --- /dev/null +++ b/tests/test_grn.py @@ -0,0 +1,46 @@ +"""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.assertEqual( + message.body, + "#### [Alerting] Panel Title alert\nNotification Message\n\n* Count: 1\n", + ) From 491415257e1b23dd8696973a7e03ba1587c9fb50 Mon Sep 17 00:00:00 2001 From: Peter Kern Date: Sun, 11 Jun 2023 10:48:57 +0200 Subject: [PATCH 06/10] retrigger checks --- tests/test_grn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_grn.py b/tests/test_grn.py index 3d5cd69..123c0bf 100644 --- a/tests/test_grn.py +++ b/tests/test_grn.py @@ -1,4 +1,4 @@ -"""Test module for github release notifier (grn) formatter. +"""Test module for Github Release Notifier (grn) formatter. ref https://github.com/femtopixel/github-release-notifier """ From 6507e3016da5ae7cc293d71773479b20c63329cf Mon Sep 17 00:00:00 2001 From: Guilhem Saurel Date: Sun, 11 Jun 2023 22:37:52 +0200 Subject: [PATCH 07/10] format: split long string --- matrix_webhook/formatters.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/matrix_webhook/formatters.py b/matrix_webhook/formatters.py index 25c5cd2..8ca321b 100644 --- a/matrix_webhook/formatters.py +++ b/matrix_webhook/formatters.py @@ -78,7 +78,10 @@ def grn(data, headers): version, title, author, package = ( data[k] for k in ["version", "title", "author", "package_name"] ) - data[ - "body" - ] = f"### {package} - {version}\n\n{title}\n\n[{author} released new version **{version}** for **{package}**](https://github.com/{package}/releases/tag/{version}).\n\n" + 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 From c7fe50f2cd48f7dfe2e6f0e52b6122abe08f3a67 Mon Sep 17 00:00:00 2001 From: Guilhem Saurel Date: Sun, 11 Jun 2023 22:40:32 +0200 Subject: [PATCH 08/10] changelog: add #93 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) 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 From 8133b7fcf6eac4dd5adaf0b38f7a57b7855d9d46 Mon Sep 17 00:00:00 2001 From: Guilhem Saurel Date: Sun, 11 Jun 2023 22:40:52 +0200 Subject: [PATCH 09/10] tests: typo --- tests/example_grn.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/example_grn.json b/tests/example_grn.json index 1cf8307..eb9bc65 100644 --- a/tests/example_grn.json +++ b/tests/example_grn.json @@ -4,6 +4,6 @@ "title": "Fixes split modules", "content": "", "media": "https://avatars0.githubusercontent.com/u/14236493?s=60&v=4", - "author": "jaymoulin" + "author": "jaymoulin", "package_name": "jaymoulin/google-music-manager" } From a81072d844273c7c46bf7ea05ec39fe6880585e4 Mon Sep 17 00:00:00 2001 From: Guilhem Saurel Date: Sun, 11 Jun 2023 22:44:34 +0200 Subject: [PATCH 10/10] tests: fix expected string --- tests/test_grn.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_grn.py b/tests/test_grn.py index 123c0bf..d69ce59 100644 --- a/tests/test_grn.py +++ b/tests/test_grn.py @@ -40,7 +40,5 @@ class GithubReleaseNotifierFormatterTest(unittest.IsolatedAsyncioTestCase): message = messages.chunk[0] self.assertEqual(message.sender, FULL_ID) - self.assertEqual( - message.body, - "#### [Alerting] Panel Title alert\nNotification Message\n\n* Count: 1\n", - ) + self.assertIn("Fixes split modules", message.body) + self.assertIn("jaymoulin/google-music-manager", message.body)