forked from CodeBoarding/CodeBoarding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_app.py
More file actions
440 lines (368 loc) · 14.8 KB
/
local_app.py
File metadata and controls
440 lines (368 loc) · 14.8 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import dotenv
from demo import generate_docs_remote
dotenv.load_dotenv()
import logging
import os
import uuid
import asyncio
from pathlib import Path
from datetime import datetime
from enum import Enum
from urllib.parse import urlparse
from fastapi import FastAPI, HTTPException, Query, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from starlette.concurrency import run_in_threadpool
from duckdb_crud import fetch_job, init_db, insert_job, update_job, fetch_all_jobs
from github_action import generate_analysis
from repo_utils import RepoDontExistError
from utils import CFGGenerationError, create_temp_repo_folder, remove_temp_repo_folder
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class JobStatus(str, Enum):
PENDING = "PENDING"
RUNNING = "RUNNING"
COMPLETED = "COMPLETED"
FAILED = "FAILED"
# Environment variables
REPO_ROOT = os.getenv("REPO_ROOT")
if not REPO_ROOT:
logger.error("REPO_ROOT environment variable not set")
# FastAPI app
app = FastAPI(
title="Onboarding Diagram Generator",
description="Generate docs/diagrams for a GitHub repo",
version="1.0.0",
)
# CORS setup
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["Content-Type", "ngrok-skip-browser-warning", "User-Agent"],
allow_credentials=False,
)
# Job concurrency limit
MAX_CONCURRENT_JOBS = 5
job_semaphore = asyncio.Semaphore(MAX_CONCURRENT_JOBS)
app.add_event_handler("startup", init_db)
def extract_repo_name(repo_url: str) -> str:
parsed = urlparse(repo_url)
parts = parsed.path.strip('/').split('/')
if len(parts) >= 2:
name = parts[-1]
return name[:-4] if name.endswith('.git') else name
raise ValueError(f"Invalid GitHub URL: {repo_url}")
# -- Job Creation & Processing --
def make_job(repo_url: str) -> dict:
job_id = str(uuid.uuid4())
now = datetime.utcnow().isoformat()
return {
"id": job_id,
"repo_url": repo_url,
"status": JobStatus.PENDING,
"result": None,
"error": None,
"created_at": now,
"started_at": None,
"finished_at": None,
}
async def generate_onboarding(job_id: str):
update_job(job_id, status=JobStatus.RUNNING, started_at=datetime.utcnow())
try:
async with job_semaphore:
temp_repo_folder = create_temp_repo_folder()
try:
job = fetch_job(job_id)
if not job:
raise ValueError(f"Job {job_id} not found")
if not REPO_ROOT:
raise ValueError("REPO_ROOT environment variable not set")
# run generation
repo_name = extract_repo_name(job["repo_url"])
generated_repo_name = await run_in_threadpool(
generate_docs_remote,
repo_url=job["repo_url"],
temp_repo_folder=temp_repo_folder,
)
# Process the generated files from temp_repo_folder
docs_content = {}
# Files are generated in the temp_repo_folder, not the returned repo_name
analysis_files_json = list(temp_repo_folder.glob("*.json"))
analysis_files_md = list(temp_repo_folder.glob("*.md"))
logger.info(f"Found {len(analysis_files_json)} JSON files and {len(analysis_files_md)} MD files in {temp_repo_folder}")
for file in analysis_files_json:
try:
with open(file, 'r', encoding='utf-8') as f:
content = f.read().strip()
if content: # Only add non-empty files
docs_content[file.name] = content
logger.info(f"Added JSON file: {file.name}")
except Exception as e:
logger.warning(f"Failed to read JSON file {file}: {e}")
for file in analysis_files_md:
try:
with open(file, 'r', encoding='utf-8') as f:
content = f.read().strip()
if content: # Only add non-empty files
docs_content[file.name] = content
logger.info(f"Added MD file: {file.name}")
except Exception as e:
logger.warning(f"Failed to read MD file {file}: {e}")
if not docs_content:
logger.warning("No documentation files generated for: %s", job["repo_url"])
update_job(job_id, status=JobStatus.FAILED, error="No documentation files were generated")
return
# Store result as JSON string in the result field
import json
result = json.dumps({"files": docs_content})
update_job(job_id, result=result, status=JobStatus.COMPLETED)
logger.info("Successfully generated %d doc files for %s (job: %s)", len(docs_content), job["repo_url"], job_id)
except RepoDontExistError:
url = job.get("repo_url", "unknown") if job else "unknown"
update_job(job_id, error=f"Repository not found: {url}", status=JobStatus.FAILED)
except CFGGenerationError:
update_job(job_id, error="Failed to generate diagram.", status=JobStatus.FAILED)
except Exception as e:
update_job(job_id, error=f"Server error: {e}", status=JobStatus.FAILED)
finally:
remove_temp_repo_folder(str(temp_repo_folder))
finally:
update_job(job_id, finished_at=datetime.utcnow())
# -- API Endpoints --
@app.post(
"/generation",
response_class=JSONResponse,
summary="Create a new onboarding job"
)
async def start_generation_job(
repo_url: str = Query(..., description="GitHub repo URL"),
background_tasks: BackgroundTasks = None
):
if not repo_url:
raise HTTPException(400, detail="repo_url is required")
job = make_job(repo_url)
insert_job(job)
if background_tasks:
background_tasks.add_task(generate_onboarding, job["id"])
else:
asyncio.create_task(generate_onboarding(job["id"]))
return {"job_id": job["id"], "status": job["status"]}
@app.get(
"/heart_beat",
response_class=JSONResponse,
summary="Get heart beat"
)
async def get_heart_beat():
return JSONResponse({"status": "ok"}, status_code=200)
@app.get(
"/generation/{job_id}",
response_class=JSONResponse,
summary="Get job status/result"
)
async def get_job(job_id: str):
job = fetch_job(job_id)
if not job:
raise HTTPException(404, detail="Job not found")
response_data = {
"job_id": job["id"],
"status": job["status"],
"created_at": job["created_at"],
"started_at": job["started_at"],
"finished_at": job["finished_at"],
"repo_url": job["repo_url"]
}
if job["status"] == JobStatus.COMPLETED:
if job.get("result"):
# Check if result is a JSON string containing files
try:
import json
result_data = json.loads(job["result"])
if "files" in result_data:
response_data["files"] = result_data["files"]
else:
response_data["result"] = job["result"]
except (json.JSONDecodeError, TypeError):
response_data["result"] = job["result"]
else:
response_data["result"] = {"message": "Job completed but no result available"}
elif job["status"] == JobStatus.FAILED:
if job.get("error"):
response_data["error"] = job["error"]
else:
response_data["error"] = "Job failed with unknown error"
return JSONResponse(content=response_data)
class DocsGenerationRequest(BaseModel):
url: str
source_branch: str = "main"
target_branch: str = "main"
extension: str = ".md"
output_directory: str = ".codeboarding"
@app.post(
"/github_action/jobs",
response_class=JSONResponse,
summary="Start a job to generate onboarding docs for a GitHub repo",
responses={
202: {"description": "Job created successfully, returns job ID"},
400: {"description": "Invalid request parameters"},
},
)
async def start_docs_generation_job(
background_tasks: BackgroundTasks,
docs_request: DocsGenerationRequest
):
"""
Start a background job to generate onboarding documentation.
Example:
POST /github_action/jobs?url=https://github.com/your/repo
Returns:
JSON object with job_id that can be used to check status
"""
logger.info("Received request to start docs generation job for %s", docs_request.url)
if docs_request.extension not in [".md", ".rst", ".html", ".mdx"]:
logger.warning("Unsupported extension provided: %s. Defaulting to markdown", docs_request.extension)
docs_request.extension = ".md" # Default to markdown if unsupported extension is provided
# Create job entry using duckdb
job = make_job(docs_request.url)
insert_job(job)
# Start background task
background_tasks.add_task(
process_docs_generation_job,
job["id"],
docs_request.url,
docs_request.source_branch,
docs_request.target_branch,
docs_request.output_directory,
docs_request.extension,
)
logger.info("Created job %s for %s", job["id"], docs_request.url)
return JSONResponse(
status_code=202,
content={
"job_id": job["id"],
"message": "Job created successfully. Use the job_id to check status."
}
)
@app.get(
"/github_action/jobs/{job_id}",
response_class=JSONResponse,
summary="Check the status of a documentation generation job",
responses={
200: {"description": "Returns job status and result if completed"},
404: {"description": "Job not found"},
},
)
async def get_github_action_status(job_id: str):
"""
Check the status of a documentation generation job.
Example:
GET /github_action/jobs/{job_id}
Returns:
JSON object with job status, and result if completed
"""
job = fetch_job(job_id)
if not job:
logger.warning("Job not found: %s", job_id)
raise HTTPException(404, detail="Job not found")
response_data = {
"job_id": job["id"],
"status": job["status"],
"created_at": job["created_at"],
"started_at": job["started_at"],
"finished_at": job["finished_at"],
"repo_url": job["repo_url"]
}
if job["status"] == JobStatus.COMPLETED:
if job.get("result"):
response_data["result"] = job["result"]
else:
response_data["result"] = {"message": "Job completed but no result available"}
elif job["status"] == JobStatus.FAILED:
if job.get("error"):
response_data["error"] = job["error"]
else:
response_data["error"] = "Job failed with unknown error"
return JSONResponse(content=response_data)
@app.get(
"/github_action/jobs",
response_class=JSONResponse,
summary="List all jobs",
responses={
200: {"description": "Returns list of all jobs"},
},
)
async def list_jobs():
"""
List all documentation generation jobs.
Returns:
JSON object with list of all jobs
"""
jobs_list = []
all_jobs = fetch_all_jobs()
for job in all_jobs:
job_summary = {
"job_id": job["id"],
"status": job["status"],
"created_at": job["created_at"],
"started_at": job["started_at"],
"finished_at": job["finished_at"],
"repo_url": job["repo_url"]
}
jobs_list.append(job_summary)
return JSONResponse(content={"jobs": jobs_list})
async def process_docs_generation_job(job_id: str, url: str, source_branch: str, target_branch: str, output_dir: str,
extension: str):
"""Background task to process documentation generation"""
update_job(job_id, status=JobStatus.RUNNING, started_at=datetime.utcnow())
temp_repo_folder = create_temp_repo_folder()
try:
# Ensure the URL starts with the correct prefix
if not url.startswith("https://github.com/"):
url = "https://github.com/" + url
# generate the docs
files_dir = await run_in_threadpool(
generate_analysis,
repo_url=url,
source_branch=source_branch,
target_branch=target_branch,
extension=extension,
output_dir=output_dir,
)
# Process the generated files
docs_content = {}
analysis_files_json = list(Path(files_dir).glob("*.json"))
analysis_files_extension = list(Path(files_dir).glob(f"*{extension}"))
for file in analysis_files_json:
with open(file, 'r') as f:
fname = file.stem
docs_content[f"{fname}.json"] = f.read().strip()
for file in analysis_files_extension:
with open(file, 'r') as f:
fname = file.stem
docs_content[f"{fname}{extension}"] = f.read().strip()
if not docs_content:
logger.warning("No documentation files generated for: %s", url)
update_job(job_id, status=JobStatus.FAILED, error="No documentation files were generated",
finished_at=datetime.utcnow())
return
# Store result as JSON string in the result field
import json
result = json.dumps({"files": docs_content})
update_job(job_id, status=JobStatus.COMPLETED, result=result, finished_at=datetime.utcnow())
logger.info("Successfully generated %d doc files for %s (job: %s)", len(docs_content), url, job_id)
except RepoDontExistError as e:
logger.warning("Repo not found or clone failed: %s (job: %s)", url, job_id)
update_job(job_id, status=JobStatus.FAILED, error=f"Repository not found or failed to clone: {url}",
finished_at=datetime.utcnow())
except CFGGenerationError as e:
logger.warning("CFG generation error for: %s (job: %s)", url, job_id)
update_job(job_id, status=JobStatus.FAILED, error="Failed to generate diagram. We will look into it 🙂",
finished_at=datetime.utcnow())
except Exception as e:
logger.exception("Unexpected error processing repo %s (job: %s)", url, job_id)
update_job(job_id, status=JobStatus.FAILED, error="Internal server error", finished_at=datetime.utcnow())
finally:
# cleanup temp folder for this run
remove_temp_repo_folder(temp_repo_folder)