aboutsummaryrefslogtreecommitdiffstats
path: root/gerrit_api.py
blob: 356e3e67967a8f52b5d2c56777faa8747ffbb75a (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
# 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
""" Gerrit API wrapper module for Qt purposes """
from pygerrit2.rest import GerritRestAPI


class GerritApiException(Exception):
    """ Exception class """


# pylint: disable=R0903
class GerritApi():
    """ Gerrit API wrapper class for Qt purposes """

    def __init__(self, gerrit_server) -> None:
        self._server_url = 'https://' + gerrit_server

    def get_coin_task_id(self, sha) -> str:
        """ Fetches COIN task id from gerrit review comments """
        client = GerritRestAPI(url=self._server_url)

        messages = client.get('changes/' + sha + '/messages')

        for message in messages:
            if "Continuous Integration: Passed" in message["message"]:
                return message["message"].split("tasks/", 1)[1].split("\n")[0]

        raise GerritApiException(f'Gerrit comment from COIN not found from {messages}')