Spaces:
Running
Running
File size: 9,652 Bytes
4b445f6 | 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 | // ---------------------------------------------------------------------------
// Ninja Code Guard – API client + mock data for development
// ---------------------------------------------------------------------------
import type {
Finding,
PRReviewRecord,
RepoStats,
SynthesizedReview,
} from "./types";
const API_URL = process.env.NEXT_PUBLIC_API_URL ?? "";
// ---------------------------------------------------------------------------
// Generic fetcher
// ---------------------------------------------------------------------------
async function apiFetch<T>(path: string): Promise<T> {
if (!API_URL) return null as unknown as T; // fall through to mock
const res = await fetch(`${API_URL}${path}`, { next: { revalidate: 60 } });
if (!res.ok) throw new Error(`API ${res.status}: ${res.statusText}`);
return res.json() as Promise<T>;
}
// ---------------------------------------------------------------------------
// Mock findings
// ---------------------------------------------------------------------------
const MOCK_FINDINGS: Finding[] = [
{
agent: "security",
file_path: "src/auth/login.ts",
line_start: 42,
line_end: 48,
severity: "critical",
category: "SQL Injection",
title: "Unsanitized user input in SQL query",
description:
"User-supplied `username` is interpolated directly into a SQL query string without parameterisation. An attacker can inject arbitrary SQL to bypass authentication or exfiltrate data.",
suggested_fix:
'Use parameterised queries: `db.query("SELECT * FROM users WHERE username = $1", [username])`',
cwe_id: "CWE-89",
confidence: 0.95,
},
{
agent: "security",
file_path: "src/api/middleware.ts",
line_start: 15,
line_end: 22,
severity: "high",
category: "Authentication",
title: "Missing JWT expiry validation",
description:
"The JWT verification step does not check the `exp` claim, allowing expired tokens to grant access indefinitely.",
suggested_fix:
"Pass `{ algorithms: ['HS256'], ignoreExpiration: false }` to `jwt.verify()`.",
cwe_id: "CWE-613",
confidence: 0.88,
},
{
agent: "performance",
file_path: "src/services/dataLoader.ts",
line_start: 78,
line_end: 95,
severity: "high",
category: "N+1 Query",
title: "Sequential database queries inside loop",
description:
"Each iteration of the `for` loop executes a separate `SELECT` query. For 1 000 records this produces 1 001 queries instead of a single batch query.",
suggested_fix:
"Collect IDs first, then fetch all records in a single `WHERE id IN (...)` query.",
cwe_id: null,
confidence: 0.92,
},
{
agent: "performance",
file_path: "src/utils/imageProcessor.ts",
line_start: 12,
line_end: 30,
severity: "medium",
category: "Memory",
title: "Large buffer allocated synchronously",
description:
"A 50 MB buffer is allocated on the main thread for image processing. This can cause the event loop to stall and trigger OOM errors under load.",
suggested_fix:
"Stream the image in chunks or offload processing to a worker thread.",
cwe_id: null,
confidence: 0.78,
},
{
agent: "style",
file_path: "src/components/Dashboard.tsx",
line_start: 5,
line_end: 5,
severity: "low",
category: "Naming",
title: "Component file uses default export without matching name",
description:
"The file exports `export default function Dash()` which does not match the filename `Dashboard.tsx`. This hurts discoverability in IDEs and stack traces.",
suggested_fix:
"Rename the function to `Dashboard` or rename the file to `Dash.tsx`.",
cwe_id: null,
confidence: 0.99,
},
{
agent: "style",
file_path: "src/hooks/useData.ts",
line_start: 18,
line_end: 45,
severity: "low",
category: "Complexity",
title: "Function exceeds recommended cyclomatic complexity",
description:
"The `transformPayload` function has a cyclomatic complexity of 14. Consider extracting branches into helper functions for readability.",
suggested_fix:
"Extract the nested conditionals into separate pure functions (e.g. `normaliseDate`, `mapStatus`).",
cwe_id: null,
confidence: 0.85,
},
{
agent: "security",
file_path: "src/config/cors.ts",
line_start: 3,
line_end: 8,
severity: "medium",
category: "CORS",
title: "Wildcard CORS origin in production config",
description:
"The CORS configuration uses `origin: '*'` which allows any website to make credentialed requests to the API.",
suggested_fix:
"Restrict the origin to your frontend domain(s): `origin: ['https://app.example.com']`.",
cwe_id: "CWE-942",
confidence: 0.91,
},
];
// ---------------------------------------------------------------------------
// Mock PR review records
// ---------------------------------------------------------------------------
function makeMockReviews(
repo: string,
count: number
): PRReviewRecord[] {
const base = Date.now();
return Array.from({ length: count }, (_, i) => {
const score = Math.min(100, Math.max(35, 72 + Math.round(Math.sin(i) * 18)));
const crit = score < 50 ? 2 : score < 70 ? 1 : 0;
const high = Math.max(0, 3 - Math.floor(score / 30));
const med = Math.max(0, 4 - Math.floor(score / 25));
const low = 2;
return {
id: `pr-${repo}-${i}`,
repo_full_name: repo,
pr_number: 100 + count - i,
commit_sha: `abc${String(i).padStart(4, "0")}`,
health_score: score,
critical_count: crit,
high_count: high,
medium_count: med,
low_count: low,
summary: `Automated review for PR #${100 + count - i}`,
findings: MOCK_FINDINGS.slice(0, 3 + (i % 4)),
duration_ms: 1200 + i * 300,
created_at: new Date(base - i * 86_400_000).toISOString(),
};
});
}
// ---------------------------------------------------------------------------
// Mock repos
// ---------------------------------------------------------------------------
export interface MockRepo {
owner: string;
repo: string;
full_name: string;
health_score: number;
open_prs: number;
last_review: string;
}
export const MOCK_REPOS: MockRepo[] = [
{
owner: "acme",
repo: "web-app",
full_name: "acme/web-app",
health_score: 87,
open_prs: 4,
last_review: "2 hours ago",
},
{
owner: "acme",
repo: "api-server",
full_name: "acme/api-server",
health_score: 64,
open_prs: 7,
last_review: "35 minutes ago",
},
{
owner: "acme",
repo: "mobile-sdk",
full_name: "acme/mobile-sdk",
health_score: 93,
open_prs: 2,
last_review: "1 day ago",
},
{
owner: "acme",
repo: "infra-tools",
full_name: "acme/infra-tools",
health_score: 51,
open_prs: 11,
last_review: "10 minutes ago",
},
];
// ---------------------------------------------------------------------------
// Mock synthesized review
// ---------------------------------------------------------------------------
const MOCK_SYNTH_REVIEW: SynthesizedReview = {
health_score: 64,
executive_summary:
"This PR introduces several new API endpoints and a database migration. While the feature logic is sound, there are critical security issues — most notably an SQL injection vulnerability in the login flow — and performance concerns around N+1 queries in the data-loading layer. Style issues are minor but should be addressed for long-term maintainability.",
recommendation: "request_changes",
findings: MOCK_FINDINGS,
critical_count: 1,
high_count: 2,
medium_count: 2,
low_count: 2,
duration_ms: 3420,
};
// ---------------------------------------------------------------------------
// Public API functions
// ---------------------------------------------------------------------------
export async function getRepoReviews(
owner: string,
repo: string
): Promise<PRReviewRecord[]> {
try {
if (API_URL) return await apiFetch(`/repos/${owner}/${repo}/reviews`);
} catch {
/* fall through to mock */
}
return makeMockReviews(`${owner}/${repo}`, 10);
}
export async function getReviewDetail(
owner: string,
repo: string,
prNumber: number
): Promise<{ review: SynthesizedReview; record: PRReviewRecord }> {
try {
if (API_URL)
return await apiFetch(`/repos/${owner}/${repo}/prs/${prNumber}`);
} catch {
/* fall through to mock */
}
const records = makeMockReviews(`${owner}/${repo}`, 10);
const record =
records.find((r) => r.pr_number === prNumber) ?? records[0];
return {
review: { ...MOCK_SYNTH_REVIEW, health_score: record.health_score },
record,
};
}
export async function getRepoStats(
owner: string,
repo: string
): Promise<RepoStats> {
try {
if (API_URL) return await apiFetch(`/repos/${owner}/${repo}/stats`);
} catch {
/* fall through to mock */
}
const reviews = makeMockReviews(`${owner}/${repo}`, 10);
const scores = reviews.map((r) => r.health_score).reverse();
return {
repo_full_name: `${owner}/${repo}`,
total_reviews: reviews.length,
average_health_score: Math.round(
scores.reduce((a, b) => a + b, 0) / scores.length
),
total_findings: reviews.reduce((s, r) => s + r.findings.length, 0),
recent_scores: scores,
top_categories: [
{ category: "SQL Injection", count: 4 },
{ category: "N+1 Query", count: 3 },
{ category: "Naming", count: 6 },
{ category: "Authentication", count: 2 },
{ category: "Complexity", count: 5 },
],
};
}
|