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