Skip to content

Commit a804f5a

Browse files
committed
more review feedback
1 parent 8727a54 commit a804f5a

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

intbot/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66

77
@pytest.fixture(scope="session")
8-
def gh_data():
8+
def github_data():
9+
"""Pytest fixture with examples of webhooks from github"""
910
base_path = settings.BASE_DIR / "tests" / "test_integrations" / "github"
1011

1112
return {

intbot/core/bot/channel_router.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DiscordChannel:
1919
channel_name: str
2020

2121

22-
dont_send_it = DiscordChannel(channel_id="0", channel_name="/dev/null")
22+
dont_send_it = DiscordChannel(channel_id="0", channel_name="DONT_SEND_IT")
2323

2424

2525
class Channels:
@@ -67,28 +67,27 @@ def github_router(wh: Webhook) -> DiscordChannel:
6767

6868
# We have three github projects, that we want to route to three different
6969
# channels - one for ep2025, one for EM, and one for the board.
70-
if project.id == GithubProjects.board_project:
71-
return Channels.board_channel
70+
PROJECTS = {
71+
GithubProjects.board_project: Channels.board_channel,
72+
GithubProjects.ep2025_project: Channels.ep2025_channel,
73+
GithubProjects.em_project: Channels.em_channel,
74+
}
7275

73-
elif project.id == GithubProjects.ep2025_project:
74-
return Channels.ep2025_channel
75-
76-
elif project.id == GithubProjects.em_project:
77-
return Channels.em_channel
76+
if channel := PROJECTS.get(project.id):
77+
return channel
7878

7979
# Then we have our open source repositories, like website, this bot, and
8080
# some others, that we also might want to route to different channels
81-
if repository == GithubRepositories.website_repo:
82-
return Channels.website_channel
83-
84-
elif repository == GithubRepositories.bot_repo:
85-
return Channels.bot_channel
81+
REPOSITORIES = {
82+
GithubRepositories.website_repo: Channels.website_channel,
83+
GithubRepositories.bot_repo: Channels.bot_channel,
84+
}
8685

87-
elif repository == ...:
88-
...
86+
if channel := REPOSITORIES.get(repository.id):
87+
return channel
8988

9089
# Finally, we can use this to drop notifications that we don't want to
91-
# support, by routing them to /dev/null
90+
# support, by not sending them.
9291
return dont_send_it
9392

9493

intbot/core/integrations/github.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class GithubProject(BaseModel):
6363
url: str
6464

6565

66+
class GithubRepository(BaseModel):
67+
id: str
68+
name: str
69+
70+
6671
class GithubSender(BaseModel):
6772
login: str
6873
html_url: str
@@ -84,13 +89,14 @@ class GithubDraftIssue(BaseModel):
8489
def as_discord_message(self):
8590
return self.title
8691

92+
JsonType = dict[str, Any]
8793

8894
class GithubWebhook:
8995
"""
9096
Base class for all the other specific types of webhooks.
9197
"""
9298

93-
def __init__(self, action: str, headers: dict, content: dict, extra: dict):
99+
def __init__(self, action: str, headers: JsonType, content: JsonType, extra: JsonType):
94100
self.action = action
95101
self.headers = headers
96102
self.content = content
@@ -143,9 +149,9 @@ def github_object(self) -> GithubDraftIssue | GithubIssue:
143149
def get_project(self) -> GithubProject:
144150
return GithubProject.model_validate(self.extra["project"])
145151

146-
def get_repository(self):
152+
def get_repository(self) -> GithubRepository:
147153
# Not relevant at the moment
148-
return ...
154+
return GithubRepository(name="Placeholder", id="placeholder-repo")
149155

150156
def changes(self) -> dict:
151157

@@ -158,6 +164,9 @@ def changes(self) -> dict:
158164
field_name = fv["field_name"]
159165
field_type = fv["field_type"]
160166

167+
changed_from: str
168+
changed_to: str
169+
161170
if field_type == "date":
162171
changed_from = (
163172
fv["from"].split("T")[0] if fv["from"] is not None else "None"

intbot/core/tasks.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import logging
22

33
from core.integrations.github import parse_github_webhook, prep_github_webhook
4-
from core.bot.channel_router import discord_channel_router
4+
from core.bot.channel_router import discord_channel_router, dont_send_it
55
from core.models import DiscordMessage, Webhook
66
from django.utils import timezone
77
from django_tasks import task
88

99
logger = logging.getLogger()
1010

1111

12+
1213
@task
1314
def process_webhook(wh_uuid: str):
1415
wh = Webhook.objects.get(uuid=wh_uuid)
@@ -56,11 +57,14 @@ def process_github_webhook(wh: Webhook):
5657
parsed = parse_github_webhook(wh)
5758
channel = discord_channel_router(wh)
5859

60+
if channel == dont_send_it:
61+
wh.processed_at = timezone.now()
62+
wh.save()
63+
return
64+
5965
DiscordMessage.objects.create(
6066
channel_id=channel.channel_id,
6167
channel_name=channel.channel_name,
62-
# channel_id=settings.DISCORD_TEST_CHANNEL_ID,
63-
# channel_name=settings.DISCORD_TEST_CHANNEL_NAME,
6468
content=f"GitHub: {parsed.message}",
6569
# Mark as unsend - to be sent with the next batch
6670
sent_at=None,

intbot/tests/test_integrations/test_github.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,11 @@ def test_changes_for_unsupported_format(self):
250250
}
251251

252252

253-
def test_get_project_parses_project_correctly(self, gh_data):
253+
def test_get_project_parses_project_correctly(self, github_data):
254254
wh = Webhook(
255255
meta={"X-Github-Event": "projects_v2_item"},
256-
content=gh_data["project_v2_item.edited"],
257-
extra=gh_data["query_result"],
256+
content=github_data["project_v2_item.edited"],
257+
extra=github_data["query_result"],
258258
)
259259
gwh = parse_github_webhook(wh)
260260

@@ -263,11 +263,11 @@ def test_get_project_parses_project_correctly(self, gh_data):
263263
assert ghp.title == "Board Project"
264264
assert ghp.url == "https://github.com/orgs/EuroPython/projects/1337"
265265

266-
def test_get_sender_parses_sender_correctly(self, gh_data):
266+
def test_get_sender_parses_sender_correctly(self, github_data):
267267
wh = Webhook(
268268
meta={"X-Github-Event": "projects_v2_item"},
269-
content=gh_data["project_v2_item.edited"],
270-
extra=gh_data["query_result"],
269+
content=github_data["project_v2_item.edited"],
270+
extra=github_data["query_result"],
271271
)
272272
gwh = parse_github_webhook(wh)
273273

@@ -277,11 +277,11 @@ def test_get_sender_parses_sender_correctly(self, gh_data):
277277
assert sender.login == "github-project-automation[bot]"
278278
assert sender.html_url == "https://github.com/apps/github-project-automation"
279279

280-
def test_sender_formats_sender_correctly(self, gh_data):
280+
def test_sender_formats_sender_correctly(self, github_data):
281281
wh = Webhook(
282282
meta={"X-Github-Event": "projects_v2_item"},
283-
content=gh_data["project_v2_item.edited"],
284-
extra=gh_data["query_result"],
283+
content=github_data["project_v2_item.edited"],
284+
extra=github_data["query_result"],
285285
)
286286
gwh = parse_github_webhook(wh)
287287

0 commit comments

Comments
 (0)