# -*- coding: utf-8 -*- ############################ Copyrights and license ############################ # # # Copyright 2012 Vincent Jacques # # Copyright 2012 Zearin # # Copyright 2013 AKFish # # Copyright 2013 Vincent Jacques # # Copyright 2013 martinqt # # Copyright 2014 Vincent Jacques # # Copyright 2015 Kyle Hornberg # # Copyright 2016 Jannis Gebauer # # Copyright 2016 Peter Buckley # # Copyright 2018 Steve Kowalik # # Copyright 2018 Wan Liuyang # # Copyright 2018 sfdye # # # # This file is part of PyGithub. # # http://pygithub.readthedocs.io/ # # # # PyGithub is free software: you can redistribute it and/or modify it under # # the terms of the GNU Lesser General Public License as published by the Free # # Software Foundation, either version 3 of the License, or (at your option) # # any later version. # # # # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # # details. # # # # You should have received a copy of the GNU Lesser General Public License # # along with PyGithub. If not, see . # # # ################################################################################ import github.GithubObject import github.BranchProtection import github.Commit import github.RequiredPullRequestReviews import github.RequiredStatusChecks class Branch(github.GithubObject.NonCompletableGithubObject): """ This class represents Branches. The reference can be found here https://developer.github.com/v3/repos/branches """ def __repr__(self): return self.get__repr__({"name": self._name.value}) @property def commit(self): """ :type: :class:`github.Commit.Commit` """ return self._commit.value @property def name(self): """ :type: string """ return self._name.value @property def protected(self): """ :type: bool """ return self._protected.value @property def protection_url(self): """ :type: string """ return self._protection_url.value def _initAttributes(self): self._commit = github.GithubObject.NotSet self._name = github.GithubObject.NotSet self._protection_url = github.GithubObject.NotSet self._protected = github.GithubObject.NotSet def _useAttributes(self, attributes): if "commit" in attributes: # pragma no branch self._commit = self._makeClassAttribute(github.Commit.Commit, attributes["commit"]) if "name" in attributes: # pragma no branch self._name = self._makeStringAttribute(attributes["name"]) if "protection_url" in attributes: # pragma no branch self._protection_url = self._makeStringAttribute(attributes["protection_url"]) if "protected" in attributes: # pragma no branch self._protected = self._makeBoolAttribute(attributes["protected"]) def get_protection(self): """ :calls: `GET /repos/:owner/:repo/branches/:branch/protection `_ """ headers, data = self._requester.requestJsonAndCheck( "GET", self.protection_url ) return github.BranchProtection.BranchProtection(self._requester, headers, data, completed=True) def edit_protection(self, strict=github.GithubObject.NotSet, contexts=github.GithubObject.NotSet, enforce_admins=github.GithubObject.NotSet, dismissal_users=github.GithubObject.NotSet, dismissal_teams=github.GithubObject.NotSet, dismiss_stale_reviews=github.GithubObject.NotSet, require_code_owner_reviews=github.GithubObject.NotSet, user_push_restrictions=github.GithubObject.NotSet, team_push_restrictions=github.GithubObject.NotSet): """ :calls: `PUT /repos/:owner/:repo/branches/:branch/protection `_ :strict: bool :contexts: list of strings :enforce_admins: bool :dismissal_users: list of strings :dismissal_teams: list of strings :dismiss_stale_reviews: bool :require_code_owner_reviews: bool :user_push_restrictions: list of strings :team_push_restrictions: list of strings NOTE: The GitHub API groups strict and contexts together, both must be submitted. Take care to pass both as arguments even if only one is changing. Use edit_required_status_checks() to avoid this. """ assert strict is github.GithubObject.NotSet or isinstance(strict, bool), strict assert contexts is github.GithubObject.NotSet or all(isinstance(element, (str, unicode)) or isinstance(element, (str, unicode)) for element in contexts), contexts assert enforce_admins is github.GithubObject.NotSet or isinstance(enforce_admins, bool), enforce_admins assert dismissal_users is github.GithubObject.NotSet or all(isinstance(element, (str, unicode)) or isinstance(element, (str, unicode)) for element in dismissal_users), dismissal_users assert dismissal_teams is github.GithubObject.NotSet or all(isinstance(element, (str, unicode)) or isinstance(element, (str, unicode)) for element in dismissal_teams), dismissal_teams assert dismiss_stale_reviews is github.GithubObject.NotSet or isinstance(dismiss_stale_reviews, bool), dismiss_stale_reviews assert require_code_owner_reviews is github.GithubObject.NotSet or isinstance(require_code_owner_reviews, bool), require_code_owner_reviews post_parameters = {} if strict is not github.GithubObject.NotSet or contexts is not github.GithubObject.NotSet: if strict is github.GithubObject.NotSet: strict = False if contexts is github.GithubObject.NotSet: contexts = [] post_parameters["required_status_checks"] = {"strict": strict, "contexts": contexts} else: post_parameters["required_status_checks"] = None if enforce_admins is not github.GithubObject.NotSet: post_parameters["enforce_admins"] = enforce_admins else: post_parameters["enforce_admins"] = None if dismissal_users is not github.GithubObject.NotSet or dismissal_teams is not github.GithubObject.NotSet or dismiss_stale_reviews is not github.GithubObject.NotSet or require_code_owner_reviews is not github.GithubObject.NotSet: post_parameters["required_pull_request_reviews"] = {} if dismiss_stale_reviews is not github.GithubObject.NotSet: post_parameters["required_pull_request_reviews"]["dismiss_stale_reviews"] = dismiss_stale_reviews if require_code_owner_reviews is not github.GithubObject.NotSet: post_parameters["required_pull_request_reviews"]["require_code_owner_reviews"] = require_code_owner_reviews if dismissal_users is not github.GithubObject.NotSet: post_parameters["required_pull_request_reviews"]["dismissal_restrictions"] = {"users": dismissal_users} if dismissal_teams is not github.GithubObject.NotSet: if "dismissal_restrictions" not in post_parameters["required_pull_request_reviews"]: post_parameters["required_pull_request_reviews"]["dismissal_restrictions"] = {} post_parameters["required_pull_request_reviews"]["dismissal_restrictions"]["teams"] = dismissal_teams else: post_parameters["required_pull_request_reviews"] = None if user_push_restrictions is not github.GithubObject.NotSet or team_push_restrictions is not github.GithubObject.NotSet: if user_push_restrictions is github.GithubObject.NotSet: user_push_restrictions = [] if team_push_restrictions is github.GithubObject.NotSet: team_push_restrictions = [] post_parameters["restrictions"] = {"users": user_push_restrictions, "teams": team_push_restrictions} else: post_parameters["restrictions"] = None headers, data = self._requester.requestJsonAndCheck( "PUT", self.protection_url, input=post_parameters ) def remove_protection(self): """ :calls: `DELETE /repos/:owner/:repo/branches/:branch/protection `_ """ headers, data = self._requester.requestJsonAndCheck( "DELETE", self.protection_url, ) def get_required_status_checks(self): """ :calls: `GET /repos/:owner/:repo/branches/:branch/protection/required_status_checks `_ :rtype: :class:`github.RequiredStatusChecks.RequiredStatusChecks` """ headers, data = self._requester.requestJsonAndCheck( "GET", self.protection_url + "/required_status_checks" ) return github.RequiredStatusChecks.RequiredStatusChecks(self._requester, headers, data, completed=True) def edit_required_status_checks(self, strict=github.GithubObject.NotSet, contexts=github.GithubObject.NotSet): """ :calls: `PATCH /repos/:owner/:repo/branches/:branch/protection/required_status_checks `_ :strict: bool :contexts: list of strings """ assert strict is github.GithubObject.NotSet or isinstance(strict, bool), strict assert contexts is github.GithubObject.NotSet or all(isinstance(element, (str, unicode)) or isinstance(element, (str, unicode)) for element in contexts), contexts post_parameters = {} if strict is not github.GithubObject.NotSet: post_parameters["strict"] = strict if contexts is not github.GithubObject.NotSet: post_parameters["contexts"] = contexts headers, data = self._requester.requestJsonAndCheck( "PATCH", self.protection_url + "/required_status_checks", input=post_parameters ) def remove_required_status_checks(self): """ :calls: `DELETE /repos/:owner/:repo/branches/:branch/protection/required_status_checks `_ """ headers, data = self._requester.requestJsonAndCheck( "DELETE", self.protection_url + "/required_status_checks" ) def get_required_pull_request_reviews(self): """ :calls: `GET /repos/:owner/:repo/branches/:branch/protection/required_pull_request_reviews `_ :rtype: :class:`github.RequiredPullRequestReviews.RequiredPullRequestReviews` """ headers, data = self._requester.requestJsonAndCheck( "GET", self.protection_url + "/required_pull_request_reviews" ) return github.RequiredPullRequestReviews.RequiredPullRequestReviews(self._requester, headers, data, completed=True) def edit_required_pull_request_reviews(self, dismissal_users=github.GithubObject.NotSet, dismissal_teams=github.GithubObject.NotSet, dismiss_stale_reviews=github.GithubObject.NotSet, require_code_owner_reviews=github.GithubObject.NotSet): """ :calls: `PATCH /repos/:owner/:repo/branches/:branch/protection/required_pull_request_reviews `_ :dismissal_users: list of strings :dismissal_teams: list of strings :dismiss_stale_reviews: bool :require_code_owner_reviews: bool """ assert dismissal_users is github.GithubObject.NotSet or all(isinstance(element, (str, unicode)) or isinstance(element, (str, unicode)) for element in dismissal_users), dismissal_users assert dismissal_teams is github.GithubObject.NotSet or all(isinstance(element, (str, unicode)) or isinstance(element, (str, unicode)) for element in dismissal_teams), dismissal_teams assert dismiss_stale_reviews is github.GithubObject.NotSet or isinstance(dismiss_stale_reviews, bool), dismiss_stale_reviews assert require_code_owner_reviews is github.GithubObject.NotSet or isinstance(require_code_owner_reviews, bool), require_code_owner_reviews post_parameters = {} if dismissal_users is not github.GithubObject.NotSet: post_parameters["dismissal_restrictions"] = {"users": dismissal_users} if dismissal_teams is not github.GithubObject.NotSet: if "dismissal_restrictions" not in post_parameters: post_parameters["dismissal_restrictions"] = {} post_parameters["dismissal_restrictions"]["teams"] = dismissal_teams if dismiss_stale_reviews is not github.GithubObject.NotSet: post_parameters["dismiss_stale_reviews"] = dismiss_stale_reviews if require_code_owner_reviews is not github.GithubObject.NotSet: post_parameters["require_code_owner_reviews"] = require_code_owner_reviews headers, data = self._requester.requestJsonAndCheck( "PATCH", self.protection_url + "/required_pull_request_reviews", input=post_parameters ) def remove_required_pull_request_reviews(self): """ :calls: `DELETE /repos/:owner/:repo/branches/:branch/protection/required_pull_request_reviews `_ """ headers, data = self._requester.requestJsonAndCheck( "DELETE", self.protection_url + "/required_pull_request_reviews" ) def get_admin_enforcement(self): """ :calls: `GET /repos/:owner/:repo/branches/:branch/protection/enforce_admins `_ :rtype: bool """ headers, data = self._requester.requestJsonAndCheck( "GET", self.protection_url + "/enforce_admins" ) return data["enabled"] def set_admin_enforcement(self): """ :calls: `POST /repos/:owner/:repo/branches/:branch/protection/enforce_admins `_ """ headers, data = self._requester.requestJsonAndCheck( "POST", self.protection_url + "/enforce_admins" ) def remove_admin_enforcement(self): """ :calls: `DELETE /repos/:owner/:repo/branches/:branch/protection/enforce_admins `_ """ headers, data = self._requester.requestJsonAndCheck( "DELETE", self.protection_url + "/enforce_admins" ) def get_user_push_restrictions(self): """ :calls: `GET /repos/:owner/:repo/branches/:branch/protection/restrictions/users `_ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ return github.PaginatedList.PaginatedList( github.NamedUser.NamedUser, self._requester, self.protection_url + "/restrictions/users", None ) def get_team_push_restrictions(self): """ :calls: `GET /repos/:owner/:repo/branches/:branch/protection/restrictions/teams `_ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Team.Team` """ return github.PaginatedList.PaginatedList( github.Team.Team, self._requester, self.protection_url + "/restrictions/teams", None ) def edit_user_push_restrictions(self, *users): """ :calls: `POST /repos/:owner/:repo/branches/:branch/protection/restrictions `_ :users: list of strings """ assert all(isinstance(element, (str, unicode)) or isinstance(element, (str, unicode)) for element in users), users headers, data = self._requester.requestJsonAndCheck( "POST", self.protection_url + "/restrictions/users", input=users ) def edit_team_push_restrictions(self, *teams): """ :calls: `POST /repos/:owner/:repo/branches/:branch/protection/restrictions `_ :teams: list of strings """ assert all(isinstance(element, (str, unicode)) or isinstance(element, (str, unicode)) for element in teams), teams headers, data = self._requester.requestJsonAndCheck( "POST", self.protection_url + "/restrictions/teams", input=teams ) def remove_push_restrictions(self): """ :calls: `DELETE /repos/:owner/:repo/branches/:branch/protection/restrictions `_ """ headers, data = self._requester.requestJsonAndCheck( "DELETE", self.protection_url + "/restrictions" )