Installation.py
1 ############################ Copyrights and license ############################ 2 # # 3 # Copyright 2017 Jannis Gebauer <ja.geb@me.com> # 4 # Copyright 2017 Simon <spam@esemi.ru> # 5 # Copyright 2018 Wan Liuyang <tsfdye@gmail.com> # 6 # Copyright 2018 sfdye <tsfdye@gmail.com> # 7 # Copyright 2019 Rigas Papathanasopoulos <rigaspapas@gmail.com> # 8 # # 9 # This file is part of PyGithub. # 10 # http://pygithub.readthedocs.io/ # 11 # # 12 # PyGithub is free software: you can redistribute it and/or modify it under # 13 # the terms of the GNU Lesser General Public License as published by the Free # 14 # Software Foundation, either version 3 of the License, or (at your option) # 15 # any later version. # 16 # # 17 # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # 18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # 19 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # 20 # details. # 21 # # 22 # You should have received a copy of the GNU Lesser General Public License # 23 # along with PyGithub. If not, see <http://www.gnu.org/licenses/>. # 24 # # 25 ################################################################################ 26 from __future__ import annotations 27 28 from typing import TYPE_CHECKING, Any 29 30 import github.Authorization 31 import github.Event 32 import github.Gist 33 import github.GithubObject 34 import github.Issue 35 import github.Notification 36 import github.Organization 37 import github.PaginatedList 38 import github.Plan 39 import github.Repository 40 import github.UserKey 41 from github import Consts 42 from github.Auth import AppAuth 43 from github.GithubObject import Attribute, NonCompletableGithubObject, NotSet 44 from github.PaginatedList import PaginatedList 45 from github.Requester import Requester 46 47 if TYPE_CHECKING: 48 from github.MainClass import Github 49 50 INTEGRATION_PREVIEW_HEADERS = {"Accept": Consts.mediaTypeIntegrationPreview} 51 52 53 class Installation(NonCompletableGithubObject): 54 """ 55 This class represents Installations. The reference can be found here https://docs.github.com/en/rest/reference/apps#installations 56 """ 57 58 def __init__( 59 self, 60 requester: Requester, 61 headers: dict[str, str | int], 62 attributes: Any, 63 completed: bool, 64 ) -> None: 65 super().__init__(requester, headers, attributes, completed) 66 67 auth = self._requester.auth if self._requester is not None else None 68 # Usually, an Installation is created from a Requester with App authentication 69 if isinstance(auth, AppAuth): 70 # But the installation has to authenticate as an installation (e.g. for get_repos()) 71 auth = auth.get_installation_auth(self.id, requester=self._requester) 72 self._requester = self._requester.withAuth(auth) 73 74 def _initAttributes(self) -> None: 75 self._id: Attribute[int] = NotSet 76 self._app_id: Attribute[int] = NotSet 77 self._target_id: Attribute[int] = NotSet 78 self._target_type: Attribute[str] = NotSet 79 80 def __repr__(self) -> str: 81 return self.get__repr__({"id": self._id.value}) 82 83 def get_github_for_installation(self) -> Github: 84 return github.Github(**self._requester.kwargs) 85 86 @property 87 def id(self) -> int: 88 return self._id.value 89 90 @property 91 def app_id(self) -> int: 92 return self._app_id.value 93 94 @property 95 def target_id(self) -> int: 96 return self._target_id.value 97 98 @property 99 def target_type(self) -> str: 100 return self._target_type.value 101 102 def get_repos(self) -> PaginatedList[github.Repository.Repository]: 103 """ 104 :calls: `GET /installation/repositories <https://docs.github.com/en/rest/reference/integrations/installations#list-repositories>`_ 105 """ 106 url_parameters: dict[str, Any] = {} 107 108 return PaginatedList( 109 contentClass=github.Repository.Repository, 110 requester=self._requester, 111 firstUrl="/installation/repositories", 112 firstParams=url_parameters, 113 headers=INTEGRATION_PREVIEW_HEADERS, 114 list_item="repositories", 115 ) 116 117 def _useAttributes(self, attributes: dict[str, Any]) -> None: 118 if "id" in attributes: # pragma no branch 119 self._id = self._makeIntAttribute(attributes["id"]) 120 if "app_id" in attributes: # pragma no branch 121 self._app_id = self._makeIntAttribute(attributes["app_id"]) 122 if "target_id" in attributes: # pragma no branch 123 self._target_id = self._makeIntAttribute(attributes["target_id"]) 124 if "target_type" in attributes: # pragma no branch 125 self._target_type = self._makeStringAttribute(attributes["target_type"])