chore: update src/lib/partial-file-approvals.ts

swadhinbiswas committed 8 months ago
556e308
1 parent 9968b3b
commit 556e308
1 file changed
+31 -9
Showing 1 changed file+31-9
▼src/lib/partial-file-approvals.ts
+31-9
9
9
import { logger } from "./logger";
10
10
import { pullRequests } from "@/db/schema/pull-requests";
11
11
import { users } from "@/db/schema/users";
12
+import { getChangedFiles } from "./git";
13
+import { resolveRepoPath } from "./git-storage";
12
14
13
15
/**
14
16
* File-level approvals table
107
109
return { files: [], allApproved: false };
108
110
}
109
111
110
- // Get changed files (would need git diff in real implementation)
111
- // For now, return approvals we have
112
+ const repo = await db.query.repositories.findFirst({
113
+ where: eq(schema.repositories.id, pr.repositoryId),
114
+ });
115
+
116
+ if (!repo) {
117
+ return { files: [], allApproved: false };
118
+ }
119
+
120
+ let changedPaths: string[] = [];
121
+ try {
122
+ const repoPath = await resolveRepoPath(repo.diskPath);
123
+ changedPaths = await getChangedFiles(repoPath, pr.baseBranch, pr.headBranch);
124
+ } catch (error) {
125
+ logger.warn({ prId, error }, "Failed to calculate changed files for file approvals");
126
+ }
127
+
112
128
const approvals = await db.query.fileApprovals?.findMany({
113
129
where: eq(schema.fileApprovals.pullRequestId, prId),
114
130
with: { approvedBy: { columns: { username: true } } },
128
144
}
129
145
}
130
146
131
- const files = Array.from(fileMap.entries()).map(([path, data]) => ({
132
- path,
133
- approved: data.approvers.length > 0 && !data.stale,
134
- approvers: data.approvers,
135
- stale: data.stale,
136
- }));
147
+ const fileSet = new Set<string>(changedPaths);
148
+ for (const p of fileMap.keys()) fileSet.add(p);
149
+
150
+ const files = Array.from(fileSet.values()).map((path) => {
151
+ const data = fileMap.get(path) || { approvers: [], stale: false };
152
+ return {
153
+ path,
154
+ approved: data.approvers.length > 0 && !data.stale,
155
+ approvers: data.approvers,
156
+ stale: data.stale,
157
+ };
158
+ });
137
159
138
160
return {
139
161
files,
140
- allApproved: files.every(f => f.approved),
162
+ allApproved: files.length > 0 && files.every(f => f.approved),
141
163
};
142
164
}
143
165
144
166