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