Skip to content

Add a ping endpoint to webhook service #26

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

Merged
merged 1 commit into from
Aug 26, 2022
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
10 changes: 10 additions & 0 deletions templates/Caddyfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ http://{{ caddy.addresses.webhook }} {
}
}

handle /ping {
reverse_proxy * localhost:1234 {
# Don't leak out internal problems.
@error status 4xx 5xx
handle_response @error {
error 503
}
}
}

handle {
error 404
}
Expand Down
9 changes: 9 additions & 0 deletions webhook/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ async def test_update_repo(tmp_path_factory):
assert dest_commit == src_commit


async def test_ping(aiohttp_client, monkeypatch, tmp_path):
"""Test ping always works."""
monkeypatch.setenv('SITE_DIR', str(tmp_path))
client = await aiohttp_client(create_app())

resp = await client.get('/ping')
assert resp.status == 200


async def test_github_webhook_errors(aiohttp_client, monkeypatch, tmp_path):
"""Test invalid inputs to webhook."""
monkeypatch.setenv('SITE_DIR', str(tmp_path))
Expand Down
6 changes: 6 additions & 0 deletions webhook/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ async def github_webhook(request: web.Request):
return web.Response(status=200)


async def ping(request: web.Request):
"""Respond to a ping, thus verifying the webhook service is alive."""
return web.Response(status=200)


def create_app():
"""Create the aiohttp app and setup routes."""
site_dir = Path(os.environ.get('SITE_DIR', 'sites')).resolve()
Expand All @@ -182,6 +187,7 @@ def create_app():
app['site_dir'] = site_dir
app.add_routes([
web.post('/gh/{repo}', github_webhook),
web.get('/ping', ping),
])
return app

Expand Down