8000 skip unsupported github events · EuroPython/internal-bot@f4be329 · GitHub
[go: up one dir, main page]

Skip to content

Commit f4be329

Browse files
committed
skip unsupported github events
1 parent f89d02a commit f4be329

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

intbot/core/integrations/github.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def parse_github_webhook(headers: dict, content: dict) -> tuple[str, str]:
4242
return "", ""
4343

4444
else:
45-
raise ValueError(f"Event {event} not supported")
45+
raise ValueError(f"Event `{event}` not supported")
4646

4747

4848
@dataclasses.dataclass

intbot/core/tasks.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
from core.models import DiscordMessage, Webhook
1+
import logging
2+
23
from core.integrations.github import parse_github_webhook
4+
from core.models import DiscordMessage, Webhook
35
from django.conf import settings
46
from django.utils import timezone
57
from django_tasks import task
68

9+
logger = logging.getLogger()
10+
711

812
@task
913
def process_webhook(wh_uuid: str):
@@ -38,7 +42,14 @@ def process_github_webhook(wh: Webhook):
3842
if wh.source != "github":
3943
raise ValueError("Incorrect wh.source = {wh.source}")
4044

41-
message, event_action = parse_github_webhook(headers=wh.meta, content=wh.content)
45+
try:
46+
message, event_action = parse_github_webhook(
47+
headers=wh.meta, content=wh.content
48+
)
49+
except ValueError as e:
50+
# Downgrading to info because it's most likely event not supported
51+
logger.info(f"Not processing Github Webhook {wh.uuid}: {e}")
52+
return
4253

4354
# NOTE WHERE SHOULD WE GET THE CHANNEL ID FROM?
4455
DiscordMessage.objects.create(

intbot/tests/test_tasks.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import logging
2+
13
import pytest
24
from core.models import DiscordMessage, Webhook
3-
from core.tasks import process_internal_webhook, process_webhook
5+
from core.tasks import process_github_webhook, process_internal_webhook, process_webhook
46
from django_tasks.task import ResultStatus
57

68

@@ -31,7 +33,7 @@ def test_process_internal_webhook_fails_if_incorrect_source():
3133

3234

3335
@pytest.mark.django_db
34-
def test_process_webhook_fails_if_unsupported_source(caplog):
36+
def test_process_webhook_fails_if_unsupported_source():
3537
wh = Webhook.objects.create(source="asdf", event="test1", content={})
3638

3739
# If the task is enqueued the errors are not emited.
@@ -41,3 +43,23 @@ def test_process_webhook_fails_if_unsupported_source(caplog):
4143
assert result.status == ResultStatus.FAILED
4244
assert result._exception_class == ValueError
4345
assert result.traceback.endswith("ValueError: Unsupported source asdf\n")
46+
47+
48+
@pytest.mark.django_db
49+
def test_process_github_webhook_logs_unsupported_event(caplog):
50+
wh = Webhook.objects.create(
51+
source="github",
52+
event="",
53+
meta={"X-Github-Event": "testrandom"},
54+
content={},
55+
)
56+
57+
with caplog.at_level(logging.INFO):
58+
process_github_webhook(wh)
59+
60+
assert len(caplog.records) == 1
61+
assert caplog.records[0].levelname == "INFO"
62+
assert (
63+
caplog.records[0].message
64+
== f"Not processing Github Webhook {wh.uuid}: Event `testrandom` not supported"
65+
)

0 commit comments

Comments
 (0)
0