swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
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
/**
* Stack Approvals Library
* Calculate and validate approval status across entire stacks
*/
import { getDatabase, schema } from "@/db";
import { eq, and, inArray } from "drizzle-orm";
import { getStack } from "./stacks";
import { logger } from "./logger";
import crypto from "crypto";
export interface StackApprovalStatus {
stackId: string;
allApproved: boolean;
prs: {
prId: string;
prNumber: number;
title: string;
isApproved: boolean;
approvalCount: number;
requiredApprovals: number;
changesRequested: boolean;
}[];
}
/**
* Get approval status for an entire stack
*/
export async function getStackApprovalStatus(stackId: string): Promise<StackApprovalStatus | null> {
const stack = await getStack(stackId);
if (!stack) return null;
const db = getDatabase();
const reviewRequirements = await db.query.reviewRequirements.findFirst({
where: eq(schema.reviewRequirements.repositoryId, stack.stack.repositoryId),
});
const rules = await db.query.branchProtection.findMany({
where: and(
eq(schema.branchProtection.repositoryId, stack.stack.repositoryId),
eq(schema.branchProtection.active, true)
)
});
const prs: StackApprovalStatus["prs"] = [];
for (const entry of stack.entries) {
const reviews = await db.query.pullRequestReviews.findMany({
where: eq(schema.pullRequestReviews.pullRequestId, entry.pr.id),
});
const approvals = reviews.filter(r => r.state === "approved");
const changesRequested = reviews.some(r => r.state === "changes_requested");
const matchingRule = rules.find(rule => {
if (rule.pattern === entry.pr.baseBranch) return true;
if (rule.pattern.endsWith("*")) {
return entry.pr.baseBranch.startsWith(rule.pattern.slice(0, -1));
}
return false;
});
const requiredApprovals = Math.max(
reviewRequirements?.minApprovals ?? 0,
matchingRule ? (matchingRule.requiredApprovals ?? 1) : 0
);
prs.push({
prId: entry.pr.id,
prNumber: entry.pr.number,
title: entry.pr.title,
isApproved: approvals.length >= requiredApprovals && !changesRequested,
approvalCount: approvals.length,
requiredApprovals,
changesRequested,
});
}
return {
stackId,
allApproved: prs.every(pr => pr.isApproved),
prs,
};
}
/**
* Check if a stack can be merged (all PRs approved)
*/
export async function canMergeStack(stackId: string): Promise<{
canMerge: boolean;
blockers: string[];
}> {
const status = await getStackApprovalStatus(stackId);
if (!status) {
return { canMerge: false, blockers: ["Stack not found"] };
}
const blockers: string[] = [];
for (const pr of status.prs) {
if (!pr.isApproved) {
if (pr.changesRequested) {
blockers.push(`PR #${pr.prNumber}: Changes requested`);
} else {
blockers.push(`PR #${pr.prNumber}: Needs ${pr.requiredApprovals - pr.approvalCount} more approval(s)`);
}
}
}
return {
canMerge: blockers.length === 0,
blockers,
};
}
/**
* Request approval for all PRs in a stack
*/
export async function requestStackApproval(
stackId: string,
reviewerIds: string[]
): Promise<boolean> {
const stack = await getStack(stackId);
if (!stack) return false;
const db = getDatabase();
try {
for (const entry of stack.entries) {
for (const reviewerId of reviewerIds) {
// Check if already requested
const existing = await db.query.pullRequestReviewers.findFirst({
where: and(
eq(schema.pullRequestReviewers.pullRequestId, entry.pr.id),
eq(schema.pullRequestReviewers.userId, reviewerId)
),
});
if (!existing) {
// @ts-expect-error - Drizzle multi-db union type issue
await db.insert(schema.pullRequestReviewers).values({
id: crypto.randomUUID(),
pullRequestId: entry.pr.id,
userId: reviewerId,
isRequired: true,
requestedAt: new Date(),
});
}
}
}
logger.info({ stackId, reviewerCount: reviewerIds.length }, "Stack approval requested");
return true;
} catch (error) {
logger.error({ stackId, error }, "Failed to request stack approval");
return false;
}
}