diff --git a/swh/web/save_origin_webhooks/generic_receiver.py b/swh/web/save_origin_webhooks/generic_receiver.py --- a/swh/web/save_origin_webhooks/generic_receiver.py +++ b/swh/web/save_origin_webhooks/generic_receiver.py @@ -25,6 +25,9 @@ def is_forge_request(self, request: Request) -> bool: ... + def is_ping_event(self, request: Request) -> bool: + return False + @abc.abstractmethod def is_push_event(self, request: Request) -> bool: ... @@ -81,6 +84,9 @@ "has not been processed." ) + if self.is_ping_event(request): + return {"message": "pong"} + if not self.is_push_event(request): raise BadInputExc( f"Event sent by {self.FORGE_TYPE} webhook is not a push one, request " diff --git a/swh/web/save_origin_webhooks/github.py b/swh/web/save_origin_webhooks/github.py --- a/swh/web/save_origin_webhooks/github.py +++ b/swh/web/save_origin_webhooks/github.py @@ -26,6 +26,9 @@ and f"X-{self.FORGE_TYPE}-Event" in request.headers ) + def is_ping_event(self, request: Request) -> bool: + return request.headers[f"X-{self.FORGE_TYPE}-Event"] == "ping" + def is_push_event(self, request: Request) -> bool: return request.headers[f"X-{self.FORGE_TYPE}-Event"] == "push" diff --git a/swh/web/save_origin_webhooks/tests/test_github.py b/swh/web/save_origin_webhooks/tests/test_github.py --- a/swh/web/save_origin_webhooks/tests/test_github.py +++ b/swh/web/save_origin_webhooks/tests/test_github.py @@ -8,7 +8,11 @@ import pytest +from swh.web.tests.helpers import check_api_post_responses +from swh.web.utils import reverse + from .utils import ( + django_http_headers, origin_save_webhook_receiver_invalid_content_type_test, origin_save_webhook_receiver_invalid_event_test, origin_save_webhook_receiver_invalid_request_test, @@ -86,3 +90,21 @@ payload=payload, api_client=api_client, ) + + +def test_origin_save_github_webhook_receiver_ping_event(api_client): + url = reverse("api-1-origin-save-webhook-github") + + resp = check_api_post_responses( + api_client, + url, + status_code=200, + **django_http_headers( + { + "User-Agent": "GitHub-Hookshot/ede37db", + "X-GitHub-Event": "ping", + } + ), + ) + + assert resp.data == {"message": "pong"} diff --git a/swh/web/save_origin_webhooks/tests/utils.py b/swh/web/save_origin_webhooks/tests/utils.py --- a/swh/web/save_origin_webhooks/tests/utils.py +++ b/swh/web/save_origin_webhooks/tests/utils.py @@ -9,7 +9,7 @@ from swh.web.utils import reverse -def _django_http_headers(http_headers: Dict[str, Any]): +def django_http_headers(http_headers: Dict[str, Any]): return {f"HTTP_{k.upper().replace('-', '_')}": v for k, v in http_headers.items()} @@ -29,7 +29,7 @@ url, status_code=200, data=payload, - **_django_http_headers(http_headers), + **django_http_headers(http_headers), ) assert resp.data["origin_url"] == expected_origin_url @@ -54,7 +54,7 @@ url, status_code=400, data=payload, - **_django_http_headers(http_headers), + **django_http_headers(http_headers), ) assert resp.data == { @@ -79,7 +79,7 @@ url, status_code=400, data=payload, - **_django_http_headers(http_headers), + **django_http_headers(http_headers), ) assert resp.data == { @@ -107,7 +107,7 @@ url, status_code=400, data=payload, - **_django_http_headers(http_headers), + **django_http_headers(http_headers), ) assert resp.data == { @@ -132,7 +132,7 @@ url, status_code=400, data=payload, - **_django_http_headers(http_headers), + **django_http_headers(http_headers), ) assert resp.data == {