Phase 7 Week 2: API & Automation¶
Status: In Progress Timeline: 5 days Prerequisites: ✅ Week 1 complete (95 tests passing)
Week 2 Overview¶
Goal¶
Build the API layer and webhook automation that allows users to trigger auto-fix PRs and handles GitHub events automatically.
What We're Building¶
Day 1: API endpoint (/api/teams/[teamId]/create-fix-pr)
- User-triggered fix PR creation
- Authentication, authorization, quota checks
- Integration with Week 1 job queue
Day 2: Webhook integration - Detect fix opportunities in PR analysis - Post comments with "Create Fix PR" button - Handle PR merge events
Day 3: Error handling & edge cases - GitHub API rate limits - Fix validation failures - PR conflicts
Day 4: Quota management - Define quotas per plan - Track usage in database - Enforce limits
Day 5: Monitoring & logging - PostHog telemetry - Error alerting - Admin dashboard
Day 1: API Endpoint¶
Objective¶
Create REST API endpoint that allows users to trigger auto-fix PR creation from the dashboard.
Component: /api/teams/[teamId]/create-fix-pr/route.ts¶
Input:
Output:
{
"success": true,
"jobId": "codeslick-fix-owner-repo-123-abc456",
"message": "Fix PR creation job queued"
}
Implementation Steps¶
- Authentication (NextAuth session required)
- Authorization (user must be team owner/admin/member)
- Parse PR URL (extract owner, repo, prNumber)
- Fetch PR data (via GitHub API to get files/vulnerabilities)
- Check quota (based on team's plan)
- Enqueue job (using FixPRQueue from Week 1)
- Return job ID (for tracking)
Error Handling¶
- 401: Not authenticated
- 403: Not authorized (not team member)
- 400: Invalid PR URL
- 404: PR not found
- 429: Quota exceeded
- 500: Server error
Tests¶
- ✅ Successful fix PR creation
- ✅ Unauthorized user (not logged in)
- ✅ Forbidden user (not team member)
- ✅ Invalid PR URL format
- ✅ PR not found on GitHub
- ✅ Quota exceeded (Free plan)
- ✅ Quota limit (Team plan at limit)
- ✅ PR already has fix PR
- ✅ No fixable vulnerabilities
- ✅ GitHub API error
Target: 10+ tests, all passing
Acceptance Criteria¶
- Endpoint responds with 200 + jobId for valid request
- Endpoint rejects unauthenticated requests (401)
- Endpoint rejects unauthorized requests (403)
- Endpoint validates PR URL format
- Endpoint checks quota before enqueueing
- Job is successfully enqueued to FixPRQueue
- All tests passing
Day 2: Webhook Integration¶
Objective¶
Automatically detect fix opportunities when PRs are analyzed and post helpful comments.
Component: Update /api/github/webhook/route.ts¶
Webhook Events to Handle:
1. pull_request.opened - Analyze PR, post auto-fix CTA if fixable issues found
2. pull_request.synchronize - Re-analyze on new commits
3. pull_request.closed - Update fix_prs table if PR merged
Implementation Steps¶
- Enhance PR analysis (detect fixable vulnerabilities)
- Post comment with CTA (if fixable issues found)
- Track fix PR lifecycle (update database on merge)
- Handle re-analysis (when original PR updated)
Comment Format¶
## 🔒 CodeSlick Security Analysis
Found **X vulnerabilities** (Y fixable automatically)
### Fixable Issues
- SQL Injection in `auth.js:42`
- XSS in `UserInput.tsx:18`
- Hardcoded secret in `config.py:7`
[🔧 Create Auto-Fix PR](https://codeslick.dev/teams/abc123/create-fix?pr=...)
Tests¶
- ✅ PR opened → comment posted with CTA
- ✅ PR with no fixable issues → no CTA
- ✅ PR synchronized → re-analyze
- ✅ PR merged → update fix_prs table
- ✅ Fix PR merged → post success comment
- ✅ Multiple PRs → handle concurrently
- ✅ Invalid installation → graceful error
- ✅ GitHub API timeout → retry
- ✅ Webhook signature validation
- ✅ Duplicate webhook events (idempotency)
Target: 10+ tests, all passing
Acceptance Criteria¶
- Webhook posts comment when fixable issues found
- Comment includes link to create fix PR
- PR merge events update fix_prs table
- Re-analysis triggers on PR updates
- All tests passing
Day 3: Error Handling & Edge Cases¶
Objective¶
Handle all the ways things can go wrong gracefully.
Error Scenarios¶
- GitHub API rate limits
- Detect 403 rate limit response
- Exponential backoff (1s, 2s, 4s, 8s, 16s)
-
Queue job for retry later
-
Fix validation failures
- Fixed code has syntax errors
- Fixed code breaks tests
-
Rollback and notify user
-
PR conflicts
- Base branch deleted
- PR already merged
- Base branch has conflicts
-
Skip and notify
-
GitHub permissions
- Installation lacks write permissions
- Branch protection rules
- User-friendly error messages
Implementation¶
Create src/lib/github/error-handler.ts:
export class GitHubAPIError extends Error {
constructor(
public statusCode: number,
public isRateLimit: boolean,
public retryAfter?: number
) {
super();
}
}
export function handleGitHubError(error: any): GitHubAPIError {
// Parse error, determine retry strategy
}
Tests¶
- ✅ Rate limit → exponential backoff
- ✅ Invalid syntax after fix → rollback
- ✅ Branch deleted → skip job
- ✅ PR merged → skip job
- ✅ Insufficient permissions → user-friendly error
- ✅ Network timeout → retry
- ✅ Invalid credentials → alert admin
- ✅ Concurrent PR updates → handle race condition
Target: 8+ tests, all passing
Day 4: Quota Management¶
Objective¶
Define and enforce quotas for auto-fix PR feature.
Quota Definitions¶
| Plan | Auto-Fix PRs/Month | Cost |
|---|---|---|
| Free | 0 | €0 |
| Team | 10 | €99/month |
| Enterprise | Unlimited | €299/month |
Database Schema Addition¶
ALTER TABLE teams ADD COLUMN auto_fix_prs_used INTEGER DEFAULT 0;
ALTER TABLE teams ADD COLUMN auto_fix_prs_reset_at TIMESTAMP DEFAULT NOW();
Implementation¶
Update src/lib/usage/quota-checker.ts:
export async function checkAutoFixQuota(teamId: string): Promise<QuotaCheckResult> {
const team = await db.select().from(teams).where(eq(teams.id, teamId));
// Reset counter if month passed
const now = new Date();
const resetAt = team.autoFixPrsResetAt;
if (now.getTime() - resetAt.getTime() > 30 * 24 * 60 * 60 * 1000) {
await db.update(teams)
.set({ autoFixPrsUsed: 0, autoFixPrsResetAt: now })
.where(eq(teams.id, teamId));
}
// Check quota
const limit = getAutoFixLimit(team.plan);
const remaining = limit === Infinity ? Infinity : limit - team.autoFixPrsUsed;
return {
allowed: remaining > 0,
limit,
used: team.autoFixPrsUsed,
remaining
};
}
Tests¶
- ✅ Free plan → quota denied
- ✅ Team plan → 10 per month
- ✅ Enterprise plan → unlimited
- ✅ Counter resets monthly
- ✅ Concurrent requests → no race condition
- ✅ Quota exceeded → 429 error
Target: 6+ tests, all passing
Day 5: Monitoring & Logging¶
Objective¶
Add comprehensive monitoring and logging for production readiness.
PostHog Events¶
posthog.capture('fix_pr_requested', {
teamId,
prUrl,
fixableCount
});
posthog.capture('fix_pr_created', {
teamId,
prNumber,
filesFixed,
vulnerabilitiesFixed,
duration
});
posthog.capture('fix_pr_merged', {
teamId,
prNumber,
timeToMerge
});
posthog.capture('fix_pr_failed', {
teamId,
prNumber,
errorType,
errorMessage
});
Logging¶
logger.info('Fix PR creation started', {
teamId,
prUrl,
jobId
});
logger.error('Fix PR creation failed', {
teamId,
prUrl,
error,
stack
});
Admin Dashboard¶
Add section to /dashboard/analytics:
- Total fix PRs created
- Success rate (% merged)
- Average time to merge
- Top teams using feature
Error Alerting¶
Email notification on: - 3+ consecutive job failures - GitHub API quota exhausted - Critical errors (500s)
Tests¶
- ✅ PostHog events captured correctly
- ✅ Logs written to Vercel
- ✅ Admin dashboard displays metrics
- ✅ Error alerts sent
Target: 4+ tests, all passing
Week 2 Deliverables¶
By end of Week 2, we will have:
- ✅ API Endpoint (
/api/teams/[teamId]/create-fix-pr) - User-triggered fix PR creation
-
10+ tests passing
-
✅ Webhook Integration (
/api/github/webhook) - Automatic comment posting
- PR lifecycle tracking
-
10+ tests passing
-
✅ Error Handling (
src/lib/github/error-handler.ts) - Rate limit handling
- Validation failures
-
8+ tests passing
-
✅ Quota Management (
src/lib/usage/quota-checker.ts) - Per-plan limits
- Monthly reset
-
6+ tests passing
-
✅ Monitoring (PostHog + Vercel logs)
- Event tracking
- Admin dashboard
- 4+ tests passing
Total New Tests: 38+ (Week 1: 95, Week 2: 38 = 133 total)
Success Criteria¶
- API endpoint works end-to-end (manual test)
- Webhook posts comments automatically
- Quotas enforced correctly
- All tests passing (133+)
- No breaking changes to existing features
- Ready for Week 3 (UI development)
Next: Week 3 (UI & Polish)¶
After Week 2, we'll build: - Team dashboard UI - PR comment UI improvements - Settings & preferences - Documentation & launch
Estimated: 5 days