/ github / Migration.py
Migration.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 2013 martinqt <m.ki2@laposte.net>                                  #
  8  # Copyright 2014 Andy Casey <acasey@mso.anu.edu.au>                            #
  9  # Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net>                 #
 10  # Copyright 2016 Jannis Gebauer <ja.geb@me.com>                                #
 11  # Copyright 2016 John Eskew <jeskew@edx.org>                                   #
 12  # Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com>          #
 13  # Copyright 2018 sfdye <tsfdye@gmail.com>                                      #
 14  #                                                                              #
 15  # This file is part of PyGithub.                                               #
 16  # http://pygithub.readthedocs.io/                                              #
 17  #                                                                              #
 18  # PyGithub is free software: you can redistribute it and/or modify it under    #
 19  # the terms of the GNU Lesser General Public License as published by the Free  #
 20  # Software Foundation, either version 3 of the License, or (at your option)    #
 21  # any later version.                                                           #
 22  #                                                                              #
 23  # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
 24  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
 25  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
 26  # details.                                                                     #
 27  #                                                                              #
 28  # You should have received a copy of the GNU Lesser General Public License     #
 29  # along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
 30  #                                                                              #
 31  ################################################################################
 32  from __future__ import annotations
 33  
 34  from datetime import datetime
 35  from typing import Any
 36  
 37  import github.GithubObject
 38  import github.NamedUser
 39  import github.PaginatedList
 40  import github.Repository
 41  from github import Consts
 42  from github.GithubObject import Attribute, CompletableGithubObject, NotSet
 43  
 44  
 45  class Migration(CompletableGithubObject):
 46      """
 47      This class represents Migrations. The reference can be found here https://docs.github.com/en/rest/reference/migrations
 48      """
 49  
 50      def _initAttributes(self) -> None:
 51          self._id: Attribute[int] = NotSet
 52          self._owner: Attribute[github.NamedUser.NamedUser] = NotSet
 53          self._guid: Attribute[str] = NotSet
 54          self._state: Attribute[str] = NotSet
 55          self._lock_repositories: Attribute[bool] = NotSet
 56          self._exclude_attachments: Attribute[bool] = NotSet
 57          self._repositories: Attribute[list[github.Repository.Repository]] = NotSet
 58          self._url: Attribute[str] = NotSet
 59  
 60      def __repr__(self) -> str:
 61          return self.get__repr__({"state": self._state.value, "url": self._url.value})
 62  
 63      @property
 64      def id(self) -> int:
 65          return self._id.value
 66  
 67      @property
 68      def owner(self) -> github.NamedUser.NamedUser:
 69          self._completeIfNotSet(self._owner)
 70          return self._owner.value
 71  
 72      @property
 73      def guid(self) -> str:
 74          self._completeIfNotSet(self._guid)
 75          return self._guid.value
 76  
 77      @property
 78      def state(self) -> str:
 79          self._completeIfNotSet(self._guid)
 80          return self._state.value
 81  
 82      @property
 83      def lock_repositories(self) -> bool:
 84          self._completeIfNotSet(self._repositories)
 85          return self._lock_repositories.value
 86  
 87      @property
 88      def exclude_attachments(self) -> bool:
 89          self._completeIfNotSet(self._exclude_attachments)
 90          return self._exclude_attachments.value
 91  
 92      @property
 93      def repositories(self) -> list[github.Repository.Repository]:
 94          self._completeIfNotSet(self._repositories)
 95          return self._repositories.value
 96  
 97      @property
 98      def url(self) -> str:
 99          self._completeIfNotSet(self._url)
100          return self._url.value
101  
102      @property
103      def created_at(self) -> datetime:
104          self._completeIfNotSet(self._created_at)
105          return self._created_at.value
106  
107      @property
108      def updated_at(self) -> datetime:
109          self._completeIfNotSet(self._updated_at)
110          return self._updated_at.value
111  
112      def get_status(self) -> str:
113          """
114          :calls: `GET /user/migrations/{migration_id} <https://docs.github.com/en/rest/reference/migrations>`_
115          """
116          headers, data = self._requester.requestJsonAndCheck(
117              "GET", self.url, headers={"Accept": Consts.mediaTypeMigrationPreview}
118          )
119          self._useAttributes(data)
120          return self.state
121  
122      def get_archive_url(self) -> str:
123          """
124          :calls: `GET /user/migrations/{migration_id}/archive <https://docs.github.com/en/rest/reference/migrations>`_
125          """
126          headers, data = self._requester.requestJsonAndCheck(
127              "GET",
128              f"{self.url}/archive",
129              headers={"Accept": Consts.mediaTypeMigrationPreview},
130          )
131          return data["data"]
132  
133      def delete(self) -> None:
134          """
135          :calls: `DELETE /user/migrations/{migration_id}/archive <https://docs.github.com/en/rest/reference/migrations>`_
136          """
137          headers, data = self._requester.requestJsonAndCheck(
138              "DELETE",
139              f"{self.url}/archive",
140              headers={"Accept": Consts.mediaTypeMigrationPreview},
141          )
142  
143      def unlock_repo(self, repo_name: str) -> None:
144          """
145          :calls: `DELETE /user/migrations/{migration_id}/repos/{repo_name}/lock <https://docs.github.com/en/rest/reference/migrations>`_
146          """
147          assert isinstance(repo_name, str), repo_name
148          headers, data = self._requester.requestJsonAndCheck(
149              "DELETE",
150              f"{self.url}/repos/{repo_name}/lock",
151              headers={"Accept": Consts.mediaTypeMigrationPreview},
152          )
153  
154      def _useAttributes(self, attributes: dict[str, Any]) -> None:
155          if "id" in attributes:
156              self._id = self._makeIntAttribute(attributes["id"])
157          if "owner" in attributes:
158              self._owner = self._makeClassAttribute(github.NamedUser.NamedUser, attributes["owner"])
159          if "guid" in attributes:
160              self._guid = self._makeStringAttribute(attributes["guid"])
161          if "state" in attributes:
162              self._state = self._makeStringAttribute(attributes["state"])
163          if "lock_repositories" in attributes:
164              self._lock_repositories = self._makeBoolAttribute(attributes["lock_repositories"])
165          if "exclude_attachments" in attributes:
166              self._exclude_attachments = self._makeBoolAttribute(attributes["exclude_attachments"])
167          if "repositories" in attributes:
168              self._repositories = self._makeListOfClassesAttribute(
169                  github.Repository.Repository, attributes["repositories"]
170              )
171          if "url" in attributes:
172              self._url = self._makeStringAttribute(attributes["url"])
173          if "created_at" in attributes:
174              self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
175          if "updated_at" in attributes:
176              self._updated_at = self._makeDatetimeAttribute(attributes["updated_at"])