Skip to content

GitHubOwner

Base classes

Name Children Inherits
BaseOwner
githarbor.core.base
Base class for repository owners.

â‹” Inheritance diagram

graph TD
  782876596["owner.GitHubOwner"]
  697273899["base.BaseOwner"]
  2751680898["builtins.object"]
  697273899 --> 782876596
  2751680898 --> 697273899

🛈 DocStrings

Bases: BaseOwner

Source code in src/githarbor/providers/github_provider/owner.py
class GitHubOwner(BaseOwner):
    url_patterns: ClassVar = ["github.com"]
    is_async = False

    def __init__(self, username: str, token: str | None = None):
        from github import Auth, Github, GithubException

        try:
            t = token or TOKEN
            if t is None:
                logger.info("No GitHub token provided. Stricter rate limit.")
            auth = Auth.Token(t) if t else None
            self._gh = Github(auth=auth)
            self._name = username
            self._user = self._gh.get_user(username)
        except GithubException as e:
            msg = f"GitHub authentication failed: {e!s}"
            raise AuthenticationError(msg) from e

    @property
    def name(self) -> str:
        """The name of the owner."""
        return self._name

    @classmethod
    def from_url(cls, url: str, **kwargs: Any) -> GitHubOwner:
        # Handle URLs like "https://github.com/phil65"
        parsed = urlparse(url)
        parts = parsed.path.strip("/").split("/")
        return cls(username=parts[0], token=kwargs.get("token"))

    @githubtools.handle_github_errors("Failed to list repositories")
    def list_repositories(self) -> list[BaseRepository]:
        """List all repositories owned by this user."""
        from githarbor.repositories import create_repository

        return [create_repository(repo.html_url) for repo in self._user.get_repos()]

    @githubtools.handle_github_errors("Failed to create repository {name}")
    def create_repository(
        self,
        name: str,
        description: str = "",
        private: bool = False,
    ) -> BaseRepository:
        """Create a new repository."""
        from github.AuthenticatedUser import AuthenticatedUser

        from githarbor.repositories import create_repository

        auth_user = self._gh.get_user()
        assert isinstance(auth_user, AuthenticatedUser)
        repo = auth_user.create_repo(name=name, description=description, private=private)
        return create_repository(repo.html_url)

    @githubtools.handle_github_errors("Failed to get user information")
    def get_user(self) -> User:
        """Get user information."""
        return githubtools.create_user_model(self._user)

    @githubtools.handle_github_errors("Failed to delete repository {name}")
    def delete_repository(self, name: str) -> None:
        """Delete a repository."""
        from github import GithubException

        try:
            repo = self._gh.get_repo(f"{self._user.login}/{name}")
            repo.delete()
        except GithubException as e:
            if e.status == HTML_ERROR_CODE:
                msg = f"Repository {name} not found"
                raise ResourceNotFoundError(msg) from e
            raise

name property

name: str

The name of the owner.

create_repository

create_repository(name: str, description: str = '', private: bool = False) -> BaseRepository

Create a new repository.

Source code in src/githarbor/providers/github_provider/owner.py
@githubtools.handle_github_errors("Failed to create repository {name}")
def create_repository(
    self,
    name: str,
    description: str = "",
    private: bool = False,
) -> BaseRepository:
    """Create a new repository."""
    from github.AuthenticatedUser import AuthenticatedUser

    from githarbor.repositories import create_repository

    auth_user = self._gh.get_user()
    assert isinstance(auth_user, AuthenticatedUser)
    repo = auth_user.create_repo(name=name, description=description, private=private)
    return create_repository(repo.html_url)

delete_repository

delete_repository(name: str) -> None

Delete a repository.

Source code in src/githarbor/providers/github_provider/owner.py
@githubtools.handle_github_errors("Failed to delete repository {name}")
def delete_repository(self, name: str) -> None:
    """Delete a repository."""
    from github import GithubException

    try:
        repo = self._gh.get_repo(f"{self._user.login}/{name}")
        repo.delete()
    except GithubException as e:
        if e.status == HTML_ERROR_CODE:
            msg = f"Repository {name} not found"
            raise ResourceNotFoundError(msg) from e
        raise

get_user

get_user() -> User

Get user information.

Source code in src/githarbor/providers/github_provider/owner.py
@githubtools.handle_github_errors("Failed to get user information")
def get_user(self) -> User:
    """Get user information."""
    return githubtools.create_user_model(self._user)

list_repositories

list_repositories() -> list[BaseRepository]

List all repositories owned by this user.

Source code in src/githarbor/providers/github_provider/owner.py
@githubtools.handle_github_errors("Failed to list repositories")
def list_repositories(self) -> list[BaseRepository]:
    """List all repositories owned by this user."""
    from githarbor.repositories import create_repository

    return [create_repository(repo.html_url) for repo in self._user.get_repos()]

:fa-brands-github: Show source on GitHub