chore: update src/lib/code-quality.ts

swadhinbiswas committed 8 months ago
df89663
1 parent 7640c46
commit df89663
1 file changed
+58 -7
Showing 1 changed file+58-7
▼src/lib/code-quality.ts
+58-7
327
327
if (!config || !config.serverUrl || !config.projectKey) return false;
328
328
329
329
try {
330
- // In real implementation, this would trigger sonar-scanner
331
- // For now, create a pending report
330
+ const status = config.apiToken
331
+ ? await fetchSonarQubeStatus({
332
+ serverUrl: config.serverUrl,
333
+ projectKey: config.projectKey,
334
+ token: config.apiToken,
335
+ })
336
+ : null;
337
+
332
338
const report = {
333
339
id: crypto.randomUUID(),
334
340
repositoryId: options.repositoryId,
340
346
linesTotal: null,
341
347
branchCoverage: null,
342
348
delta: null,
343
- status: "pending",
349
+ status: status
350
+ ? status.status === "OK"
351
+ ? "success"
352
+ : "failure"
353
+ : "pending",
344
354
reportUrl: `${config.serverUrl}/dashboard?id=${config.projectKey}`,
345
355
createdAt: new Date(),
346
356
};
348
358
// @ts-expect-error - Drizzle multi-db union type issue
349
359
await db.insert(schema.coverageReports).values(report);
350
360
351
- logger.info({ projectKey: config.projectKey }, "SonarQube analysis triggered");
361
+ if (status?.issues?.length) {
362
+ const issues = status.issues.map((issue) => ({
363
+ ...issue,
364
+ id: crypto.randomUUID(),
365
+ repositoryId: options.repositoryId,
366
+ pullRequestId: options.pullRequestId || null,
367
+ commitSha: options.commitSha,
368
+ provider: "sonarqube",
369
+ createdAt: new Date(),
370
+ }));
371
+
372
+ // @ts-expect-error - Drizzle multi-db union type issue
373
+ await db.insert(schema.codeQualityIssues).values(issues);
374
+ }
375
+
376
+ logger.info(
377
+ { projectKey: config.projectKey, qualityGate: status?.status || "pending" },
378
+ "SonarQube analysis processed"
379
+ );
352
380
return true;
353
381
} catch (error) {
354
382
logger.error({ error }, "Failed to trigger SonarQube analysis");
433
461
const issues: CodeQualityIssue[] = [];
434
462
435
463
try {
436
- // In real implementation, this would call Snyk API
437
- // For now, simulate a scan result
438
464
const response = await fetch(`https://api.snyk.io/v1/test`, {
439
465
method: "POST",
440
466
headers: {
443
469
},
444
470
body: JSON.stringify({
445
471
target: { remoteUrl: "", branch: "" },
472
+ targetFile: options.targetFile,
446
473
}),
447
474
});
448
475
449
476
if (!response.ok) {
450
- logger.warn("Snyk API call failed, using mock data");
477
+ const errorText = await response.text().catch(() => "");
478
+ throw new Error(`Snyk API call failed: ${response.status} ${errorText}`);
479
+ }
480
+
481
+ const data = await response.json();
482
+ const vulnerabilities = (data?.vulnerabilities || []) as any[];
483
+
484
+ for (const vuln of vulnerabilities) {
485
+ const id = crypto.randomUUID();
486
+ issues.push({
487
+ id,
488
+ repositoryId: options.repositoryId,
489
+ pullRequestId: options.pullRequestId || null,
490
+ commitSha: options.commitSha,
491
+ provider: "snyk",
492
+ issueType: vuln.type || "vulnerability",
493
+ severity: (vuln.severity || "medium").toLowerCase(),
494
+ message: vuln.title || vuln.id || "Snyk vulnerability",
495
+ file: vuln.from?.[1] || options.targetFile || null,
496
+ line: null,
497
+ rule: vuln.id || null,
498
+ effort: vuln.fixedIn?.[0] || null,
499
+ status: "open",
500
+ createdAt: new Date(),
501
+ } as CodeQualityIssue);
451
502
}
452
503
453
504
// Store issues
454
505