Skip to content

Commit 62de985

Browse files
committed
fix: review round 2 -- redis guards, remove dead param, extract GitHub headers
1. indexing.py: added redis_client None guard before creating AnonymousIndexingJob in both start_anonymous_indexing and get_indexing_status. Returns 503 if Redis is down instead of crashing with AttributeError. 2. validation.py: removed unused 'req: Request' param from validate_github_repo and removed unused Request import. 3. validation.py: extracted _github_headers() helper to replace duplicate header construction in fetch_repo_metadata and count_code_files. Skipped (duplicates): create_session error handling (3rd time), list_repos sync (3rd time), validator dedup (refactor risk in split PR), limit_result typing, session ordering, private method naming, redundant get_session_data call. 289 tests pass.
1 parent da97a2b commit 62de985

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

backend/routes/playground/indexing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ async def start_anonymous_indexing(
152152

153153
# Create job and start background indexing
154154
response_time_ms = int((time.time() - start_time) * 1000)
155+
if not redis_client:
156+
raise HTTPException(status_code=503, detail="Indexing service unavailable (Redis down)")
155157
job_manager = AnonymousIndexingJob(redis_client)
156158
job_id = job_manager.generate_job_id()
157159

@@ -199,6 +201,8 @@ async def get_indexing_status(job_id: str, req: Request) -> dict:
199201
"error": "invalid_job_id", "message": "Invalid job ID format"
200202
})
201203

204+
if not redis_client:
205+
raise HTTPException(status_code=503, detail="Indexing service unavailable (Redis down)")
202206
job_manager = AnonymousIndexingJob(redis_client)
203207
job = job_manager.get_job(job_id)
204208

backend/routes/playground/validation.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import time
44
from typing import Optional
55
import httpx
6-
from fastapi import APIRouter, HTTPException, Request
6+
from fastapi import APIRouter, HTTPException
77
from pydantic import BaseModel, field_validator
88

99
from dependencies import cache
@@ -42,18 +42,22 @@ def parse_github_url(url: str) -> tuple[Optional[str], Optional[str], Optional[s
4242
return match.group("owner"), match.group("repo"), None
4343

4444

45-
async def fetch_repo_metadata(owner: str, repo: str) -> dict:
46-
"""Fetch repository metadata from GitHub API."""
47-
url = f"{GITHUB_API_BASE}/repos/{owner}/{repo}"
45+
def _github_headers() -> dict:
46+
"""Build GitHub API request headers with optional auth token."""
4847
headers = {"Accept": "application/vnd.github.v3+json", "User-Agent": "OpenCodeIntel/1.0"}
49-
5048
github_token = os.getenv("GITHUB_TOKEN")
5149
if github_token:
5250
headers["Authorization"] = f"token {github_token}"
51+
return headers
52+
53+
54+
async def fetch_repo_metadata(owner: str, repo: str) -> dict:
55+
"""Fetch repository metadata from GitHub API."""
56+
url = f"{GITHUB_API_BASE}/repos/{owner}/{repo}"
5357

5458
async with httpx.AsyncClient(timeout=GITHUB_API_TIMEOUT) as client:
5559
try:
56-
response = await client.get(url, headers=headers)
60+
response = await client.get(url, headers=_github_headers())
5761
if response.status_code == 404:
5862
return {"error": "not_found", "message": "Repository not found"}
5963
if response.status_code == 403:
@@ -73,15 +77,10 @@ async def count_code_files(
7377
) -> tuple[int, Optional[str]]:
7478
"""Count code files using GitHub tree API. Returns (file_count, error)."""
7579
url = f"{GITHUB_API_BASE}/repos/{owner}/{repo}/git/trees/{default_branch}?recursive=1"
76-
headers = {"Accept": "application/vnd.github.v3+json", "User-Agent": "OpenCodeIntel/1.0"}
77-
78-
github_token = os.getenv("GITHUB_TOKEN")
79-
if github_token:
80-
headers["Authorization"] = f"token {github_token}"
8180

8281
async with httpx.AsyncClient(timeout=GITHUB_API_TIMEOUT) as client:
8382
try:
84-
response = await client.get(url, headers=headers)
83+
response = await client.get(url, headers=_github_headers())
8584
if response.status_code == 404:
8685
return 0, "Could not fetch repository tree"
8786
if response.status_code == 403:
@@ -115,7 +114,7 @@ async def count_code_files(
115114

116115

117116
@router.post("/validate-repo")
118-
async def validate_github_repo(request: ValidateRepoRequest, req: Request) -> dict:
117+
async def validate_github_repo(request: ValidateRepoRequest) -> dict:
119118
"""Validate a GitHub repository URL for anonymous indexing."""
120119
start_time = time.time()
121120

0 commit comments

Comments
 (0)