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))