/ github / Deployment.py
Deployment.py
  1  ############################ Copyrights and license ############################
  2  #                                                                              #
  3  # Copyright 2020 Steve Kowalik <steven@wedontsleep.org>                        #
  4  # Copyright 2020 Colby Gallup <colbygallup@gmail.com>                          #
  5  # Copyright 2020 Pascal Hofmann <mail@pascalhofmann.de>                        #
  6  #                                                                              #
  7  # This file is part of PyGithub.                                               #
  8  # http://pygithub.readthedocs.io/                                              #
  9  #                                                                              #
 10  # PyGithub is free software: you can redistribute it and/or modify it under    #
 11  # the terms of the GNU Lesser General Public License as published by the Free  #
 12  # Software Foundation, either version 3 of the License, or (at your option)    #
 13  # any later version.                                                           #
 14  #                                                                              #
 15  # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
 16  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
 17  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
 18  # details.                                                                     #
 19  #                                                                              #
 20  # You should have received a copy of the GNU Lesser General Public License     #
 21  # along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
 22  #                                                                              #
 23  ################################################################################
 24  from __future__ import annotations
 25  
 26  from datetime import datetime
 27  from typing import Any
 28  
 29  import github.Consts
 30  import github.DeploymentStatus
 31  import github.NamedUser
 32  from github.GithubObject import Attribute, CompletableGithubObject, NotSet, Opt
 33  from github.PaginatedList import PaginatedList
 34  
 35  
 36  class Deployment(CompletableGithubObject):
 37      """
 38      This class represents Deployments. The reference can be found here https://docs.github.com/en/rest/reference/repos#deployments
 39      """
 40  
 41      def _initAttributes(self) -> None:
 42          self._id: Attribute[int] = NotSet
 43          self._ref: Attribute[str] = NotSet
 44          self._url: Attribute[str] = NotSet
 45          self._sha: Attribute[str] = NotSet
 46          self._task: Attribute[str] = NotSet
 47          self._payload: Attribute[dict[str, Any]] = NotSet
 48          self._original_environment: Attribute[str] = NotSet
 49          self._environment: Attribute[str] = NotSet
 50          self._production_environment: Attribute[bool] = NotSet
 51          self._transient_environment: Attribute[bool] = NotSet
 52          self._description: Attribute[str] = NotSet
 53          self._creator: Attribute[github.NamedUser.NamedUser] = NotSet
 54          self._created_at: Attribute[datetime] = NotSet
 55          self._updated_at: Attribute[datetime | None] = NotSet
 56          self._statuses_url: Attribute[str] = NotSet
 57          self._repository_url: Attribute[str] = NotSet
 58  
 59      def __repr__(self) -> str:
 60          return self.get__repr__({"id": self._id.value, "url": self._url.value})
 61  
 62      @property
 63      def id(self) -> int:
 64          self._completeIfNotSet(self._id)
 65          return self._id.value
 66  
 67      @property
 68      def ref(self) -> str:
 69          self._completeIfNotSet(self._ref)
 70          return self._ref.value
 71  
 72      @property
 73      def url(self) -> str:
 74          self._completeIfNotSet(self._url)
 75          return self._url.value
 76  
 77      @property
 78      def sha(self) -> str:
 79          self._completeIfNotSet(self._sha)
 80          return self._sha.value
 81  
 82      @property
 83      def task(self) -> str:
 84          self._completeIfNotSet(self._task)
 85          return self._task.value
 86  
 87      @property
 88      def payload(self) -> dict[str, Any]:
 89          self._completeIfNotSet(self._payload)
 90          return self._payload.value
 91  
 92      @property
 93      def original_environment(self) -> str:
 94          self._completeIfNotSet(self._original_environment)
 95          return self._original_environment.value
 96  
 97      @property
 98      def environment(self) -> str:
 99          self._completeIfNotSet(self._environment)
100          return self._environment.value
101  
102      @property
103      def production_environment(self) -> bool:
104          self._completeIfNotSet(self._production_environment)
105          return self._production_environment.value
106  
107      @property
108      def transient_environment(self) -> bool:
109          self._completeIfNotSet(self._transient_environment)
110          return self._transient_environment.value
111  
112      @property
113      def description(self) -> str:
114          self._completeIfNotSet(self._description)
115          return self._description.value
116  
117      @property
118      def creator(self) -> github.NamedUser.NamedUser:
119          self._completeIfNotSet(self._creator)
120          return self._creator.value
121  
122      @property
123      def created_at(self) -> datetime:
124          self._completeIfNotSet(self._created_at)
125          return self._created_at.value
126  
127      @property
128      def updated_at(self) -> datetime | None:
129          self._completeIfNotSet(self._updated_at)
130          return self._updated_at.value
131  
132      @property
133      def statuses_url(self) -> str:
134          self._completeIfNotSet(self._statuses_url)
135          return self._statuses_url.value
136  
137      @property
138      def repository_url(self) -> str:
139          self._completeIfNotSet(self._repository_url)
140          return self._repository_url.value
141  
142      def get_statuses(self) -> PaginatedList[github.DeploymentStatus.DeploymentStatus]:
143          """
144          :calls: `GET /repos/{owner}/deployments/{deployment_id}/statuses <https://docs.github.com/en/rest/reference/repos#list-deployments>`_
145          """
146          return PaginatedList(
147              github.DeploymentStatus.DeploymentStatus,
148              self._requester,
149              f"{self.url}/statuses",
150              None,
151              headers={"Accept": self._get_accept_header()},
152          )
153  
154      def get_status(self, id_: int) -> github.DeploymentStatus.DeploymentStatus:
155          """
156          :calls: `GET /repos/{owner}/deployments/{deployment_id}/statuses/{status_id}  <https://docs.github.com/en/rest/reference/repos#get-a-deployment>`_
157          """
158          assert isinstance(id_, int), id_
159          headers, data = self._requester.requestJsonAndCheck(
160              "GET",
161              f"{self.url}/statuses/{id_}",
162              headers={"Accept": self._get_accept_header()},
163          )
164          return github.DeploymentStatus.DeploymentStatus(self._requester, headers, data, completed=True)
165  
166      def create_status(
167          self,
168          state: str,
169          target_url: Opt[str] = NotSet,
170          description: Opt[str] = NotSet,
171          environment: Opt[str] = NotSet,
172          environment_url: Opt[str] = NotSet,
173          auto_inactive: Opt[bool] = NotSet,
174      ) -> github.DeploymentStatus.DeploymentStatus:
175          """
176          :calls: `POST /repos/{owner}/{repo}/deployments/{deployment_id}/statuses <https://docs.github.com/en/rest/reference/repos#create-a-deployment-status>`_
177          """
178          assert isinstance(state, str), state
179          assert target_url is NotSet or isinstance(target_url, str), target_url
180          assert description is NotSet or isinstance(description, str), description
181          assert environment is NotSet or isinstance(environment, str), environment
182          assert environment_url is NotSet or isinstance(environment_url, str), environment_url
183          assert auto_inactive is NotSet or isinstance(auto_inactive, bool), auto_inactive
184  
185          post_parameters = NotSet.remove_unset_items(
186              {
187                  "state": state,
188                  "target_url": target_url,
189                  "description": description,
190                  "environment": environment,
191                  "environment_url": environment_url,
192                  "auto_inactive": auto_inactive,
193              }
194          )
195  
196          headers, data = self._requester.requestJsonAndCheck(
197              "POST",
198              f"{self.url}/statuses",
199              input=post_parameters,
200              headers={"Accept": self._get_accept_header()},
201          )
202          return github.DeploymentStatus.DeploymentStatus(self._requester, headers, data, completed=True)
203  
204      @staticmethod
205      def _get_accept_header() -> str:
206          return ", ".join(
207              [
208                  github.Consts.deploymentEnhancementsPreview,
209                  github.Consts.deploymentStatusEnhancementsPreview,
210              ]
211          )
212  
213      def _useAttributes(self, attributes: dict[str, Any]) -> None:
214          if "id" in attributes:  # pragma no branch
215              self._id = self._makeIntAttribute(attributes["id"])
216          if "production_environment" in attributes:  # pragma no branch
217              self._production_environment = self._makeBoolAttribute(attributes["production_environment"])
218          if "ref" in attributes:  # pragma no branch
219              self._ref = self._makeStringAttribute(attributes["ref"])
220          if "transient_environment" in attributes:  # pragma no branch
221              self._transient_environment = self._makeBoolAttribute(attributes["transient_environment"])
222          if "url" in attributes:  # pragma no branch
223              self._url = self._makeStringAttribute(attributes["url"])
224          if "sha" in attributes:  # pragma no branch
225              self._sha = self._makeStringAttribute(attributes["sha"])
226          if "task" in attributes:  # pragma no branch
227              self._task = self._makeStringAttribute(attributes["task"])
228          if "payload" in attributes:  # pragma no branch
229              self._payload = self._makeDictAttribute(attributes["payload"])
230          if "original_environment" in attributes:  # pragma no branch
231              self._original_environment = self._makeStringAttribute(attributes["original_environment"])
232          if "environment" in attributes:  # pragma no branch
233              self._environment = self._makeStringAttribute(attributes["environment"])
234          if "description" in attributes:  # pragma no branch
235              self._description = self._makeStringAttribute(attributes["description"])
236          if "creator" in attributes:  # pragma no branch
237              self._creator = self._makeClassAttribute(github.NamedUser.NamedUser, attributes["creator"])
238          if "created_at" in attributes:  # pragma no branch
239              self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
240          if "updated_at" in attributes:  # pragma no branch
241              self._updated_at = self._makeDatetimeAttribute(attributes["updated_at"])
242          if "statuses_url" in attributes:  # pragma no branch
243              self._statuses_url = self._makeStringAttribute(attributes["statuses_url"])
244          if "repository_url" in attributes:  # pragma no branch
245              self._repository_url = self._makeStringAttribute(attributes["repository_url"])