IssueComment.py
1 ############################ Copyrights and license ############################ 2 # # 3 # Copyright 2012 Vincent Jacques <vincent@vincent-jacques.net> # 4 # Copyright 2012 Zearin <zearin@gonk.net> # 5 # Copyright 2013 AKFish <akfish@gmail.com> # 6 # Copyright 2013 Michael Stead <michael.stead@gmail.com> # 7 # Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net> # 8 # Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net> # 9 # Copyright 2016 Jannis Gebauer <ja.geb@me.com> # 10 # Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com> # 11 # Copyright 2017 Nicolas AgustÃn Torres <nicolastrres@gmail.com> # 12 # Copyright 2018 Wan Liuyang <tsfdye@gmail.com> # 13 # Copyright 2018 per1234 <accounts@perglass.com> # 14 # Copyright 2018 sfdye <tsfdye@gmail.com> # 15 # Copyright 2020 Huan-Cheng Chang <changhc84@gmail.com> # 16 # # 17 # This file is part of PyGithub. # 18 # http://pygithub.readthedocs.io/ # 19 # # 20 # PyGithub is free software: you can redistribute it and/or modify it under # 21 # the terms of the GNU Lesser General Public License as published by the Free # 22 # Software Foundation, either version 3 of the License, or (at your option) # 23 # any later version. # 24 # # 25 # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # 26 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # 27 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # 28 # details. # 29 # # 30 # You should have received a copy of the GNU Lesser General Public License # 31 # along with PyGithub. If not, see <http://www.gnu.org/licenses/>. # 32 # # 33 ################################################################################ 34 from __future__ import annotations 35 36 from datetime import datetime 37 from typing import TYPE_CHECKING, Any 38 39 import github.GithubObject 40 import github.NamedUser 41 from github import Consts 42 from github.GithubObject import Attribute, CompletableGithubObject, NotSet 43 from github.PaginatedList import PaginatedList 44 45 if TYPE_CHECKING: 46 from github.Reaction import Reaction 47 48 49 class IssueComment(CompletableGithubObject): 50 """ 51 This class represents IssueComments. The reference can be found here https://docs.github.com/en/rest/reference/issues#comments 52 """ 53 54 def _initAttributes(self) -> None: 55 self._body: Attribute[str] = NotSet 56 self._created_at: Attribute[datetime] = NotSet 57 self._id: Attribute[int] = NotSet 58 self._issue_url: Attribute[str] = NotSet 59 self._updated_at: Attribute[datetime] = NotSet 60 self._url: Attribute[str] = NotSet 61 self._html_url: Attribute[str] = NotSet 62 self._user: Attribute[github.NamedUser.NamedUser] = NotSet 63 64 def __repr__(self) -> str: 65 return self.get__repr__({"id": self._id.value, "user": self._user.value}) 66 67 @property 68 def body(self) -> str: 69 self._completeIfNotSet(self._body) 70 return self._body.value 71 72 @property 73 def created_at(self) -> datetime: 74 self._completeIfNotSet(self._created_at) 75 return self._created_at.value 76 77 @property 78 def id(self) -> int: 79 self._completeIfNotSet(self._id) 80 return self._id.value 81 82 @property 83 def issue_url(self) -> str: 84 self._completeIfNotSet(self._issue_url) 85 return self._issue_url.value 86 87 @property 88 def updated_at(self) -> datetime: 89 self._completeIfNotSet(self._updated_at) 90 return self._updated_at.value 91 92 @property 93 def url(self) -> str: 94 self._completeIfNotSet(self._url) 95 return self._url.value 96 97 @property 98 def html_url(self) -> str: 99 self._completeIfNotSet(self._html_url) 100 return self._html_url.value 101 102 @property 103 def user(self) -> github.NamedUser.NamedUser: 104 self._completeIfNotSet(self._user) 105 return self._user.value 106 107 def delete(self) -> None: 108 """ 109 :calls: `DELETE /repos/{owner}/{repo}/issues/comments/{id} <https://docs.github.com/en/rest/reference/issues#comments>`_ 110 """ 111 headers, data = self._requester.requestJsonAndCheck("DELETE", self.url) 112 113 def edit(self, body: str) -> None: 114 """ 115 :calls: `PATCH /repos/{owner}/{repo}/issues/comments/{id} <https://docs.github.com/en/rest/reference/issues#comments>`_ 116 """ 117 assert isinstance(body, str), body 118 post_parameters = { 119 "body": body, 120 } 121 headers, data = self._requester.requestJsonAndCheck("PATCH", self.url, input=post_parameters) 122 self._useAttributes(data) 123 124 def get_reactions(self) -> PaginatedList[Reaction]: 125 """ 126 :calls: `GET /repos/{owner}/{repo}/issues/comments/{id}/reactions 127 <https://docs.github.com/en/rest/reference/reactions#list-reactions-for-an-issue-comment>`_ 128 """ 129 return PaginatedList( 130 github.Reaction.Reaction, 131 self._requester, 132 f"{self.url}/reactions", 133 None, 134 headers={"Accept": Consts.mediaTypeReactionsPreview}, 135 ) 136 137 def create_reaction(self, reaction_type: str) -> Reaction: 138 """ 139 :calls: `POST /repos/{owner}/{repo}/issues/comments/{id}/reactions 140 <https://docs.github.com/en/rest/reference/reactions#create-reaction-for-an-issue-comment>`_ 141 """ 142 assert isinstance(reaction_type, str), reaction_type 143 post_parameters = { 144 "content": reaction_type, 145 } 146 headers, data = self._requester.requestJsonAndCheck( 147 "POST", 148 f"{self.url}/reactions", 149 input=post_parameters, 150 headers={"Accept": Consts.mediaTypeReactionsPreview}, 151 ) 152 return github.Reaction.Reaction(self._requester, headers, data, completed=True) 153 154 def delete_reaction(self, reaction_id: int) -> bool: 155 """ 156 :calls: `DELETE /repos/{owner}/{repo}/issues/comments/{comment_id}/reactions/{reaction_id} 157 <https://docs.github.com/en/rest/reference/reactions#delete-an-issue-comment-reaction>`_ 158 """ 159 assert isinstance(reaction_id, int), reaction_id 160 status, _, _ = self._requester.requestJson( 161 "DELETE", 162 f"{self.url}/reactions/{reaction_id}", 163 headers={"Accept": Consts.mediaTypeReactionsPreview}, 164 ) 165 return status == 204 166 167 def _useAttributes(self, attributes: dict[str, Any]) -> None: 168 if "body" in attributes: # pragma no branch 169 self._body = self._makeStringAttribute(attributes["body"]) 170 if "created_at" in attributes: # pragma no branch 171 self._created_at = self._makeDatetimeAttribute(attributes["created_at"]) 172 if "id" in attributes: # pragma no branch 173 self._id = self._makeIntAttribute(attributes["id"]) 174 if "issue_url" in attributes: # pragma no branch 175 self._issue_url = self._makeStringAttribute(attributes["issue_url"]) 176 if "updated_at" in attributes: # pragma no branch 177 self._updated_at = self._makeDatetimeAttribute(attributes["updated_at"]) 178 if "url" in attributes: # pragma no branch 179 self._url = self._makeStringAttribute(attributes["url"]) 180 if "html_url" in attributes: # pragma no branch 181 self._html_url = self._makeStringAttribute(attributes["html_url"]) 182 if "user" in attributes: # pragma no branch 183 self._user = self._makeClassAttribute(github.NamedUser.NamedUser, attributes["user"])