summaryrefslogtreecommitdiff
path: root/kodereviewer/data.py
blob: 61f1a01cc505c621ec13a51ab3ca8d54c507a75c (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
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 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]
    _files: list[ChangedFile]

    _initial_data: dict[str, Any]

    commentsLoaded = Signal()
    filesLoaded = 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._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

    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 copy(self):
        """Create a copy of a PullRequest with it's initial parameters."""
        return PullRequest(deepcopy(self._initial_data))