swadhinbiswas chore: update src/lib/partial-file-approvals.ts · 2/18/2026
556e308
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
/**
* Partial File Approvals Library
* Allow approving specific files within a PR
*/
import { pgTable, text, timestamp } from "drizzle-orm/pg-core";
import { getDatabase, schema } from "@/db";
import { eq, and } from "drizzle-orm";
import { logger } from "./logger";
import { pullRequests } from "@/db/schema/pull-requests";
import { users } from "@/db/schema/users";
import { getChangedFiles } from "./git";
import { resolveRepoPath } from "./git-storage";
/**
* File-level approvals table
*/
export const fileApprovals = pgTable("file_approvals", {
id: text("id").primaryKey(),
pullRequestId: text("pull_request_id")
.notNull()
.references(() => pullRequests.id, { onDelete: "cascade" }),
path: text("path").notNull(), // File path
approvedById: text("approved_by_id")
.notNull()
.references(() => users.id),
approvedAt: timestamp("approved_at").notNull().defaultNow(),
commitSha: text("commit_sha").notNull(), // SHA when approved
comment: text("comment"),
});
export type FileApproval = typeof fileApprovals.$inferSelect;
/**
* Approve a specific file in a PR
*/
export async function approveFile(options: {
pullRequestId: string;
path: string;
approvedById: string;
comment?: string;
}): Promise<FileApproval> {
const db = getDatabase();
// Get current PR head
const pr = await db.query.pullRequests.findFirst({
where: eq(schema.pullRequests.id, options.pullRequestId),
});
if (!pr) {
throw new Error("Pull request not found");
}
// Check for existing approval
const existing = await db.query.fileApprovals?.findFirst({
where: and(
eq(schema.fileApprovals.pullRequestId, options.pullRequestId),
eq(schema.fileApprovals.path, options.path),
eq(schema.fileApprovals.approvedById, options.approvedById)
),
});
if (existing) {
// Update existing approval
// @ts-expect-error - Drizzle multi-db union type issue
await db.update(schema.fileApprovals)
.set({
commitSha: pr.headSha,
approvedAt: new Date(),
comment: options.comment,
})
.where(eq(schema.fileApprovals.id, existing.id));
return { ...existing, commitSha: pr.headSha, approvedAt: new Date() };
}
const approval: FileApproval = {
id: crypto.randomUUID(),
pullRequestId: options.pullRequestId,
path: options.path,
approvedById: options.approvedById,
approvedAt: new Date(),
commitSha: pr.headSha,
comment: options.comment || null,
};
// @ts-expect-error - Drizzle multi-db union type issue
await db.insert(schema.fileApprovals).values(approval);
logger.info({ prId: options.pullRequestId, path: options.path }, "File approved");
return approval;
}
/**
* Get file approval status for a PR
*/
export async function getFileApprovalStatus(prId: string): Promise<{
files: { path: string; approved: boolean; approvers: string[]; stale: boolean }[];
allApproved: boolean;
}> {
const db = getDatabase();
const pr = await db.query.pullRequests.findFirst({
where: eq(schema.pullRequests.id, prId),
});
if (!pr) {
return { files: [], allApproved: false };
}
const repo = await db.query.repositories.findFirst({
where: eq(schema.repositories.id, pr.repositoryId),
});
if (!repo) {
return { files: [], allApproved: false };
}
let changedPaths: string[] = [];
try {
const repoPath = await resolveRepoPath(repo.diskPath);
changedPaths = await getChangedFiles(repoPath, pr.baseBranch, pr.headBranch);
} catch (error) {
logger.warn({ prId, error }, "Failed to calculate changed files for file approvals");
}
const approvals = await db.query.fileApprovals?.findMany({
where: eq(schema.fileApprovals.pullRequestId, prId),
with: { approvedBy: { columns: { username: true } } },
}) || [];
// Group by path
const fileMap = new Map<string, { approvers: string[]; stale: boolean }>();
for (const approval of approvals) {
if (!fileMap.has(approval.path)) {
fileMap.set(approval.path, { approvers: [], stale: false });
}
const file = fileMap.get(approval.path)!;
file.approvers.push(approval.approvedBy?.username || "Unknown");
if (approval.commitSha !== pr.headSha) {
file.stale = true;
}
}
const fileSet = new Set<string>(changedPaths);
for (const p of fileMap.keys()) fileSet.add(p);
const files = Array.from(fileSet.values()).map((path) => {
const data = fileMap.get(path) || { approvers: [], stale: false };
return {
path,
approved: data.approvers.length > 0 && !data.stale,
approvers: data.approvers,
stale: data.stale,
};
});
return {
files,
allApproved: files.length > 0 && files.every(f => f.approved),
};
}
/**
* Revoke a file approval
*/
export async function revokeFileApproval(
approvalId: string
): Promise<boolean> {
const db = getDatabase();
try {
// @ts-expect-error - Drizzle multi-db union type issue
await db.delete(schema.fileApprovals)
.where(eq(schema.fileApprovals.id, approvalId));
return true;
} catch (error) {
logger.error({ approvalId, error }, "Failed to revoke file approval");
return false;
}
}
/**
* Get files needing approval (not yet approved or stale)
*/
export async function getFilesNeedingApproval(prId: string): Promise<string[]> {
const status = await getFileApprovalStatus(prId);
return status.files
.filter(f => !f.approved || f.stale)
.map(f => f.path);
}