summaryrefslogtreecommitdiff
path: root/kodereviewer/data.py
blob: 1598e9ddcfd6ae563f7c822bf9e88f933732c877 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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']

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

    @Property(str, constant=True)
    def avatarUrl(self) -> str:
        return self._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'])

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

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

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

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


class ReviewComment(Comment):
    _id: int
    _in_reply_to: int | None
    _diff: str
    _line: int
    def __init__(self, data: dict[str, Any]):
        super().__init__(data)
        self._id = data['id']
        self._in_reply_to = data.get('in_reply_to_id')
        self._diff = data['diff_hunk']
        self._line = data['line']

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

    @Property(int, constant=True)
    def in_reply_to(self) -> int:
        if self._in_reply_to is None:
            return 0
        return self._in_reply_to

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

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


class ChangedFileStatus(Enum):
    ADDED = 'added'
    MODIFIED = 'modified'
    DELETED = 'deleted'


class ChangedFile(QObject):
    """  {
      "sha": "bbcd538c8e72b8c175046e27cc8f907076331401",
      "filename": "file1.txt",
      "status": "added",
      "additions": 103,
      "deletions": 21,
      "changes": 124,
      "blob_url": "https://github.com/octocat/Hello-World/blob/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt",
      "raw_url": "https://github.com/octocat/Hello-World/raw/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt",
      "contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/file1.txt?ref=6dcb09b5b57875f334f61aebed695e2e4193db5e",
      "patch": "@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test"
    }"""

    sha: str
    filename: str
    status: ChangedFileStatus
    additions: int
    deletions: int
    changes: int
    patch: str

    def __init__(self, data: dict[str, Any]):
        self.sha = data['sha']
        self.filename = data['filename']
        self.status = data['status']
        self.additions = int(data['additions'])
        self.deletions = int(data['deletions'])
        self.changes = int(data['changes'])
        self.patch = data.get('patch', '')


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
    _reviewers: list[User]
    _labels: list[Label]

    _last_commit: str

    _comments: list[Comment]
    _reviews: list[ReviewComment]
    _files: list[ChangedFile]

    _initial_data: dict[str, Any]

    commentsLoaded = Signal()
    filesLoaded = Signal()
    reviewsLoaded = 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._reviewers = [
            User(user) for user in data['assignees']
        ]

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

        self._last_commit = data['head']['sha']

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

        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(str, constant=True)
    def reviewers(self) -> list[User]:
        return self._reviewers

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

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

    @Property(list, constant=True)
    def files(self) -> list[ChangedFile]:
        return self._files

    @Property(list, constant=True)
    def reviews(self) -> list[ReviewComment]:
        return self._reviews

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

    def load_files(self, response: QByteArray) -> None:
        data = json.loads(response.toStdString())
        self._files = [ChangedFile(file) for file in data]
        self.filesLoaded.emit()

    def load_reviews(self, response: QByteArray) -> None:
        data = json.loads(response.toStdString())
        self._reviews = [ReviewComment(review) for review in data]
        self.reviewsLoaded.emit()

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