blob: aaad15db2365d663cd728adbc6bd4252abfabcdb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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()
|