Skip to content

Commit

Permalink
Add: announce workflow failures if configured (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueBrain authored Mar 10, 2024
1 parent 8299a73 commit 2e30b51
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
63 changes: 63 additions & 0 deletions plugins/GitHub/events/workflow.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions plugins/GitHub/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
pull_request,
push,
tag,
workflow,
)
from .patches import gidgethub # noqa
from .protocols import irc as irc_protocols # noqa
Expand Down
11 changes: 11 additions & 0 deletions plugins/GitHub/protocols/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 11 additions & 0 deletions plugins/GitHub/protocols/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"])

0 comments on commit 2e30b51

Please sign in to comment.