swadhinbiswas Add actionable merge-gate readiness reporting · 2/19/2026
0a55637
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { beforeEach, describe, expect, it, vi } from "vitest";
const {
canReadRepoMock,
canAdminRepoMock,
addRequiredCheckMock,
createMergeGateMock,
getMergeGatesMock,
evaluateGatesMock,
fakeSchema,
} = vi.hoisted(() => ({
canReadRepoMock: vi.fn(async () => true),
canAdminRepoMock: vi.fn(async () => true),
addRequiredCheckMock: vi.fn(async () => ({ id: "check-1", checkName: "ci/build", branch: "main" })),
createMergeGateMock: vi.fn(async () => ({ id: "gate-1", name: "PR Label Gate", gateType: "label" })),
getMergeGatesMock: vi.fn(async () => [{ id: "gate-1", name: "PR Label Gate", gateType: "label" }]),
evaluateGatesMock: vi.fn(async () => ({ canMerge: true, results: [] })),
fakeSchema: {
users: { username: {} },
repositories: { ownerId: {}, name: {}, id: {} },
requiredStatusChecks: { repositoryId: {} },
pullRequests: { repositoryId: {}, number: {} },
} as any,
}));
let mockDb: any;
vi.mock("@/db", () => ({
getDatabase: () => mockDb,
schema: fakeSchema,
}));
vi.mock("@/lib/permissions", () => ({
canReadRepo: canReadRepoMock,
canAdminRepo: canAdminRepoMock,
}));
vi.mock("@/lib/ci-gates", () => ({
addRequiredCheck: addRequiredCheckMock,
createMergeGate: createMergeGateMock,
getMergeGates: getMergeGatesMock,
evaluateGates: evaluateGatesMock,
}));
import { GET as mergeGatesGet, POST as mergeGatesPost } from "@/pages/api/repos/[owner]/[repo]/merge-gates";
function makeDb() {
return {
query: {
users: {
findFirst: vi.fn(async () => ({ id: "owner-1" })),
},
repositories: {
findFirst: vi.fn(async () => ({ id: "repo-1", ownerId: "owner-1", name: "demo" })),
},
requiredStatusChecks: {
findMany: vi.fn(async () => [{ id: "check-1", checkName: "ci/build", branch: "main" }]),
},
pullRequests: {
findFirst: vi.fn(async () => ({ id: "pr-1", number: 12 })),
},
},
};
}
async function readJson(response: Response): Promise<any> {
return response.json();
}
describe("merge gates route", () => {
beforeEach(() => {
vi.clearAllMocks();
mockDb = makeDb();
canReadRepoMock.mockResolvedValue(true);
canAdminRepoMock.mockResolvedValue(true);
addRequiredCheckMock.mockResolvedValue({ id: "check-1", checkName: "ci/build", branch: "main" });
createMergeGateMock.mockResolvedValue({ id: "gate-1", name: "PR Label Gate", gateType: "label" });
getMergeGatesMock.mockResolvedValue([{ id: "gate-1", name: "PR Label Gate", gateType: "label" }]);
evaluateGatesMock.mockResolvedValue({ canMerge: true, results: [] });
});
it("returns merge gate policy configuration for readers", async () => {
getMergeGatesMock.mockResolvedValue([
{ id: "gate-1", name: "PR Label Gate", gateType: "custom", conditionScript: null },
] as any);
mockDb.query.requiredStatusChecks.findMany = vi.fn(async () => [
{ id: "check-1", checkName: "ci/build", branch: "main" },
{ id: "check-2", checkName: "ci/build", branch: "main" },
]);
const response = await mergeGatesGet({
params: { owner: "owner-1", repo: "demo" },
locals: { user: { id: "user-1", isAdmin: false } },
request: new Request("http://localhost/api/repos/owner-1/demo/merge-gates"),
} as any);
const body = await readJson(response);
expect(response.status).toBe(200);
expect(body?.data?.requiredChecks).toHaveLength(2);
expect(body?.data?.mergeGates).toHaveLength(1);
expect(body?.data?.report?.requiredChecksTotal).toBe(2);
expect(body?.data?.report?.mergeGatesTotal).toBe(1);
expect(body?.data?.report?.warnings).toHaveLength(2);
expect(body?.data?.report?.warnings?.[0]?.code).toBe("duplicate_required_checks");
expect(body?.data?.report?.warnings?.[0]?.severity).toBe("warning");
expect(body?.data?.report?.warnings?.[0]?.message).toContain("Duplicate required checks");
});
it("returns 401 for unauthenticated reads", async () => {
const response = await mergeGatesGet({
params: { owner: "owner-1", repo: "demo" },
locals: {},
request: new Request("http://localhost/api/repos/owner-1/demo/merge-gates"),
} as any);
expect(response.status).toBe(401);
});
it("returns readiness report when pullNumber is provided", async () => {
evaluateGatesMock.mockResolvedValue({
canMerge: false,
results: [
{ passed: false, gateName: "Status: ci/build", message: "failed" },
{ passed: false, gateName: "Review", message: "Needs 1 more approval(s)" },
],
});
const response = await mergeGatesGet({
params: { owner: "owner-1", repo: "demo" },
locals: { user: { id: "user-1", isAdmin: false } },
request: new Request("http://localhost/api/repos/owner-1/demo/merge-gates?pullNumber=12"),
} as any);
const body = await readJson(response);
expect(response.status).toBe(200);
expect(evaluateGatesMock).toHaveBeenCalledWith("pr-1");
expect(body?.data?.readiness?.canMerge).toBe(false);
expect(body?.data?.readinessReport?.failedGateCount).toBe(2);
expect(body?.data?.readinessReport?.failedGateNames).toEqual(["Status: ci/build", "Review"]);
expect(body?.data?.readinessReport?.recommendations).toContain(
"Re-run or fix failing required status checks."
);
});
it("validates pullNumber query parameter", async () => {
const response = await mergeGatesGet({
params: { owner: "owner-1", repo: "demo" },
locals: { user: { id: "user-1", isAdmin: false } },
request: new Request("http://localhost/api/repos/owner-1/demo/merge-gates?pullNumber=abc"),
} as any);
expect(response.status).toBe(400);
});
it("creates required check for admins", async () => {
const response = await mergeGatesPost({
params: { owner: "owner-1", repo: "demo" },
locals: { user: { id: "user-1", isAdmin: false } },
request: new Request("http://localhost/api/repos/owner-1/demo/merge-gates", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
kind: "required_check",
branch: "main",
checkName: "ci/build",
}),
}),
} as any);
expect(response.status).toBe(200);
expect(addRequiredCheckMock).toHaveBeenCalledWith({
repositoryId: "repo-1",
branch: "main",
checkName: "ci/build",
strictMode: undefined,
});
});
it("creates merge gate for admins", async () => {
const response = await mergeGatesPost({
params: { owner: "owner-1", repo: "demo" },
locals: { user: { id: "user-1", isAdmin: false } },
request: new Request("http://localhost/api/repos/owner-1/demo/merge-gates", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
kind: "merge_gate",
name: "PR Label Gate",
gateType: "label",
config: { required: "ready-to-merge" },
}),
}),
} as any);
expect(response.status).toBe(200);
expect(createMergeGateMock).toHaveBeenCalledWith({
repositoryId: "repo-1",
name: "PR Label Gate",
description: undefined,
gateType: "label",
config: { required: "ready-to-merge" },
conditionScript: undefined,
});
});
it("returns 403 when non-admin attempts write", async () => {
canAdminRepoMock.mockResolvedValue(false);
const response = await mergeGatesPost({
params: { owner: "owner-1", repo: "demo" },
locals: { user: { id: "user-1", isAdmin: false } },
request: new Request("http://localhost/api/repos/owner-1/demo/merge-gates", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
kind: "required_check",
branch: "main",
checkName: "ci/build",
}),
}),
} as any);
expect(response.status).toBe(403);
expect(addRequiredCheckMock).not.toHaveBeenCalled();
expect(createMergeGateMock).not.toHaveBeenCalled();
});
it("returns 400 for invalid payload", async () => {
const response = await mergeGatesPost({
params: { owner: "owner-1", repo: "demo" },
locals: { user: { id: "user-1", isAdmin: false } },
request: new Request("http://localhost/api/repos/owner-1/demo/merge-gates", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
kind: "merge_gate",
gateType: "label",
}),
}),
} as any);
expect(response.status).toBe(400);
expect(createMergeGateMock).not.toHaveBeenCalled();
});
});