-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (81 loc) · 3.44 KB
/
main.py
File metadata and controls
103 lines (81 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#setup logging before importing other modules to ensure all logs are captured
from utils.logging_config import setup_logging
setup_logging()
# load libraies and modules
import os
import logging
from fastapi import FastAPI, Header, HTTPException, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from typing import Optional
from utils.clone_repo import clone_repository_impl, CloneError
from github_summary import summarize_github_repo # your summarizer
import shutil
import logging
logger = logging.getLogger(__name__)
## FastAPI app
app = FastAPI(title="GitHub Repo Summarizer")
class SummarizeRequest(BaseModel):
github_url: str
def _extract_token_from_headers(authorization: Optional[str], x_git_token: Optional[str]) -> Optional[str]:
"""
Accept either:
- Authorization: Bearer <token>
- X-Git-Token: <token>
Returns token or None.
"""
if x_git_token:
return x_git_token.strip()
if authorization:
parts = authorization.split()
if len(parts) == 2 and parts[0].lower() == "bearer":
return parts[1].strip()
return None
@app.post("/summarize")
def summarize_repo(request: SummarizeRequest, Authorization: Optional[str] = Header(None), X_Git_Token: Optional[str] = Header(None)):
token = _extract_token_from_headers(Authorization, X_Git_Token)
logger.debug("Summarize called for %s (token provided: %s)", request.github_url, bool(token))
temp_path = None
try:
# clone repository
temp_path, metadata = clone_repository_impl(request.github_url, token=token, depth=1)
# Call your summarizer
result = summarize_github_repo(temp_path, metadata, max_iterations=5)
# cleanup the cloned repo now that summarization is done
try:
if temp_path and os.path.exists(temp_path):
shutil.rmtree(temp_path, ignore_errors=True)
except Exception as e:
logger.warning("Failed to remove temp dir %s: %s", temp_path, e)
# Normalize result shape
return {
"summary": result.get("summary"),
"technologies": result.get("technologies"),
"structure": result.get("structure")
}
except CloneError as ce:
# Return clear JSON for expected clone issues
logger.info("CloneError: %s (%s) detail=%s", ce.message, ce.code, ce.detail)
return JSONResponse(status_code=ce.status_code, content={
"status": "error",
"error_code": ce.code,
"message": ce.message,
"detail": ce.detail if ce.status_code >= 500 else None
})
except Exception as e:
# Catch-all for summarizer errors or unexpected issues
logger.exception("Unexpected error during summarization: %s", e)
# ensure cleanup
if temp_path and os.path.exists(temp_path):
try:
shutil.rmtree(temp_path, ignore_errors=True)
except Exception:
pass
return JSONResponse(status_code=500, content={
"status": "error",
"error_code": "internal_error",
"message": "An internal server error occurred while summarizing the repository."
})
@app.get("/")
def root():
return {"message": "GitHub Repo Summarizer API. Use POST /summarize with JSON body {\"github_url\": \"https://github.com/owner/repo\"} and optionally Authorization: Bearer <token> or X-Git-Token header for private repos."}