Skip to content

Commit

Permalink
chore: prep for create_pull_request functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Feb 22, 2025
1 parent c79ddb3 commit 6f9684d
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/githarbor/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ def list_tags(self) -> list[Tag]:
msg = f"{self.__class__.__name__} does not implement list_tags"
raise FeatureNotSupportedError(msg)

def create_pull_request(
self,
title: str,
body: str,
head_branch: str,
base_branch: str,
draft: bool = False,
) -> PullRequest:
msg = f"{self.__class__.__name__} does not implement create_pull_request"
raise FeatureNotSupportedError(msg)

async def get_repo_user_async(self) -> User:
"""Get repository owner information asynchronously."""
msg = f"{self.__class__.__name__} does not implement get_repo_user_async"
Expand Down Expand Up @@ -348,6 +359,17 @@ async def list_tags_async(self) -> list[Tag]:
msg = f"{self.__class__.__name__} does not implement list_tags_async"
raise FeatureNotSupportedError(msg)

async def create_pull_request_async(
self,
title: str,
body: str,
head_branch: str,
base_branch: str,
draft: bool = False,
) -> PullRequest:
msg = f"{self.__class__.__name__} does not implement create_pull_request_async"
raise FeatureNotSupportedError(msg)


class BaseOwner:
"""Base class for repository owners."""
Expand Down
66 changes: 66 additions & 0 deletions src/githarbor/core/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,44 @@ def get_tag(self, name: str) -> Tag:
return asyncio.run(self._repository.get_tag_async(name))
return self._repository.get_tag(name)

def create_pull_request(
self,
title: str,
body: str,
head_branch: str,
base_branch: str,
draft: bool = False,
) -> PullRequest:
"""Create a new pull request.
Args:
title: Pull request title
body: Pull request description
head_branch: Source branch containing the changes
base_branch: Target branch for the changes
draft: Whether to create a draft pull request
Returns:
Newly created pull request
"""
if self._repository.is_async:
return asyncio.run(
self._repository.create_pull_request_async(
title=title,
body=body,
head_branch=head_branch,
base_branch=base_branch,
draft=draft,
)
)
return self._repository.create_pull_request(
title=title,
body=body,
head_branch=head_branch,
base_branch=base_branch,
draft=draft,
)

def list_tags(self) -> list[Tag]:
"""List all tags.
Expand Down Expand Up @@ -797,6 +835,32 @@ async def get_tag_async(self, name: str) -> Tag:
return await self._repository.get_tag_async(name) # type: ignore
return await asyncio.to_thread(self._repository.get_tag, name)

async def create_pull_request_async(
self,
title: str,
body: str,
head_branch: str,
base_branch: str,
draft: bool = False,
) -> PullRequest:
"""See create_pull_request."""
if self._repository.is_async:
return await self._repository.create_pull_request_async(
title=title,
body=body,
head_branch=head_branch,
base_branch=base_branch,
draft=draft,
)
return await asyncio.to_thread(
self._repository.create_pull_request,
title=title,
body=body,
head_branch=head_branch,
base_branch=base_branch,
draft=draft,
)

async def list_tags_async(self) -> list[Tag]:
"""See list_tags."""
if self._repository.is_async:
Expand All @@ -808,6 +872,7 @@ def get_sync_methods(self) -> list[Callable]:
return [
self.get_repo_user,
self.get_branch,
self.create_pull_request,
self.get_pull_request,
self.list_pull_requests,
self.get_issue,
Expand All @@ -834,6 +899,7 @@ def get_async_methods(self) -> list[Callable]:
return [
self.get_repo_user_async,
self.get_branch_async,
self.create_pull_request_async,
self.get_pull_request_async,
self.list_pull_requests_async,
self.get_issue_async,
Expand Down
32 changes: 32 additions & 0 deletions src/githarbor/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,37 @@ async def delete_repository_async(url: str, name: str) -> None:
await owner.delete_repository_async(name)


async def create_pull_request_async(
url: str,
title: str,
body: str,
head_branch: str,
base_branch: str,
draft: bool = False,
) -> PullRequest:
"""Create a new pull request.
Args:
url: Repository URL
title: Pull request title
body: Pull request description
head_branch: Source branch containing the changes
base_branch: Target branch for the changes
draft: Whether to create a draft pull request
Returns:
Newly created pull request
"""
repo = RepoRegistry.get(url)
return await repo.create_pull_request_async(
title=title,
body=body,
head_branch=head_branch,
base_branch=base_branch,
draft=draft,
)


get_repo_user = make_sync(get_repo_user_async)
get_branch = make_sync(get_branch_async)
get_pull_request = make_sync(get_pull_request_async)
Expand All @@ -411,6 +442,7 @@ async def delete_repository_async(url: str, name: str) -> None:
get_release = make_sync(get_release_async)
get_tag = make_sync(get_tag_async)
list_tags = make_sync(list_tags_async)
create_pull_request = make_sync(create_pull_request_async)
list_repositories = make_sync(list_repositories_async)
create_repository = make_sync(create_repository_async)
get_user = make_sync(get_user_async)
Expand Down

0 comments on commit 6f9684d

Please sign in to comment.