Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't propagate the status received from Discord to GitHub Webhook #1281

Merged
merged 4 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions pydis_site/apps/api/tests/test_github_webhook_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ def test_accepts_interesting_events(self):
context_mock.read.return_value = b'{"status": "ok"}'

response = self.client.post(url, data=payload, headers=headers)
self.assertEqual(response.status_code, context_mock.status)
self.assertEqual(response.headers.get('X-Clacks-Overhead'), 'Joe Armstrong')
response_body = response.json()
self.assertEqual(response.status_code, 200)
self.assertEqual(response_body.get("headers", {}).get("X-Clacks-Overhead"), 'Joe Armstrong')
self.assertEqual(response_body.get("original_status"), 299)

def test_rate_limit_is_logged_to_sentry(self):
url = reverse('api:github-webhook-filter', args=('id', 'token'))
Expand All @@ -60,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()
23 changes: 20 additions & 3 deletions pydis_site/apps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,26 @@ def post(self, request: Request, *, webhook_id: str, webhook_token: str) -> Resp
(response_status, headers, body) = self.send_webhook(
webhook_id, webhook_token, request.data, dict(request.headers),
)
headers.pop('Connection', None)
headers.pop('Content-Length', None)
return Response(data=body, headers=headers, status=response_status)

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_decoded,
"headers": headers,
}

return Response(response_body)

def send_webhook(
self,
Expand Down
Loading