Skip to content

Commit

Permalink
Log failed webhook attempts to stderr in GitHub Webhook Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jb3 committed Apr 1, 2024
1 parent 02787cd commit 3d04acc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pydis_site/apps/api/tests/test_github_webhook_filter.py
Expand Up @@ -62,3 +62,17 @@ def test_rate_limit_is_logged_to_sentry(self):
self.client.post(url, data=payload, headers=headers)

logger.warning.assert_called_once()

def test_other_error_is_logged(self):
url = reverse('api:github-webhook-filter', args=('id', 'token'))
payload = {}
headers = {'X-GitHub-Event': 'pull_request_review'}
with (
mock.patch('urllib.request.urlopen') as urlopen,
mock.patch.object(GitHubWebhookFilterView, "logger") as logger,
):
urlopen.side_effect = HTTPError(None, 451, 'Unavailable For Legal Reasons', {}, None)
logger.warning = mock.PropertyMock()
self.client.post(url, data=payload, headers=headers)

logger.warning.assert_called_once()
14 changes: 13 additions & 1 deletion pydis_site/apps/api/views.py
Expand Up @@ -304,9 +304,21 @@ def post(self, request: Request, *, webhook_id: str, webhook_token: str) -> Resp
webhook_id, webhook_token, request.data, dict(request.headers),
)

body_decoded = body.decode("utf-8")

if (
not (status.HTTP_200_OK <= response_status < status.HTTP_300_MULTIPLE_CHOICES)
and response_status != status.HTTP_429_TOO_MANY_REQUESTS
):
self.logger.warning(
"Failed to send GitHub webhook to Discord. Response code %d, body: %s",
response_status,
body_decoded,
)

response_body = {
"original_status": response_status,
"data": body.decode("utf-8"),
"data": body_decoded,
"headers": headers,
}

Expand Down

0 comments on commit 3d04acc

Please sign in to comment.