Skip to content

Commit

Permalink
Add: Add async banches GitHub API
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Oct 25, 2022
1 parent 27bc11a commit 17e906f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pontos/github/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
GitHubAsyncRESTArtifacts,
GitHubRESTArtifactsMixin,
)
from pontos.github.api.branch import GitHubRESTBranchMixin
from pontos.github.api.branch import (
GitHubAsyncRESTBranches,
GitHubRESTBranchMixin,
)
from pontos.github.api.client import GitHubAsyncRESTClient
from pontos.github.api.contents import GitHubRESTContentMixin
from pontos.github.api.helper import (
Expand Down Expand Up @@ -79,6 +82,14 @@ def artifacts(self):
"""
return GitHubAsyncRESTArtifacts(self._client)

@property
def branches(self):
"""
Branches related API
"""
return GitHubAsyncRESTBranches(self._client)

async def __aenter__(self) -> "GitHubAsyncRESTApi":
await self._client.__aenter__()
return self
Expand Down
48 changes: 48 additions & 0 deletions pontos/github/api/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,54 @@

import httpx

from pontos.github.api.client import GitHubAsyncREST
from pontos.github.api.helper import JSON


class GitHubAsyncRESTBranches(GitHubAsyncREST):
async def exists(self, repo: str, branch: str) -> bool:
"""
Check if a single branch in a repository exists
Args:
repo: GitHub repository (owner/name) to use
branch: Branch name to check
"""
api = f"/repos/{repo}/branches/{branch}"
response = await self._client.get(api)
return response.is_success

async def delete(self, repo: str, branch: str):
"""
Delete a branch on GitHub
Args:
repo: GitHub repository (owner/name) to use
branch: Branch to be deleted
Raises:
HTTPError if the request was invalid
"""
api = f"/repos/{repo}/git/refs/{branch}"
response = await self._client.delete(api)
response.raise_for_status()

async def protection_rules(self, repo: str, branch: str) -> JSON:
"""
Get branch protection rules for a specific repository
branch
Args:
repo: GitHub repository (owner/name) to use
branch: Get protection rules for this branch
Raises:
HTTPError if the request was invalid
"""
api = f"/repos/{repo}/branches/{branch}/protection"
response = await self._client.get(api)
return response.json()


class GitHubRESTBranchMixin:
def branch_exists(self, repo: str, branch: str) -> bool:
Expand Down

0 comments on commit 17e906f

Please sign in to comment.