diff options
| author | 2024-04-01 12:59:00 +0100 | |
|---|---|---|
| committer | 2024-04-01 13:56:22 +0100 | |
| commit | 3d04acc24d32d82bf1d817adaae9c65a0cd7ef92 (patch) | |
| tree | 0ab1f6022c92d903aeb6ca4d9a228c7f01505da1 | |
| parent | No need to remove headers in GitHub Filter Endpoint (diff) | |
Log failed webhook attempts to stderr in GitHub Webhook Filter
Diffstat (limited to '')
| -rw-r--r-- | pydis_site/apps/api/tests/test_github_webhook_filter.py | 14 | ||||
| -rw-r--r-- | pydis_site/apps/api/views.py | 14 | 
2 files changed, 27 insertions, 1 deletions
diff --git a/pydis_site/apps/api/tests/test_github_webhook_filter.py b/pydis_site/apps/api/tests/test_github_webhook_filter.py index a2a00d4c..649a9e64 100644 --- a/pydis_site/apps/api/tests/test_github_webhook_filter.py +++ b/pydis_site/apps/api/tests/test_github_webhook_filter.py @@ -62,3 +62,17 @@ class GitHubWebhookFilterAPITests(APITestCase):              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() diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py index 787f8811..a3b0016c 100644 --- a/pydis_site/apps/api/views.py +++ b/pydis_site/apps/api/views.py @@ -304,9 +304,21 @@ class GitHubWebhookFilterView(APIView):              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,          }  |