/ github / CheckRun.py
CheckRun.py
  1  ############################ Copyrights and license ############################
  2  #                                                                              #
  3  # Copyright 2020 Dhruv Manilawala <dhruvmanila@gmail.com>                      #
  4  #                                                                              #
  5  # This file is part of PyGithub.                                               #
  6  # http://pygithub.readthedocs.io/                                              #
  7  #                                                                              #
  8  # PyGithub is free software: you can redistribute it and/or modify it under    #
  9  # the terms of the GNU Lesser General Public License as published by the Free  #
 10  # Software Foundation, either version 3 of the License, or (at your option)    #
 11  # any later version.                                                           #
 12  #                                                                              #
 13  # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
 14  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
 15  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
 16  # details.                                                                     #
 17  #                                                                              #
 18  # You should have received a copy of the GNU Lesser General Public License     #
 19  # along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
 20  #                                                                              #
 21  ################################################################################
 22  
 23  from __future__ import annotations
 24  
 25  from datetime import datetime
 26  from typing import TYPE_CHECKING, Any
 27  
 28  import github.CheckRunAnnotation
 29  import github.CheckRunOutput
 30  import github.GithubApp
 31  import github.GithubObject
 32  import github.PullRequest
 33  from github.GithubObject import (
 34      Attribute,
 35      CompletableGithubObject,
 36      NotSet,
 37      Opt,
 38      is_defined,
 39      is_optional,
 40      is_optional_list,
 41  )
 42  from github.PaginatedList import PaginatedList
 43  
 44  if TYPE_CHECKING:
 45      from github.CheckRunAnnotation import CheckRunAnnotation
 46      from github.CheckRunOutput import CheckRunOutput
 47      from github.GithubApp import GithubApp
 48      from github.PullRequest import PullRequest
 49  
 50  
 51  class CheckRun(CompletableGithubObject):
 52      """
 53      This class represents check runs.
 54      The reference can be found here https://docs.github.com/en/rest/reference/checks#check-runs
 55      """
 56  
 57      def _initAttributes(self) -> None:
 58          self._app: Attribute[GithubApp] = NotSet
 59          self._check_suite_id: Attribute[int] = NotSet
 60          self._completed_at: Attribute[datetime | None] = NotSet
 61          self._conclusion: Attribute[str] = NotSet
 62          self._details_url: Attribute[str] = NotSet
 63          self._external_id: Attribute[str] = NotSet
 64          self._head_sha: Attribute[str] = NotSet
 65          self._html_url: Attribute[str] = NotSet
 66          self._id: Attribute[int] = NotSet
 67          self._name: Attribute[str] = NotSet
 68          self._node_id: Attribute[str] = NotSet
 69          self._output: Attribute[github.CheckRunOutput.CheckRunOutput] = NotSet
 70          self._pull_requests: Attribute[list[PullRequest]] = NotSet
 71          self._started_at: Attribute[datetime] = NotSet
 72          self._status: Attribute[str] = NotSet
 73          self._url: Attribute[str] = NotSet
 74  
 75      def __repr__(self) -> str:
 76          return self.get__repr__({"id": self._id.value, "conclusion": self._conclusion.value})
 77  
 78      @property
 79      def app(self) -> GithubApp:
 80          self._completeIfNotSet(self._app)
 81          return self._app.value
 82  
 83      @property
 84      def check_suite_id(self) -> int:
 85          self._completeIfNotSet(self._check_suite_id)
 86          return self._check_suite_id.value
 87  
 88      @property
 89      def completed_at(self) -> datetime | None:
 90          self._completeIfNotSet(self._completed_at)
 91          return self._completed_at.value
 92  
 93      @property
 94      def conclusion(self) -> str:
 95          self._completeIfNotSet(self._conclusion)
 96          return self._conclusion.value
 97  
 98      @property
 99      def details_url(self) -> str:
100          self._completeIfNotSet(self._details_url)
101          return self._details_url.value
102  
103      @property
104      def external_id(self) -> str:
105          self._completeIfNotSet(self._external_id)
106          return self._external_id.value
107  
108      @property
109      def head_sha(self) -> str:
110          self._completeIfNotSet(self._head_sha)
111          return self._head_sha.value
112  
113      @property
114      def html_url(self) -> str:
115          self._completeIfNotSet(self._html_url)
116          return self._html_url.value
117  
118      @property
119      def id(self) -> int:
120          self._completeIfNotSet(self._id)
121          return self._id.value
122  
123      @property
124      def name(self) -> str:
125          self._completeIfNotSet(self._name)
126          return self._name.value
127  
128      @property
129      def node_id(self) -> str:
130          self._completeIfNotSet(self._node_id)
131          return self._node_id.value
132  
133      @property
134      def output(self) -> CheckRunOutput:
135          self._completeIfNotSet(self._output)
136          return self._output.value
137  
138      @property
139      def pull_requests(self) -> list[PullRequest]:
140          self._completeIfNotSet(self._pull_requests)
141          return self._pull_requests.value
142  
143      @property
144      def started_at(self) -> datetime:
145          self._completeIfNotSet(self._started_at)
146          return self._started_at.value
147  
148      @property
149      def status(self) -> str:
150          self._completeIfNotSet(self._status)
151          return self._status.value
152  
153      @property
154      def url(self) -> str:
155          self._completeIfNotSet(self._url)
156          return self._url.value
157  
158      def get_annotations(self) -> PaginatedList[CheckRunAnnotation]:
159          """
160          :calls: `GET /repos/{owner}/{repo}/check-runs/{check_run_id}/annotations <https://docs.github.com/en/rest/reference/checks#list-check-run-annotations>`_
161          """
162          return PaginatedList(
163              github.CheckRunAnnotation.CheckRunAnnotation,
164              self._requester,
165              f"{self.url}/annotations",
166              None,
167              headers={"Accept": "application/vnd.github.v3+json"},
168          )
169  
170      def edit(
171          self,
172          name: Opt[str] = NotSet,
173          head_sha: Opt[str] = NotSet,
174          details_url: Opt[str] = NotSet,
175          external_id: Opt[str] = NotSet,
176          status: Opt[str] = NotSet,
177          started_at: Opt[datetime] = NotSet,
178          conclusion: Opt[str] = NotSet,
179          completed_at: Opt[datetime] = NotSet,
180          output: Opt[dict] = NotSet,
181          actions: Opt[list[dict]] = NotSet,
182      ) -> None:
183          """
184          :calls: `PATCH /repos/{owner}/{repo}/check-runs/{check_run_id} <https://docs.github.com/en/rest/reference/checks#update-a-check-run>`_
185          """
186          assert is_optional(name, str), name
187          assert is_optional(head_sha, str), head_sha
188          assert is_optional(details_url, str), details_url
189          assert is_optional(external_id, str), external_id
190          assert is_optional(status, str), status
191          assert is_optional(started_at, datetime), started_at
192          assert is_optional(conclusion, str), conclusion
193          assert is_optional(completed_at, datetime), completed_at
194          assert is_optional(output, dict), output
195          assert is_optional_list(actions, dict), actions
196  
197          post_parameters: dict[str, Any] = {}
198          if is_defined(name):
199              post_parameters["name"] = name
200          if is_defined(head_sha):
201              post_parameters["head_sha"] = head_sha
202          if is_defined(details_url):
203              post_parameters["details_url"] = details_url
204          if is_defined(external_id):
205              post_parameters["external_id"] = external_id
206          if is_defined(status):
207              post_parameters["status"] = status
208          if is_defined(started_at):
209              post_parameters["started_at"] = started_at.strftime("%Y-%m-%dT%H:%M:%SZ")
210          if is_defined(completed_at):
211              post_parameters["completed_at"] = completed_at.strftime("%Y-%m-%dT%H:%M:%SZ")
212          if is_defined(conclusion):
213              post_parameters["conclusion"] = conclusion
214          if is_defined(output):
215              post_parameters["output"] = output
216          if is_defined(actions):
217              post_parameters["actions"] = actions
218  
219          headers, data = self._requester.requestJsonAndCheck("PATCH", self.url, input=post_parameters)
220          self._useAttributes(data)
221  
222      def _useAttributes(self, attributes: dict[str, Any]) -> None:
223          if "app" in attributes:  # pragma no branch
224              self._app = self._makeClassAttribute(github.GithubApp.GithubApp, attributes["app"])
225          # This only gives us a dictionary with `id` attribute of `check_suite`
226          if "check_suite" in attributes and "id" in attributes["check_suite"]:  # pragma no branch
227              self._check_suite_id = self._makeIntAttribute(attributes["check_suite"]["id"])
228          if "completed_at" in attributes:  # pragma no branch
229              self._completed_at = self._makeDatetimeAttribute(attributes["completed_at"])
230          if "conclusion" in attributes:  # pragma no branch
231              self._conclusion = self._makeStringAttribute(attributes["conclusion"])
232          if "details_url" in attributes:  # pragma no branch
233              self._details_url = self._makeStringAttribute(attributes["details_url"])
234          if "external_id" in attributes:  # pragma no branch
235              self._external_id = self._makeStringAttribute(attributes["external_id"])
236          if "head_sha" in attributes:  # pragma no branch
237              self._head_sha = self._makeStringAttribute(attributes["head_sha"])
238          if "html_url" in attributes:  # pragma no branch
239              self._html_url = self._makeStringAttribute(attributes["html_url"])
240          if "id" in attributes:  # pragma no branch
241              self._id = self._makeIntAttribute(attributes["id"])
242          if "name" in attributes:  # pragma no branch
243              self._name = self._makeStringAttribute(attributes["name"])
244          if "node_id" in attributes:  # pragma no branch
245              self._node_id = self._makeStringAttribute(attributes["node_id"])
246          if "output" in attributes:  # pragma no branch
247              self._output = self._makeClassAttribute(github.CheckRunOutput.CheckRunOutput, attributes["output"])
248          if "pull_requests" in attributes:  # pragma no branch
249              self._pull_requests = self._makeListOfClassesAttribute(
250                  github.PullRequest.PullRequest, attributes["pull_requests"]
251              )
252          if "started_at" in attributes:  # pragma no branch
253              self._started_at = self._makeDatetimeAttribute(attributes["started_at"])
254          if "status" in attributes:  # pragma no branch
255              self._status = self._makeStringAttribute(attributes["status"])
256          if "url" in attributes:  # pragma no branch
257              self._url = self._makeStringAttribute(attributes["url"])