-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (78 loc) · 3.48 KB
/
Copy pathmain.py
File metadata and controls
92 lines (78 loc) · 3.48 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
from fastapi import FastAPI, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
from .models.schemas import RepositoryAnalysisRequest, RepositoryAnalysisResponse, RepositoryInfoResponse
from .services.github import GitHubService
from .services.agent import AzureAgentService
from .config import CORS_ORIGINS
app = FastAPI(title="AGUnblock Backend", description="Backend API for AGUnblock")
app.add_middleware(
CORSMiddleware,
allow_origins=CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def get_github_service():
return GitHubService()
def get_agent_service():
return AzureAgentService()
@app.get("/")
async def root():
return {"message": "AGUnblock Backend API"}
@app.post("/api/analyze", response_model=RepositoryAnalysisResponse)
async def analyze_repository(
request: RepositoryAnalysisRequest,
github_service: GitHubService = Depends(get_github_service),
agent_service: AzureAgentService = Depends(get_agent_service)
):
try:
print(f"Analyzing repository: {request.owner}/{request.repo} with agent: {request.agent_id}")
repo_info = await github_service.get_repository_info(request.owner, request.repo)
if not repo_info:
print(f"Repository not found: {request.owner}/{request.repo}")
repo_info = {"name": request.repo, "full_name": f"{request.owner}/{request.repo}"}
readme_content = await github_service.get_readme_content(request.owner, request.repo)
print(f"README content found: {readme_content is not None}")
dependencies = await github_service.get_requirements(request.owner, request.repo)
print(f"Dependencies found: {len(dependencies)}")
analysis = await agent_service.analyze_repository(
request.agent_id,
f"{request.owner}/{request.repo}",
readme_content or "No README found", # Provide default if None
dependencies or {} # Provide empty dict if None
)
print(f"Analysis result length: {len(analysis)}")
return RepositoryAnalysisResponse(
agent_id=request.agent_id,
repo_name=f"{request.owner}/{request.repo}",
analysis=analysis
)
except Exception as e:
print(f"Error analyzing repository: {str(e)}")
return RepositoryAnalysisResponse(
agent_id=request.agent_id,
repo_name=f"{request.owner}/{request.repo}",
analysis=f"Error analyzing repository: {str(e)}",
error=str(e)
)
@app.get("/api/repo-info/{owner}/{repo}", response_model=RepositoryInfoResponse)
async def get_repository_info(
owner: str,
repo: str,
github_service: GitHubService = Depends(get_github_service)
):
try:
print(f"Fetching repository data for {owner}/{repo}...")
repo_data = await github_service.get_repository_snapshot(owner, repo)
if not repo_data:
print(f"Repository not found: {owner}/{repo}")
raise HTTPException(status_code=404, detail="Repository not found")
return RepositoryInfoResponse(**repo_data)
except RuntimeError as e:
error_msg = f"Error fetching repository info: {str(e)}"
print(error_msg)
raise HTTPException(status_code=500, detail=error_msg)
except Exception as e:
error_msg = f"Unexpected error: {str(e)}"
print(error_msg)
raise HTTPException(status_code=500, detail=error_msg)