RepositoryAdvisoryCredit.py
1 ############################ Copyrights and license ############################ 2 # # 3 # Copyright 2023 Jonathan Leitschuh <Jonathan.Leitschuh@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 from __future__ import annotations 23 24 from typing import Any, Union 25 26 from typing_extensions import TypedDict 27 28 import github.NamedUser 29 from github.GithubObject import Attribute, NonCompletableGithubObject, NotSet 30 31 32 class SimpleCredit(TypedDict): 33 """ 34 A simple credit for a security advisory. 35 """ 36 37 login: str | github.NamedUser.NamedUser 38 type: str 39 40 41 Credit = Union[SimpleCredit, "RepositoryAdvisoryCredit"] 42 43 44 class RepositoryAdvisoryCredit(NonCompletableGithubObject): 45 """ 46 This class represents a credit that is assigned to a SecurityAdvisory. 47 The reference can be found here https://docs.github.com/en/rest/security-advisories/repository-advisories 48 """ 49 50 @property 51 def login(self) -> str: 52 """ 53 :type: string 54 """ 55 return self._login.value 56 57 @property 58 def type(self) -> str: 59 """ 60 :type: string 61 """ 62 return self._type.value 63 64 def _initAttributes(self) -> None: 65 self._login: Attribute[str] = NotSet 66 self._type: Attribute[str] = NotSet 67 68 def _useAttributes(self, attributes: dict[str, Any]) -> None: 69 if "login" in attributes: # pragma no branch 70 self._login = self._makeStringAttribute(attributes["login"]) 71 if "type" in attributes: # pragma no branch 72 self._type = self._makeStringAttribute(attributes["type"]) 73 74 @staticmethod 75 def _validate_credit(credit: Credit) -> None: 76 assert isinstance(credit, (dict, RepositoryAdvisoryCredit)), credit 77 if isinstance(credit, dict): 78 assert "login" in credit, credit 79 assert "type" in credit, credit 80 assert isinstance(credit["login"], (str, github.NamedUser.NamedUser)), credit["login"] 81 assert isinstance(credit["type"], str), credit["type"] 82 else: 83 assert isinstance(credit.login, str), credit.login 84 assert isinstance(credit.type, str), credit.type 85 86 @staticmethod 87 def _to_github_dict(credit: Credit) -> SimpleCredit: 88 assert isinstance(credit, (dict, RepositoryAdvisoryCredit)), credit 89 if isinstance(credit, dict): 90 assert "login" in credit, credit 91 assert "type" in credit, credit 92 assert isinstance(credit["login"], (str, github.NamedUser.NamedUser)), credit["login"] 93 login = credit["login"] 94 if isinstance(login, github.NamedUser.NamedUser): 95 login = login.login 96 return { 97 "login": login, 98 "type": credit["type"], 99 } 100 else: 101 return { 102 "login": credit.login, 103 "type": credit.type, 104 }