diff options
| author | Daniel Smith <daniel.smith@qt.io> | 2024-07-24 11:03:06 +0200 |
|---|---|---|
| committer | Daniel Smith <daniel.smith@qt.io> | 2024-07-24 11:04:35 +0200 |
| commit | d6752d1912619a52b44dbbe9ef3714386c98a01c (patch) | |
| tree | 0b2400252230dfd6707db2738ebbf83c0b699696 /trigger_webhook.py | |
Initial Commit of Qt Binary Size Bot
Task-number: QTQAINFRA-6459
Change-Id: I50887b05bd7f11119092ba284082aacc3062ee19
Diffstat (limited to 'trigger_webhook.py')
| -rw-r--r-- | trigger_webhook.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/trigger_webhook.py b/trigger_webhook.py new file mode 100644 index 0000000..f3a2c03 --- /dev/null +++ b/trigger_webhook.py @@ -0,0 +1,49 @@ +# Copyright (C) 2024 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +""" This script listens for incoming webhook requests of change-merged type + from Gerrit. +""" +import json +import asyncio +from aiohttp import web + +HTTP_CALLBACK = None + + +async def _handle(request): + """ Handle the incoming webhook request. """ + body = await request.text() + data = json.loads(body) + + # make sure it's a change-merged event + if data['type'] != 'change-merged': + return web.Response(status=200) + + try: + print(f'{data["change"]["number"]},{data["change"]["project"]}({data["change"]["branch"]}):' + f'Received webhook for revision: {data["patchSet"]["revision"]}') + # pylint: disable=W0602 + global HTTP_CALLBACK + HTTP_CALLBACK(data['change']['project'], data['change']['branch'], data["patchSet"]["revision"]) + # pylint: disable=W0718 + except Exception as e: + print("Error: %s", str(e)) + return web.Response(status=200) + + return web.Response(status=200) + + +async def run_web_server(callback, port): + """ Run the web server. """ + # pylint: disable=W0603 + global HTTP_CALLBACK + HTTP_CALLBACK = callback + app = web.Application() + app.add_routes([web.post('/', _handle)]) + runner = web.AppRunner(app) + await runner.setup() + site = web.TCPSite(runner, 'localhost', port) + await site.start() + print(f"Web server started on port {port}") + while True: + await asyncio.sleep(3600) |
