forked from repowise-dev/repowise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepos.py
More file actions
285 lines (240 loc) · 9.7 KB
/
repos.py
File metadata and controls
285 lines (240 loc) · 9.7 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
"""/api/repos — Repository CRUD + sync endpoints."""
from __future__ import annotations
import asyncio
import io
import logging
import zipfile
from pathlib import PurePosixPath
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import StreamingResponse
from repowise.core.persistence import crud
from repowise.core.persistence.models import (
DeadCodeFinding,
GenerationJob,
GraphNode,
Page,
Repository,
)
from repowise.server.deps import get_db_session, verify_api_key
from repowise.server.job_executor import execute_job
from repowise.server.schemas import RepoCreate, RepoResponse, RepoStatsResponse, RepoUpdate
logger = logging.getLogger(__name__)
router = APIRouter(
prefix="/api/repos",
tags=["repos"],
dependencies=[Depends(verify_api_key)],
)
@router.post("", response_model=RepoResponse, status_code=201)
async def create_repo(
body: RepoCreate,
session: AsyncSession = Depends(get_db_session), # noqa: B008
) -> RepoResponse:
"""Register a new repository (or update if same local_path exists)."""
repo = await crud.upsert_repository(
session,
name=body.name,
local_path=body.local_path,
url=body.url,
default_branch=body.default_branch,
settings=body.settings,
)
return RepoResponse.from_orm(repo)
@router.get("", response_model=list[RepoResponse])
async def list_repos(
session: AsyncSession = Depends(get_db_session), # noqa: B008
) -> list[RepoResponse]:
"""List all registered repositories."""
result = await session.execute(select(Repository).order_by(Repository.updated_at.desc()))
repos = result.scalars().all()
return [RepoResponse.from_orm(r) for r in repos]
@router.get("/{repo_id}", response_model=RepoResponse)
async def get_repo(
repo_id: str,
session: AsyncSession = Depends(get_db_session), # noqa: B008
) -> RepoResponse:
"""Get a single repository by ID."""
repo = await crud.get_repository(session, repo_id)
if repo is None:
raise HTTPException(status_code=404, detail="Repository not found")
return RepoResponse.from_orm(repo)
@router.patch("/{repo_id}", response_model=RepoResponse)
async def update_repo(
repo_id: str,
body: RepoUpdate,
session: AsyncSession = Depends(get_db_session), # noqa: B008
) -> RepoResponse:
"""Update repository fields."""
repo = await crud.get_repository(session, repo_id)
if repo is None:
raise HTTPException(status_code=404, detail="Repository not found")
if body.name is not None:
repo.name = body.name
if body.url is not None:
repo.url = body.url
if body.default_branch is not None:
repo.default_branch = body.default_branch
if body.settings is not None:
import json
repo.settings_json = json.dumps(body.settings)
await session.flush()
return RepoResponse.from_orm(repo)
@router.get("/{repo_id}/stats", response_model=RepoStatsResponse)
async def get_repo_stats(
repo_id: str,
session: AsyncSession = Depends(get_db_session), # noqa: B008
) -> RepoStatsResponse:
"""Get aggregate stats for a repository."""
repo = await crud.get_repository(session, repo_id)
if repo is None:
raise HTTPException(status_code=404, detail="Repository not found")
file_count_result = await session.execute(
select(func.count(GraphNode.id)).where(GraphNode.repository_id == repo_id)
)
file_count = file_count_result.scalar_one() or 0
symbol_count_result = await session.execute(
select(func.sum(GraphNode.symbol_count)).where(GraphNode.repository_id == repo_id)
)
symbol_count = int(symbol_count_result.scalar_one() or 0)
entry_count_result = await session.execute(
select(func.count(GraphNode.id)).where(
GraphNode.repository_id == repo_id,
GraphNode.is_entry_point == True, # noqa: E712
)
)
entry_point_count = entry_count_result.scalar_one() or 0
avg_conf_result = await session.execute(
select(func.avg(Page.confidence)).where(Page.repository_id == repo_id)
)
avg_confidence = float(avg_conf_result.scalar_one() or 0.0)
doc_coverage_pct = avg_confidence * 100
dead_result = await session.execute(
select(func.count(DeadCodeFinding.id)).where(
DeadCodeFinding.repository_id == repo_id,
DeadCodeFinding.kind == "unused_export",
DeadCodeFinding.status == "open",
)
)
dead_export_count = dead_result.scalar_one() or 0
return RepoStatsResponse(
file_count=file_count,
symbol_count=symbol_count,
entry_point_count=entry_point_count,
doc_coverage_pct=doc_coverage_pct,
freshness_score=doc_coverage_pct,
dead_export_count=dead_export_count,
)
@router.post("/{repo_id}/sync", status_code=202)
async def sync_repo(
repo_id: str,
request: Request,
session: AsyncSession = Depends(get_db_session), # noqa: B008
) -> dict:
"""Trigger an incremental documentation sync for a repository.
Creates a generation job, launches the pipeline in the background,
and returns immediately with the job ID.
"""
repo = await crud.get_repository(session, repo_id)
if repo is None:
raise HTTPException(status_code=404, detail="Repository not found")
# Prevent concurrent pipeline runs on the same repo
active = await session.execute(
select(GenerationJob.id)
.where(GenerationJob.repository_id == repo_id)
.where(GenerationJob.status.in_(["pending", "running"]))
.limit(1)
)
if active.scalar_one_or_none() is not None:
raise HTTPException(status_code=409, detail="A sync job is already in progress for this repository")
job = await crud.upsert_generation_job(
session,
repository_id=repo_id,
status="pending",
)
# Commit (not just flush) so the background task's separate session can
# see the job row. SQLite WAL isolation hides uncommitted rows from
# other connections, so flush() alone is not sufficient.
await session.commit()
_launch_job_task(request, job.id)
return {"job_id": job.id, "status": "accepted"}
@router.post("/{repo_id}/full-resync", status_code=202)
async def full_resync(
repo_id: str,
request: Request,
session: AsyncSession = Depends(get_db_session), # noqa: B008
) -> dict:
"""Trigger a full re-generation of all documentation.
Creates a generation job, launches the pipeline in the background,
and returns immediately with the job ID.
"""
repo = await crud.get_repository(session, repo_id)
if repo is None:
raise HTTPException(status_code=404, detail="Repository not found")
# Prevent concurrent pipeline runs on the same repo
active = await session.execute(
select(GenerationJob.id)
.where(GenerationJob.repository_id == repo_id)
.where(GenerationJob.status.in_(["pending", "running"]))
.limit(1)
)
if active.scalar_one_or_none() is not None:
raise HTTPException(status_code=409, detail="A sync job is already in progress for this repository")
job = await crud.upsert_generation_job(
session,
repository_id=repo_id,
status="pending",
config={"mode": "full_resync"},
)
# Commit (not just flush) so the background task's separate session can
# see the job row. See sync_repo comment for rationale.
await session.commit()
_launch_job_task(request, job.id)
return {"job_id": job.id, "status": "accepted"}
def _launch_job_task(request: Request, job_id: str) -> None:
"""Launch a background job task with proper lifecycle management.
Stores a strong reference in ``app.state.background_tasks`` to prevent
garbage collection, and removes it when the task finishes. Exceptions
are logged instead of silently swallowed.
"""
task = asyncio.create_task(execute_job(job_id, request.app.state), name=f"job-{job_id}")
bg_tasks: set[asyncio.Task] = request.app.state.background_tasks # type: ignore[assignment]
bg_tasks.add(task)
def _on_done(t: asyncio.Task) -> None:
bg_tasks.discard(t)
if not t.cancelled() and t.exception() is not None:
logger.error("background_job_failed", exc_info=t.exception())
task.add_done_callback(_on_done)
@router.get("/{repo_id}/export")
async def export_wiki(
repo_id: str,
session: AsyncSession = Depends(get_db_session), # noqa: B008
) -> StreamingResponse:
"""Export all wiki pages as a ZIP of markdown files with folder structure."""
repo = await crud.get_repository(session, repo_id)
if repo is None:
raise HTTPException(status_code=404, detail="Repository not found")
pages = (await session.execute(select(Page).where(Page.repository_id == repo_id))).scalars().all()
if not pages:
raise HTTPException(status_code=404, detail="No pages to export")
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
for page in pages:
target = page.target_path or page.id
safe = (
target.replace("::", "/")
.replace("->", "--")
.replace("\\", "/")
)
path = PurePosixPath("wiki") / page.page_type / safe
if path.suffix != ".md":
path = path.with_suffix(path.suffix + ".md")
content = f"# {page.title}\n\n{page.content}"
zf.writestr(str(path), content)
buf.seek(0)
filename = f"{repo.name}-wiki.zip"
return StreamingResponse(
buf,
media_type="application/zip",
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
)