/ opencodehub / src / db / schema / review-analysis.ts
Swadhin Biswas Feature/ai code reviewer (#73) · 8/2/2026
105a287
156 lines · 5.5 KB
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
/**
 * Review Analysis Schema - Drizzle ORM
 * Stores analysis results, summaries, and review data for PRs.
 */

import { index, integer, jsonb, pgTable, text, timestamp } from "drizzle-orm/pg-core";
import { pullRequests } from "./pull-requests";
import { repositories } from "./repositories";
import { users } from "./users";

/**
 * Stores the full analysis result for a PR review.
 */
export const prReviewAnalyses = pgTable(
  "pr_review_analyses",
  {
    id: text("id").primaryKey(),
    pullRequestId: text("pull_request_id")
      .notNull()
      .references(() => pullRequests.id, { onDelete: "cascade" }),
    repositoryId: text("repository_id")
      .notNull()
      .references(() => repositories.id, { onDelete: "cascade" }),
    status: text("status").notNull().default("pending"), // pending, running, completed, failed

    // Analysis results (JSON)
    complexityData: jsonb("complexity_data"),
    dependencyGraph: jsonb("dependency_graph"),
    architectureImpact: jsonb("architecture_impact"),
    changeGroups: jsonb("change_groups"),
    blastRadius: jsonb("blast_radius"),
    healthScore: integer("health_score"), // 0-100
    healthGrade: text("health_grade"), // A+, A, B, C, D, F

    // Summaries (JSON)
    summaries: jsonb("summaries"),

    // Metadata
    baseSha: text("base_sha"),
    headSha: text("head_sha"),
    filesAnalyzed: integer("files_analyzed"),
    chunksProcessed: integer("chunks_processed"),
    totalAdditions: integer("total_additions"),
    totalDeletions: integer("total_deletions"),

    // AI review metadata
    model: text("model"),
    provider: text("provider"),
    tokensUsed: integer("tokens_used"),
    costCents: integer("cost_cents"),
    rawAiResponse: jsonb("raw_ai_response"),

    // Tracking
    triggeredById: text("triggered_by_id").references(() => users.id),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    startedAt: timestamp("started_at"),
    completedAt: timestamp("completed_at"),
    errorMessage: text("error_message"),
  },
  (t) => ({
    pullRequestIdx: index("pr_analysis_pr_idx").on(t.pullRequestId),
    repositoryIdx: index("pr_analysis_repo_idx").on(t.repositoryId),
    statusIdx: index("pr_analysis_status_idx").on(t.status),
    createdIdx: index("pr_analysis_created_idx").on(t.createdAt),
  }),
);

/**
 * Stores file-level summaries.
 */
export const fileSummaries = pgTable(
  "file_summaries",
  {
    id: text("id").primaryKey(),
    analysisId: text("analysis_id")
      .notNull()
      .references(() => prReviewAnalyses.id, { onDelete: "cascade" }),
    filePath: text("file_path").notNull(),
    language: text("language"),
    summary: text("summary"),
    riskLevel: text("risk_level"), // low, medium, high, critical
    additions: integer("additions"),
    deletions: integer("deletions"),
    complexityScore: integer("complexity_score"),
    createdAt: timestamp("created_at").notNull().defaultNow(),
  },
  (t) => ({
    analysisIdx: index("file_summaries_analysis_idx").on(t.analysisId),
    filePathIdx: index("file_summaries_path_idx").on(t.filePath),
  }),
);

/**
 * Stores inline AI review comments on specific lines.
 */
export const aiInlineComments = pgTable(
  "ai_inline_comments",
  {
    id: text("id").primaryKey(),
    analysisId: text("analysis_id")
      .notNull()
      .references(() => prReviewAnalyses.id, { onDelete: "cascade" }),
    pullRequestId: text("pull_request_id")
      .notNull()
      .references(() => pullRequests.id, { onDelete: "cascade" }),
    filePath: text("file_path").notNull(),
    line: integer("line").notNull(),
    endLine: integer("end_line"),
    side: text("side").default("right"), // left, right
    severity: text("severity").notNull(), // info, warning, error, critical
    type: text("type").notNull(), // bug, security, performance, style, documentation, suggestion
    category: text("category"), // code-quality, security, performance, architecture, testing
    title: text("title").notNull(),
    message: text("message").notNull(),
    suggestedFix: text("suggested_fix"),
    explanation: text("explanation"),
    confidence: integer("confidence"), // 0-100
    isResolved: integer("is_resolved").default(0), // 0 or 1
    isApplied: integer("is_applied").default(0),
    appliedAt: timestamp("applied_at"),
    dismissedAt: timestamp("dismissed_at"),
    createdAt: timestamp("created_at").notNull().defaultNow(),
  },
  (t) => ({
    analysisIdx: index("ai_inline_comments_analysis_idx").on(t.analysisId),
    pullRequestIdx: index("ai_inline_comments_pr_idx").on(t.pullRequestId),
    filePathIdx: index("ai_inline_comments_path_idx").on(t.filePath),
    severityIdx: index("ai_inline_comments_severity_idx").on(t.severity),
  }),
);

/**
 * Tracks AI review conversation threads (chat with AI about PR).
 */
export const aiReviewThreads = pgTable(
  "ai_review_threads",
  {
    id: text("id").primaryKey(),
    analysisId: text("analysis_id")
      .notNull()
      .references(() => prReviewAnalyses.id, { onDelete: "cascade" }),
    pullRequestId: text("pull_request_id")
      .notNull()
      .references(() => pullRequests.id, { onDelete: "cascade" }),
    filePath: text("file_path"),
    line: integer("line"),
    messages: jsonb("messages").notNull(), // Array of { role, content, timestamp }
    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow(),
  },
  (t) => ({
    analysisIdx: index("ai_thread_analysis_idx").on(t.analysisId),
    pullRequestIdx: index("ai_thread_pr_idx").on(t.pullRequestId),
  }),
);