aboutsummaryrefslogtreecommitdiffstats
path: root/trigger_ssh.py
diff options
context:
space:
mode:
authorDaniel Smith <daniel.smith@qt.io>2024-07-24 11:03:06 +0200
committerDaniel Smith <daniel.smith@qt.io>2024-07-24 11:04:35 +0200
commitd6752d1912619a52b44dbbe9ef3714386c98a01c (patch)
tree0b2400252230dfd6707db2738ebbf83c0b699696 /trigger_ssh.py
Initial Commit of Qt Binary Size Bot
Diffstat (limited to 'trigger_ssh.py')
-rw-r--r--trigger_ssh.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/trigger_ssh.py b/trigger_ssh.py
new file mode 100644
index 0000000..aaad15d
--- /dev/null
+++ b/trigger_ssh.py
@@ -0,0 +1,39 @@
+# 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 gerrit SSH session events of change-merged type """
+
+import json
+import sys
+from typing import Optional
+import asyncssh
+
+SSH_CALLBACK = None
+
+
+class _SSHClientSession(asyncssh.SSHClientSession):
+ """ Class for SSH client session instance """
+ def data_received(self, data: str, datatype: asyncssh.DataType) -> None:
+ gerrit_json = json.loads(data)
+ print(gerrit_json)
+ # pylint: disable=W0602
+ global SSH_CALLBACK
+ if SSH_CALLBACK is not None and gerrit_json['type'] == "change-merged":
+ SSH_CALLBACK(gerrit_json['change']['project'], gerrit_json['change']['branch'], gerrit_json['newRev'])
+
+ def connection_lost(self, exc: Optional[Exception]) -> None:
+ if exc:
+ print('SSH session error: ' + str(exc), file=sys.stderr)
+
+
+async def run_client(callback, url: str, port: int) -> None:
+ """ Connects SSH session and starts listening events """
+ # pylint: disable=W0603
+ global SSH_CALLBACK
+ SSH_CALLBACK = callback
+
+ async with asyncssh.connect(url, port) as conn:
+ async with conn:
+ chan, _session = await conn.create_session(
+ _SSHClientSession, 'gerrit stream-events -s change-merged')
+ print(f"SSH session created to {url}:{port} -> {_session}")
+ await chan.wait_closed()