summaryrefslogtreecommitdiff
path: root/kodereviewer/data.py
blob: 82aad94e44a9588239cd01617a4adef63cc53929 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from copy import deepcopy
from datetime import datetime
from enum import Enum
import json
from typing import Any, Optional, Self

from PySide6.QtCore import QByteArray, QObject, QSettings, Signal, Slot, Property, qDebug
from PySide6.QtNetwork import QHttpHeaders, QNetworkAccessManager, QNetworkReply, QNetworkRequestFactory
from PySide6.QtQml import QmlElement


class User(QObject):
    username: str
    avatar_url: str

    def __init__(self, data: dict[str, Any]):
        super().__init__()
        self.username = data['login']
        self.avatar_url = data['avatar_url']


class Label(QObject):
    name: str
    color: str
    description: str

    def __init__(self, data: dict[str, Any]):
        super().__init__()
        self.name = data['name']
        self.color = data['color']
        self.description = data['description']


class Comment(QObject):
    body: str
    reactions: dict[str, int]

    created_at: datetime
    updated_at: datetime

    user: User

    def __init__(self, data: dict[str, Any]):
        super().__init__()
        self.body = data['body']
        self.reactions = data['reactions']
        self.created_at = datetime.fromisoformat(data['created_at'])
        self.updated_at = datetime.fromisoformat(data['updated_at'])
        self.user = User(data['user'])


class State(Enum):
    OPEN = 'open'
    CLOSED = 'closed'
    DRAFT = 'draft'


class PullRequest(QObject):

    _number: int
    _title: str
    _state: State
    _url: str
    _body: str | None
    _created_at: datetime
    _updated_at: datetime
    _draft: bool

    _user: User
    _assignee: User | None
    _labels: list[Label]

    _comments: list[Comment]

    _initial_data: dict[str, Any]

    commentsLoaded = Signal()

    def __init__(self, data: dict[str, Any], *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._number = data['number']
        self._state = data['state']
        self._title = data['title']
        self._url = data['html_url']
        self._body = data['body']
        self._created_at = datetime.fromisoformat(data['created_at'])
        self._updated_at = datetime.fromisoformat(data['updated_at'])
        self._draft = data['draft']

        self._user = User(data['user'])
        self._assignee = None
        if data['assignee'] is not None:
            self._assignee = User(data['assignee'])

        self._labels = [Label(label) for label in data['labels']]

        # At first this is empty until it's updated with a request
        self._comments = []

        self._initial_data = data

    def __eq__(self, other: object) -> bool:
        if isinstance(other, PullRequest):
            return self._number == other._number
        return False

    def __str__(self) -> str:
        return self._title

    @Property(int, constant=True)
    def number(self) -> int:
        return self._number

    @Property(str, constant=True)
    def title(self) -> str:
        return self._title

    @Property(str, constant=True)
    def state(self) -> str:
        return self._state

    @Property(str, constant=True)
    def url(self) -> str:
        return self._url

    @Property(str, constant=True)
    def body(self) -> str:
        if self._body is not None:
            return self._body
        return ''

    @Property(datetime, constant=True)
    def created_at(self) -> datetime:
        return self._created_at

    @Property(bool, constant=True)
    def draft(self) -> bool:
        return self._draft

    @Property(str, constant=True)
    def username(self) -> str:
        return self._user.username

    @Property(list, constant=True)
    def labels(self) -> list[Label]:
        return self._labels

    def load_comments(self, response: QByteArray) -> None:
        print("loading comments")
        data = json.loads(response.toStdString())
        self._comments = [Comment(comment) for comment in data]
        self.commentsLoaded.emit()

    def copy(self):
        """Create a copy of a PullRequest with it's initial parameters."""
        return PullRequest(deepcopy(self._initial_data))