Skip to content

Commit 8727a54

Browse files
committed
review feedback
1 parent b29341f commit 8727a54

File tree

3 files changed

+43
-38
lines changed

3 files changed

+43
-38
lines changed

intbot/core/bot/channel_router.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ def github_router(wh: Webhook) -> DiscordChannel:
7676
elif project.id == GithubProjects.em_project:
7777
return Channels.em_channel
7878

79-
else:
80-
...
81-
8279
# Then we have our open source repositories, like website, this bot, and
8380
# some others, that we also might want to route to different channels
8481
if repository == GithubRepositories.website_repo:

intbot/core/integrations/github.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,15 @@ def get_repository(self): # pragma: no cover
120120
class GithubProjectV2Item(GithubWebhook):
121121
# NOTE: This might be something for pydantic schemas in the future
122122

123+
@property
123124
def sender(self):
124125
sender = self.get_sender()
125126

126127
return f"[@{sender.login}]({sender.html_url})"
127128

129+
def get_sender(self) -> GithubSender:
130+
return GithubSender.model_validate(self.content["sender"])
131+
128132
def github_object(self) -> GithubDraftIssue | GithubIssue:
129133
content = self.extra["content"]
130134
typename = content.pop("__typename")
@@ -143,41 +147,40 @@ def get_repository(self):
143147
# Not relevant at the moment
144148
return ...
145149

146-
def get_sender(self) -> GithubSender:
147-
return GithubSender.model_validate(self.content["sender"])
148-
149150
def changes(self) -> dict:
150-
if "changes" in self.content:
151-
fv = self.content["changes"]["field_value"]
152-
field_name = fv["field_name"]
153-
field_type = fv["field_type"]
154-
155-
if field_type == "date":
156-
changed_from = (
157-
fv["from"].split("T")[0] if fv["from"] is not None else "None"
158-
)
159-
changed_to = fv["to"].split("T")[0] if fv["to"] is not None else "None"
160-
161-
elif field_type == "single_select":
162-
changed_from = fv["from"]["name"] if fv["from"] is not None else "None"
163-
changed_to = fv["to"]["name"] if fv["to"] is not None else "None"
164-
165-
else:
166-
changed_from = "None"
167-
changed_to = "None"
168-
169-
return {
170-
"field": field_name,
171-
"from": changed_from,
172-
"to": changed_to,
173-
}
174151

175-
return {}
152+
# Early return! \o/
153+
if "changes" not in self.content:
154+
# Fallback because some webhooks just don't have changes.
155+
return {}
156+
157+
fv = self.content["changes"]["field_value"]
158+
field_name = fv["field_name"]
159+
field_type = fv["field_type"]
160+
161+
if field_type == "date":
162+
changed_from = (
163+
fv["from"].split("T")[0] if fv["from"] is not None else "None"
164+
)
165+
changed_to = fv["to"].split("T")[0] if fv["to"] is not None else "None"
166+
167+
elif field_type == "single_select":
168+
changed_from = fv["from"]["name"] if fv["from"] is not None else "None"
169+
changed_to = fv["to"]["name"] if fv["to"] is not None else "None"
170+
171+
else:
172+
changed_from = "None"
173+
changed_to = "None"
174+
175+
return {
176+
"field": field_name,
177+
"from": changed_from,
178+
"to": changed_to,
179+
}
176180

177181
def as_discord_message(self) -> str:
178182
message = "{sender} {action} {details}".format
179183

180-
sender = self.sender()
181184
changes = self.changes()
182185

183186
if changes:
@@ -190,7 +193,7 @@ def as_discord_message(self) -> str:
190193

191194
return message(
192195
**{
193-
"sender": sender,
196+
"sender": self.sender,
194197
"action": self.action,
195198
"details": details,
196199
}
@@ -214,6 +217,11 @@ def prep_github_webhook(wh: Webhook):
214217
raise ValueError(f"Event `{event}` not supported")
215218

216219

220+
class GithubAPIError(Exception):
221+
"""Custom exception for GithubAPI Errors"""
222+
pass
223+
224+
217225
# Should we have a separate GithubClient that encapsulates this?
218226
# Or at least a function that runs the request.
219227
def fetch_github_project_item(item_id: str) -> dict[str, Any]:
@@ -227,7 +235,7 @@ def fetch_github_project_item(item_id: str) -> dict[str, Any]:
227235
if response.status_code == 200:
228236
return response.json()["data"]["node"]
229237
else:
230-
raise Exception(f"GitHub API error: {response.status_code} - {response.text}")
238+
raise GithubAPIError(f"GitHub API error: {response.status_code} - {response.text}")
231239

232240

233241
def parse_github_webhook(wh: Webhook):

intbot/tests/test_integrations/test_github.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from core.integrations.github import (
44
GITHUB_API_URL,
55
GithubProjectV2Item,
6+
GithubAPIError,
67
GithubSender,
78
parse_github_webhook,
89
prep_github_webhook,
@@ -284,11 +285,10 @@ def test_sender_formats_sender_correctly(self, gh_data):
284285
)
285286
gwh = parse_github_webhook(wh)
286287

287-
sender = gwh.sender()
288288

289-
assert isinstance(sender, str)
289+
assert isinstance(gwh.sender, str)
290290
assert (
291-
sender == "[@github-project-automation[bot]]("
291+
gwh.sender == "[@github-project-automation[bot]]("
292292
"https://github.com/apps/github-project-automation"
293293
")"
294294
)
@@ -354,7 +354,7 @@ def test_prep_github_webhook_reraises_exception_in_case_of_API_error():
354354

355355
respx.post(GITHUB_API_URL).mock(return_value=Response(500, json={"lol": "failed"}))
356356

357-
with pytest.raises(Exception, match='GitHub API error: 500 - {"lol":"failed"}'):
357+
with pytest.raises(GithubAPIError, match='GitHub API error: 500 - {"lol":"failed"}'):
358358
wh = prep_github_webhook(wh)
359359

360360

0 commit comments

Comments
 (0)