summaryrefslogtreecommitdiff
path: root/kodereviewer/project.py
blob: e8803525c565fcc3232ee0a59143409b2071a568 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
from typing import Optional

from PySide6.QtCore import QByteArray, QObject, QUrl, Signal, Slot, Property
from PySide6.QtQml import QmlElement

from kodereviewer.data import PullRequest

QML_IMPORT_NAME = "org.deprecated.kodereviewer"
QML_IMPORT_MAJOR_VERSION = 1

@QmlElement
class Project(QObject):
    """Represents a github project"""

    _name: str
    _owner: str
    _url: QUrl
    _pull_requests: list[PullRequest]

    pullRequestsChanged = Signal()

    def __init__(self, name: str, owner: str, url: QUrl):
        super().__init__()
        self._name = name
        self._owner = owner
        self._url = url
        self._pull_requests = []

    @Property(str)
    def name(self) -> str:
        return self._name

    @Property(str)
    def owner(self) -> str:
        return self._owner

    @Property(QUrl)
    def url(self) -> QUrl:
        return self._url

    @Property(list)
    def pullRequests(self) -> list[PullRequest]:
        return self._pull_requests

    @Slot(int, result=PullRequest)
    def pullRequest(self, number: int) -> Optional[PullRequest]:
        return self.find_pull_request(number)

    def load_pull_requests(self, response: QByteArray) -> None:
        data = json.loads(response.toStdString())
        self._pull_requests = [
            PullRequest(pr, parent=self) for pr in data
        ]
        self.pullRequestsChanged.emit()

    def find_pull_request(self, number: int) -> Optional[PullRequest]:
        for pr in self._pull_requests:
            if pr.number == number:
                return pr

        return None