From 03f87c3450c17b0e9790bc0a517d6f648c8cbbc3 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sun, 10 Mar 2024 13:30:38 +0100 Subject: [PATCH] Add: announce workflow failures when configured --- plugins/GitHub/events/workflow.py | 63 +++++++++++++++++++++++++++++ plugins/GitHub/plugin.py | 1 + plugins/GitHub/protocols/discord.py | 11 +++++ plugins/GitHub/protocols/irc.py | 11 +++++ 4 files changed, 86 insertions(+) create mode 100644 plugins/GitHub/events/workflow.py diff --git a/plugins/GitHub/events/workflow.py b/plugins/GitHub/events/workflow.py new file mode 100644 index 0000000..71877b8 --- /dev/null +++ b/plugins/GitHub/events/workflow.py @@ -0,0 +1,63 @@ +import re + +from ..helpers.github import router +from ..helpers import protocols + + +def filter_func(protocols, payload): + # Filter based on some keywords + for protocol, userdata in protocols.items(): + if protocol == "except-by": + # Check for every 'except' if we hit the user + for filter in userdata: + if re.match(filter, payload["author"]): + # If we hit the except, don't notify about this + return False + + if protocol == "only-by": + # Check for every 'only' if we hit the user + for filter in userdata: + if re.match(filter, payload["author"]): + break + else: + # If no 'only' hit, don't notify about this + return False + + if protocol == "except": + # Check for every 'except' if we hit the path + for filter in userdata: + if re.match(filter, payload["path"]): + # If we hit the except, don't notify about this + return False + + if protocol == "only": + # Check for every 'only' if we hit the path + for filter in userdata: + if re.match(filter, payload["path"]): + break + else: + # If no 'only' hit, don't notify about this + return False + + return True + + +@router.register("workflow_run") +async def workflow_run(event, github_api): + repository_name = event.data["repository"]["full_name"] + + if event.data["action"] != "completed": + return + + payload = { + "repository_name": repository_name, + "url": event.data["workflow_run"]["html_url"], + "user": event.data["sender"]["login"], + "avatar_url": event.data["sender"]["avatar_url"], + "workflow_name": event.data["workflow_run"]["name"], + "conclusion": event.data["workflow_run"]["conclusion"], + "author": event.data["workflow_run"]["actor"]["login"], + "path": event.data["workflow_run"]["path"], + } + + await protocols.dispatch(github_api, repository_name, "workflow-run", payload, filter_func=filter_func) diff --git a/plugins/GitHub/plugin.py b/plugins/GitHub/plugin.py index a042422..cbbc16a 100644 --- a/plugins/GitHub/plugin.py +++ b/plugins/GitHub/plugin.py @@ -12,6 +12,7 @@ pull_request, push, tag, + workflow, ) from .patches import gidgethub # noqa from .protocols import irc as irc_protocols # noqa diff --git a/plugins/GitHub/protocols/discord.py b/plugins/GitHub/protocols/discord.py index 50c1daf..4f11d40 100644 --- a/plugins/GitHub/protocols/discord.py +++ b/plugins/GitHub/protocols/discord.py @@ -132,3 +132,14 @@ async def ref_tag_created(hook_urls, repository_name, url, user, avatar_url, nam message += f":tada: created a new tag [{name}]({url})" await _send_messages(hook_urls, user, avatar_url, message) + + +@protocols.register("discord", "workflow-run") +async def workflow_run(hook_urls, repository_name, url, user, avatar_url, workflow_name, conclusion, author, path): + if conclusion == "success": + return + + message = f"**{repository_name}** - " + message += f":thunder_cloud_rain: [{workflow_name}]({url}) workflow was not successful" + + await _send_messages(hook_urls, user, avatar_url, message) diff --git a/plugins/GitHub/protocols/irc.py b/plugins/GitHub/protocols/irc.py index 51cf12e..b9db990 100644 --- a/plugins/GitHub/protocols/irc.py +++ b/plugins/GitHub/protocols/irc.py @@ -108,3 +108,14 @@ async def ref_tag_created(channels, repository_name, url, user, avatar_url, name channels, [f"[{repository_name}] {user} created new tag: {name} {shortened_url}"], ) + + +@protocols.register("irc", "workflow-run") +async def workflow_run(channels, repository_name, url, user, avatar_url, workflow_name, conclusion, author, path): + if conclusion == "success": + return + + message = f"{workflow_name} workflow was not successful" + + shortened_url = await shorten(url) + _send_messages(channels, [f"[{repository_name}] {message} {shortened_url}"])