Skip to main content

Application Structure

The backend is a FastAPI application (app/main.py) with async/await throughout. All I/O operations (database, Neo4j, HTTP calls) are non-blocking.

Application Lifecycle

The lifespan context manager in app/main.py handles startup and shutdown:
  1. Run Alembic migrations - Apply any pending database schema changes
  2. Create PostgreSQL tables - Ensure all SQLAlchemy models are present
  3. Initialize Neo4j driver - Connect to knowledge graph
  4. Create Neo4j schema - Set up constraints and indexes
  5. Backfill repos - Scan connected repos not yet in Neo4j (background task)
  1. Dispose PostgreSQL engine - Close all database connections
  2. Close Neo4j driver - Gracefully disconnect from graph

API Routes

Authentication Routes

GET /auth/github

Starts GitHub OAuth flow. Redirects to github.com/login/oauth/authorize with client_id and scope=repo,read:org.

GET /auth/github/callback

OAuth callback endpoint. Exchanges authorization code for access token, creates/updates user, sets JWT cookie, redirects to frontend.

GET /auth/me

Returns current user profile (requires JWT cookie).

POST /auth/logout

Clears auth cookie.

Webhook Routes

POST /api/v1/webhooks/github

Per-repo webhook receiver
  1. Verify HMAC-SHA256 signature against webhook_secret
  2. Deduplicate (ignore duplicate events within 1 hour)
  3. Create Event row with status=pending
  4. Return HTTP 200 immediately (< 1 second)
  5. Process PR in background via BackgroundTask

Repository Routes

GET /api/v1/repos

Lists all GitHub repos accessible to the user with connection status.

POST /api/v1/repos/{owner}/{repo}/install

Connects a repository:
  1. Create Installation record
  2. Install webhook (per-repo secret)
  3. Scan file tree → Neo4j graph (background task)

POST /api/v1/repos/{owner}/{repo}/rescan

Re-scans repository file tree and rebuilds Neo4j graph. Useful after repo restructure.

DELETE /api/v1/repos/{owner}/{repo}/install

Disconnects repository:
  1. Remove GitHub webhook
  2. Mark Installation as inactive
  3. (Optional) Delete Neo4j nodes/edges

Review Routes

GET /api/v1/reviews

Returns PR review history with filters:
  • repo: Filter by repository
  • status: Filter by status (completed, failed, pending)
  • limit: Max results (default 20)

Analytics Routes

GET /api/v1/analytics

Returns team metrics:
  • Total PRs reviewed
  • Verdict distribution (approve/request_changes/needs_discussion)
  • Top contributors
  • Recent review timeline

Memory Routes

GET /api/v1/memory

Lists Mem0 memories for a repository.

POST /api/v1/memory

Manually adds a project rule or developer pattern to Mem0.

DELETE /api/v1/memory/{id}

Removes a memory from Mem0.

GET /api/v1/memory/project-map

Returns aggregated project context summary (all memories).

Middleware Stack

CORS Middleware

allow_credentials=True is required because the frontend sends JWT cookies with withCredentials: true in axios.

Request Logging Middleware

Logs every HTTP request with method, path, status code, and duration in milliseconds.

MCP Server Mount

Why mount() instead of include_router()?FastMCP’s sse_app() returns a Starlette ASGI application, not an APIRouter. Mounting allows the MCP server to handle its own routing for SSE (GET /mcp/sse) and JSON-RPC (POST /mcp/messages).

Health Check Endpoint

Environment Configuration

All settings are managed via Pydantic in app/core/config.py:
All MCP integration URLs are optional. If not set, Nectr gracefully skips that integration and logs an info message.

Next Steps

Service Layer

Deep dive into PR review, AI, and context services

Data Flow

Follow a webhook event through the entire system

Database Schema

PostgreSQL tables and relationships

Neo4j Graph

Knowledge graph schema and queries