GitRef.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 2014 Vincent Jacques <vincent@vincent-jacques.net> # 8 # Copyright 2016 Jannis Gebauer <ja.geb@me.com> # 9 # Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com> # 10 # Copyright 2018 Wan Liuyang <tsfdye@gmail.com> # 11 # Copyright 2018 sfdye <tsfdye@gmail.com> # 12 # # 13 # This file is part of PyGithub. # 14 # http://pygithub.readthedocs.io/ # 15 # # 16 # PyGithub is free software: you can redistribute it and/or modify it under # 17 # the terms of the GNU Lesser General Public License as published by the Free # 18 # Software Foundation, either version 3 of the License, or (at your option) # 19 # any later version. # 20 # # 21 # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # 22 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # 23 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # 24 # details. # 25 # # 26 # You should have received a copy of the GNU Lesser General Public License # 27 # along with PyGithub. If not, see <http://www.gnu.org/licenses/>. # 28 # # 29 ################################################################################ 30 31 from __future__ import annotations 32 33 from typing import TYPE_CHECKING, Any 34 35 import github.GithubObject 36 import github.GitObject 37 from github.GithubObject import Attribute, CompletableGithubObject, NotSet, Opt, is_optional 38 39 if TYPE_CHECKING: 40 from github.GitObject import GitObject 41 42 43 class GitRef(CompletableGithubObject): 44 """ 45 This class represents GitRefs. The reference can be found here https://docs.github.com/en/rest/reference/git#references 46 """ 47 48 def _initAttributes(self) -> None: 49 self._object: Attribute[GitObject] = NotSet 50 self._ref: Attribute[str] = NotSet 51 self._url: Attribute[str] = NotSet 52 53 def __repr__(self) -> str: 54 return self.get__repr__({"ref": self._ref.value}) 55 56 @property 57 def object(self) -> GitObject: 58 self._completeIfNotSet(self._object) 59 return self._object.value 60 61 @property 62 def ref(self) -> str: 63 self._completeIfNotSet(self._ref) 64 return self._ref.value 65 66 @property 67 def url(self) -> str: 68 self._completeIfNotSet(self._url) 69 return self._url.value 70 71 def delete(self) -> None: 72 """ 73 :calls: `DELETE /repos/{owner}/{repo}/git/refs/{ref} <https://docs.github.com/en/rest/reference/git#references>`_ 74 """ 75 headers, data = self._requester.requestJsonAndCheck("DELETE", self.url) 76 77 def edit(self, sha: str, force: Opt[bool] = NotSet) -> None: 78 """ 79 :calls: `PATCH /repos/{owner}/{repo}/git/refs/{ref} <https://docs.github.com/en/rest/reference/git#references>`_ 80 """ 81 assert isinstance(sha, str), sha 82 assert is_optional(force, bool), force 83 post_parameters = NotSet.remove_unset_items({"sha": sha, "force": force}) 84 headers, data = self._requester.requestJsonAndCheck("PATCH", self.url, input=post_parameters) 85 self._useAttributes(data) 86 87 def _useAttributes(self, attributes: dict[str, Any]) -> None: 88 if "object" in attributes: # pragma no branch 89 self._object = self._makeClassAttribute(github.GitObject.GitObject, attributes["object"]) 90 if "ref" in attributes: # pragma no branch 91 self._ref = self._makeStringAttribute(attributes["ref"]) 92 if "url" in attributes: # pragma no branch 93 self._url = self._makeStringAttribute(attributes["url"])