-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server.py
More file actions
195 lines (151 loc) · 5.52 KB
/
api_server.py
File metadata and controls
195 lines (151 loc) · 5.52 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from fastapi import FastAPI, HTTPException, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from pydantic import BaseModel
from typing import Optional, List, Dict
import os
import traceback
from datetime import datetime
from pathlib import Path
from agent_core import CodeSentinelAgent
from github_client import GitHubClient
from pr_scorer import PRScorer
from storage import StorageManager
from config import Config
app = FastAPI(title="CodeSentinel API", version="1.0.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
agent = CodeSentinelAgent()
github_client = GitHubClient()
pr_scorer = PRScorer()
storage = StorageManager()
class IssueToPRRequest(BaseModel):
repo_url: str
issue_text: str
base_branch: str = "main"
max_files: int = 10
max_lines: int = 500
class PRRankingRequest(BaseModel):
repo_url: str
class ConfigResponse(BaseModel):
has_gemini_key: bool
has_github_token: bool
max_files: int
max_lines: int
whitelist_enabled: bool
whitelisted_repos: int
@app.get("/api/health")
async def health_check():
return {"status": "ok", "timestamp": datetime.now().isoformat()}
@app.get("/api/config")
async def get_config() -> ConfigResponse:
summary = Config.get_summary()
return ConfigResponse(**summary)
@app.get("/api/stats")
async def get_stats():
recent_runs = storage.get_recent_runs(1000)
completed = sum(1 for r in recent_runs if r.get('status') == 'completed')
failed = sum(1 for r in recent_runs if r.get('status') == 'failed')
return {
"total_runs": len(recent_runs),
"completed_runs": completed,
"failed_runs": failed,
"success_rate": (completed / len(recent_runs) * 100) if recent_runs else 0
}
@app.post("/api/issue-to-pr")
async def create_pr_from_issue(request: IssueToPRRequest, background_tasks: BackgroundTasks):
config_issues = Config.validate_config()
if config_issues:
raise HTTPException(status_code=400, detail={"errors": config_issues})
try:
result = agent.process_issue_to_pr(
repo_url=request.repo_url,
issue_text=request.issue_text,
base_branch=request.base_branch,
max_files=request.max_files,
max_lines=request.max_lines
)
return result
except Exception as e:
raise HTTPException(
status_code=500,
detail={
"error": str(e),
"traceback": traceback.format_exc()
}
)
@app.post("/api/pr-ranking")
async def rank_pull_requests(request: PRRankingRequest):
config_issues = Config.validate_config()
if config_issues:
raise HTTPException(status_code=400, detail={"errors": config_issues})
try:
repo_full_name = github_client.parse_repo_url(request.repo_url)
pr_list = github_client.get_open_pull_requests(repo_full_name)
if not pr_list:
return {
"repo": repo_full_name,
"total_prs": 0,
"analyzed_prs": 0,
"rankings": [],
"timestamp": datetime.now().isoformat()
}
diff_contents = {}
for pr in pr_list[:Config.PR_FETCH_LIMIT]:
diff = github_client.get_pr_diff(repo_full_name, pr['number'])
diff_contents[pr['number']] = diff
ranked_prs = pr_scorer.rank_prs(pr_list[:Config.PR_FETCH_LIMIT], diff_contents)
ranking_data = {
"repo": repo_full_name,
"timestamp": datetime.now().isoformat(),
"total_prs": len(pr_list),
"analyzed_prs": len(ranked_prs),
"rankings": ranked_prs,
"top_recommendations": pr_scorer.get_top_recommendations(ranked_prs, Config.TOP_RECOMMENDATIONS)
}
storage.save_pr_ranking(repo_full_name, ranking_data)
return ranking_data
except Exception as e:
raise HTTPException(
status_code=500,
detail={
"error": str(e),
"traceback": traceback.format_exc()
}
)
@app.get("/api/runs")
async def get_runs(limit: int = 20):
runs = storage.get_recent_runs(limit)
return {"runs": runs}
@app.get("/api/runs/{run_id}")
async def get_run_details(run_id: str):
details = storage.load_run(run_id)
if not details:
raise HTTPException(status_code=404, detail="Run not found")
return details
@app.get("/api/repos")
async def list_repos():
return {
"whitelisted_repos": Config.WHITELISTED_REPOS if Config.WHITELISTED_REPOS else [],
"whitelist_enabled": len(Config.WHITELISTED_REPOS) > 0 if Config.WHITELISTED_REPOS else False
}
build_dir = Path(__file__).parent / "client" / "dist"
if build_dir.exists():
app.mount("/assets", StaticFiles(directory=str(build_dir / "assets")), name="assets")
@app.get("/{full_path:path}")
async def serve_react_app(full_path: str):
if full_path.startswith("api/"):
raise HTTPException(status_code=404)
file_path = build_dir / full_path
if file_path.exists() and file_path.is_file():
return FileResponse(file_path)
return FileResponse(build_dir / "index.html")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)