Add RepositoryBranchProtections

pull/42/head
Jerry Jacobs 2025-07-15 18:44:25 +02:00
rodzic 7e4ad3bb0d
commit 08d9fef5c0
1 zmienionych plików z 43 dodań i 3 usunięć

Wyświetl plik

@ -357,6 +357,32 @@ class Branch(ReadonlyApiObject):
def request(cls, gitea: "Gitea", owner: str, repo: str, ref: str):
return cls._request(gitea, {"owner": owner, "repo": repo, "ref": ref})
class RepositoryBranchProtections(ApiObject):
REPO_BRANCH_PROTECTIONS_BY_NAME = """/repos/{owner}/{repo}/branch_protections/{name}"""
_owner: str
_repo: str
_patchable_fields = {
"enable_push",
}
def __init__(self, gitea):
super().__init__(gitea)
def set(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def commit(self):
values = self.get_dirty_fields()
args = {
"owner": self._owner,
"repo": self._repo,
"name": self.rule_name
}
url = RepositoryBranchProtections.REPO_BRANCH_PROTECTIONS_BY_NAME.format(**args)
self.gitea.requests_patch(url, data=values)
self.dirty_fields = {}
class Repository(ApiObject):
API_OBJECT = """/repos/{owner}/{name}""" # <owner>, <reponame>
@ -376,6 +402,7 @@ class Repository(ApiObject):
REPO_COMMITS = "/repos/%s/%s/commits" # <owner>, <reponame>
REPO_TRANSFER = "/repos/{owner}/{repo}/transfer"
REPO_MILESTONES = """/repos/{owner}/{repo}/milestones"""
REPO_BRANCH_PROTECTIONS = """/repos/{owner}/{repo}/branch_protections"""
def __init__(self, gitea):
super().__init__(gitea)
@ -747,6 +774,19 @@ class Repository(ApiObject):
)
return Repository.parse_response(gitea, result)
def get_branch_protections(self) -> 'list[RepositoryBranchProtections]':
args = {"owner":self.owner.username, "repo": self.name}
url = Repository.REPO_BRANCH_PROTECTIONS.format(**args)
results = self.gitea.requests_get(url)
branch_protections = []
for result in results:
branch_protection = RepositoryBranchProtections.parse_response(self.gitea, result)
branch_protection._owner = args["owner"]
branch_protection._repo = args["repo"]
branch_protections.append(branch_protection)
return branch_protections
class UserRepoPermission(ReadonlyApiObject):
READ = "read"