diff --git a/apps/misc/urls.py b/apps/misc/urls.py new file mode 100644 index 0000000..02bd99f --- /dev/null +++ b/apps/misc/urls.py @@ -0,0 +1,9 @@ +from django.urls import path + +from .views import recent_commits + +app_name = "misc" + +urlpatterns = [ + path("github//commits/", recent_commits, name="github-recent-commits"), +] \ No newline at end of file diff --git a/apps/misc/views.py b/apps/misc/views.py new file mode 100644 index 0000000..d3e1645 --- /dev/null +++ b/apps/misc/views.py @@ -0,0 +1,46 @@ +import os + +from django.http import JsonResponse +from django.views.decorators.http import require_GET + +try: + # octokitpy client + from octokit import Octokit +except ImportError: # pragma: no cover + Octokit = None # type: ignore + + +@require_GET +def recent_commits(request, username: str): + """ + Return metadata for a user's most recent commits (up to 100) across GitHub. + Optional query params: + - per_page: number of commits to return (default 100, max 100) + """ + if Octokit is None: + return JsonResponse({"error": "octokitpy is not installed"}, status=500) + + per_page = min(int(request.GET.get("per_page", 100)), 100) + token = os.environ.get("GITHUB_TOKEN") + + # Initialize Octokit client (authenticated if token provided to raise rate limits) + if token: + client = Octokit(type="token", token=token) + else: + client = Octokit() + + # Use the GitHub Search API for commits + # Query: search by author username, sorted by author date desc + # Note: Commit Search API historically required a preview accept header; octokitpy handles headers internally. + res = client.search.commits( + q=f"author:{username}", + sort="author-date", + order="desc", + per_page=per_page, + ) + + # Return the raw payload so the caller gets "all the metadata" + data = res.json # type: ignore[attr-defined] + # Keep it simple: forward the result from GitHub. Optionally, we could subset fields here. + + return JsonResponse(data, safe=False) \ No newline at end of file diff --git a/conf/urls.py b/conf/urls.py index b2ddfbf..39b01c6 100644 --- a/conf/urls.py +++ b/conf/urls.py @@ -6,6 +6,7 @@ urlpatterns = [ path("", include("apps.users.urls.auth")), + path("api/", include("apps.misc.urls")), path("admin/", admin.site.urls), ] diff --git a/pyproject.toml b/pyproject.toml index ccbc7ff..2a6be35 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ psycopg2-binary = "^2.8.4" python = "^3.6" redis = "^3.3.11" sentry-sdk = "^0.14.1" +octokitpy = "^0.3.0" [tool.poetry.dev-dependencies] Werkzeug = "^0.16.0"