Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion github_rest_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""GitHub REST APIs."""

from .github import Repository # noqa: F401
from .github import Organization, Repository, RepositoryType

__all__ = ["Organization", "Repository", "RepositoryType"]
22 changes: 15 additions & 7 deletions github_rest_api/github.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Simple wrapper of GitHub REST APIs."""

from enum import StrEnum
from typing import Any, Callable
from pathlib import Path
import requests
Expand Down Expand Up @@ -85,7 +86,7 @@ def __init__(self, token: str, owner: str, repo: str):
:param owner: The owner of the repository.
:param repo: The name of the repository.
"""
super(token)
super().__init__(token)
self._owner = owner
self._repo = repo
self._url_pull = f"https://api.github.com/repos/{owner}/{repo}/pulls"
Expand Down Expand Up @@ -274,36 +275,43 @@ def create_issue_comment(self, issue_number: int, body: str) -> dict[str, Any]:
).json()


class RepositoryType(StrEnum):
ALL = "all"
PUBLIC = "public"
PRIVATE = "private"


class Organization(GitHub):
def __init__(self, token: str, owner: str):
"""Initialize Repository.
:param token: An authorization token for GitHub REST APIs.
:param owner: The owner of the repository.
"""
super(token)
super().__init__(token)
self._owner = owner
self._url_repos = f"https://api.github.com/orgs/{owner}/repos"

def get_repositories(self, type_: str = "") -> list[dict[str, Any]]:
def get_repositories(
self, type_: RepositoryType = RepositoryType.ALL
) -> list[dict[str, Any]]:
"""Get all accessible repositories.

:param type_: Type of repositories (e.g., public).
"""
params = {
"type": type_,
"page": 1,
"per_page": 100,
}
if type_:
params["type"] = type_
repos = []
while True:
resp = self.get(url=self._url_repos, params=params)
resp.raise_for_status()
data = resp.json()
repos.extend(data)
if len(data) < params["per_page"]:
if len(data) < params["per_page"]: # ty: ignore[unsupported-operator]
return repos
params["page"] += 1
params["page"] += 1 # ty: ignore[unsupported-operator]
return repos

def instantiate_repository(self, repo: str) -> Repository:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "github_rest_api"
version = "0.26.0"
description = "Simple wrapper of GitHub REST APIs."
authors = [{ name = "Ben Du", email = "longendu@yahoo.com" }]
requires-python = ">=3.10,<4"
requires-python = ">=3.11,<4"
readme = "README.md"
dependencies = [
"requests>=2.28.2",
Expand Down
18 changes: 1 addition & 17 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.