BranchProtection.py
1 ############################ Copyrights and license ############################ 2 # # 3 # Copyright 2018 Steve Kowalik <steven@wedontsleep.org> # 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 23 from __future__ import annotations 24 25 from typing import TYPE_CHECKING, Any 26 27 import github.GithubObject 28 import github.NamedUser 29 import github.RequiredPullRequestReviews 30 import github.RequiredStatusChecks 31 import github.Team 32 from github.GithubObject import Attribute, NotSet, Opt, is_defined 33 from github.PaginatedList import PaginatedList 34 35 if TYPE_CHECKING: 36 from github.NamedUser import NamedUser 37 from github.RequiredPullRequestReviews import RequiredPullRequestReviews 38 from github.RequiredStatusChecks import RequiredStatusChecks 39 from github.Team import Team 40 41 42 class BranchProtection(github.GithubObject.CompletableGithubObject): 43 """ 44 This class represents Branch Protection. The reference can be found here https://docs.github.com/en/rest/reference/repos#get-branch-protection 45 """ 46 47 def __repr__(self) -> str: 48 return self.get__repr__({"url": self._url.value}) 49 50 def _initAttributes(self) -> None: 51 self._url: Attribute[str] = NotSet 52 self._required_status_checks: Attribute[RequiredStatusChecks] = NotSet 53 self._enforce_admins: Attribute[bool] = NotSet 54 self._required_linear_history: Attribute[bool] = github.GithubObject.NotSet 55 self._required_pull_request_reviews: Attribute[RequiredPullRequestReviews] = NotSet 56 self._user_push_restrictions: Opt[str] = NotSet 57 self._team_push_restrictions: Opt[str] = NotSet 58 59 @property 60 def url(self) -> str: 61 self._completeIfNotSet(self._url) 62 return self._url.value 63 64 @property 65 def required_status_checks(self) -> RequiredStatusChecks: 66 self._completeIfNotSet(self._required_status_checks) 67 return self._required_status_checks.value 68 69 @property 70 def enforce_admins(self) -> bool: 71 self._completeIfNotSet(self._enforce_admins) 72 return self._enforce_admins.value 73 74 @property 75 def required_linear_history(self) -> bool: 76 self._completeIfNotSet(self._required_linear_history) 77 return self._required_linear_history.value 78 79 @property 80 def required_pull_request_reviews(self) -> RequiredPullRequestReviews: 81 self._completeIfNotSet(self._required_pull_request_reviews) 82 return self._required_pull_request_reviews.value 83 84 def get_user_push_restrictions(self) -> PaginatedList[NamedUser] | None: 85 if not is_defined(self._user_push_restrictions): 86 return None 87 return PaginatedList( 88 github.NamedUser.NamedUser, 89 self._requester, 90 self._user_push_restrictions, 91 None, 92 ) 93 94 def get_team_push_restrictions(self) -> PaginatedList[Team] | None: 95 if not is_defined(self._team_push_restrictions): 96 return None 97 return github.PaginatedList.PaginatedList(github.Team.Team, self._requester, self._team_push_restrictions, None) 98 99 def _useAttributes(self, attributes: dict[str, Any]) -> None: 100 if "url" in attributes: # pragma no branch 101 self._url = self._makeStringAttribute(attributes["url"]) 102 if "required_status_checks" in attributes: # pragma no branch 103 self._required_status_checks = self._makeClassAttribute( 104 github.RequiredStatusChecks.RequiredStatusChecks, 105 attributes["required_status_checks"], 106 ) 107 if "enforce_admins" in attributes: # pragma no branch 108 self._enforce_admins = self._makeBoolAttribute(attributes["enforce_admins"]["enabled"]) 109 if "required_pull_request_reviews" in attributes: # pragma no branch 110 self._required_pull_request_reviews = self._makeClassAttribute( 111 github.RequiredPullRequestReviews.RequiredPullRequestReviews, 112 attributes["required_pull_request_reviews"], 113 ) 114 if "required_linear_history" in attributes: # pragma no branch 115 self._required_linear_history = self._makeBoolAttribute(attributes["required_linear_history"]["enabled"]) 116 if "restrictions" in attributes: # pragma no branch 117 self._user_push_restrictions = attributes["restrictions"]["users_url"] 118 self._team_push_restrictions = attributes["restrictions"]["teams_url"]