swadhinbiswas feat: finalize platform polish - discussions, gists, webhook queue, realtime, push-mirror, auth/metrics hardening · 8/28/2026
f02976d
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const {
resolveRepoPathMock,
validateGitCloneUrlMock,
createSimpleGitMock,
rawMock,
} = vi.hoisted(() => ({
resolveRepoPathMock: vi.fn(async () => "/data/repos/demo.git"),
validateGitCloneUrlMock: vi.fn(async (): Promise<{ valid: true } | { valid: false; reason: string }> => ({ valid: true })),
createSimpleGitMock: vi.fn(),
rawMock: vi.fn(async () => ""),
}));
vi.mock("@/db", () => ({
getDatabase: () => mockDb,
schema: {},
}));
vi.mock("@/lib/git-storage", () => ({
resolveRepoPath: resolveRepoPathMock,
}));
vi.mock("@/lib/url-validator", () => ({
validateGitCloneUrl: validateGitCloneUrlMock,
}));
vi.mock("@/lib/workflow-secret-crypto", () => ({
encryptWorkflowSecret: (value: string) => `enc:${value}`,
}));
vi.mock("@/lib/mirror-sync", () => ({
// Mirrors pull-side behavior: token embedded transiently, never persisted.
buildFetchUrl: (url: string, token: string | null | undefined) =>
token ? `https://oauth2:${token}@dest.example.com/target.git` : url,
}));
vi.mock("@/lib/git", () => ({
createSimpleGit: createSimpleGitMock,
}));
import {
configurePushMirror,
getPushMirror,
processDuePushMirrors,
pushMirrorNow,
redactCredentials,
removePushMirror,
} from "@/lib/push-mirror";
function makeDb() {
const batches: any[][] = [];
const sets: any[] = [];
const takeBatch = () => (batches.length > 0 ? batches.shift()! : []);
const db = {
select: () => ({
from: () => ({
where: () => ({
limit: async () => takeBatch(),
orderBy: () => ({ limit: async () => takeBatch() }),
}),
orderBy: () => ({ limit: async () => takeBatch() }),
}),
}),
update: () => ({
set: (values: Record<string, unknown>) => {
sets.push(values);
return { where: async () => ({}) };
},
}),
};
return { db, batches, sets };
}
let mockDb: any;
const repoRow = {
id: "repo-1",
diskPath: "/data/repos/demo.git",
pushMirrorEnabled: true,
pushMirrorUrl: "https://dest.example.com/target.git",
pushMirrorToken: "enc:tok-123",
};
describe("push-mirror library", () => {
beforeEach(() => {
vi.clearAllMocks();
createSimpleGitMock.mockReturnValue({ raw: rawMock });
rawMock.mockResolvedValue("");
validateGitCloneUrlMock.mockResolvedValue({ valid: true });
mockDb = makeDb().db;
});
afterEach(() => {
vi.unstubAllEnvs();
});
describe("configurePushMirror", () => {
it("rejects URLs failing SSRF validation without touching the database", async () => {
const state = makeDb();
mockDb = state.db;
validateGitCloneUrlMock.mockResolvedValue({
valid: false,
reason: "Scheme \"file:\" is not allowed.",
});
const result = await configurePushMirror("repo-1", { url: "file:///tmp/repo.git" });
expect(result.success).toBe(false);
expect(result.error).toContain("not allowed");
expect(state.sets).toHaveLength(0);
expect(validateGitCloneUrlMock).toHaveBeenCalledWith(
"file:///tmp/repo.git",
false
);
});
it("permits private targets when PUSH_MIRROR_ALLOW_PRIVATE=true", async () => {
const state = makeDb();
mockDb = state.db;
vi.stubEnv("PUSH_MIRROR_ALLOW_PRIVATE", "true");
state.batches.push([{ id: "repo-1" }]);
await configurePushMirror("repo-1", { url: "http://localhost:3000/repo.git" });
expect(validateGitCloneUrlMock).toHaveBeenCalledWith(
"http://localhost:3000/repo.git",
true
);
});
it("encrypts the auth token and stores pending status", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push(
[{ id: "repo-1" }],
[
{
enabled: true,
url: "https://dest.example.com/target.git",
token: "enc:tok-123",
status: "pending",
lastPushMirrorAt: null,
},
]
);
const result = await configurePushMirror("repo-1", {
url: "https://dest.example.com/target.git",
authToken: "tok-123",
});
expect(result.success).toBe(true);
expect(state.sets[0]).toMatchObject({
pushMirrorEnabled: true,
pushMirrorUrl: "https://dest.example.com/target.git",
pushMirrorToken: "enc:tok-123",
pushMirrorStatus: "pending",
});
expect(result.config?.hasToken).toBe(true);
});
it("keeps an existing token when authToken is omitted", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([{ id: "repo-1" }], []);
const result = await configurePushMirror("repo-1", {
url: "https://dest.example.com/target.git",
});
expect(result.success).toBe(true);
expect(state.sets[0].pushMirrorToken).toBeUndefined();
});
it("fails for unknown repository", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([]);
const result = await configurePushMirror("missing", {
url: "https://dest.example.com/target.git",
});
expect(result.success).toBe(false);
expect(result.error).toBe("Repository not found");
});
});
describe("getPushMirror", () => {
it("never exposes the stored token", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([
{
enabled: true,
url: "https://dest.example.com/target.git",
token: "enc:tok-123",
status: "success",
lastPushMirrorAt: new Date("2026-08-01T00:00:00Z"),
},
]);
const config = await getPushMirror("repo-1");
expect(config).toMatchObject({
enabled: true,
url: "https://dest.example.com/target.git",
hasToken: true,
status: "success",
});
expect(config && "token" in config).toBe(false);
});
it("reports hasToken=false without a stored token", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([
{ enabled: true, url: "https://dest.example.com/target.git", token: null, status: null, lastPushMirrorAt: null },
]);
const config = await getPushMirror("repo-1");
expect(config?.hasToken).toBe(false);
});
it("returns null when repository missing", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([]);
expect(await getPushMirror("missing")).toBeNull();
});
});
describe("removePushMirror", () => {
it("clears all push mirror fields", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([{ id: "repo-1" }]);
const result = await removePushMirror("repo-1");
expect(result.success).toBe(true);
expect(state.sets[0]).toMatchObject({
pushMirrorEnabled: false,
pushMirrorUrl: null,
pushMirrorToken: null,
pushMirrorStatus: null,
});
});
it("fails when repository is missing", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([]);
const result = await removePushMirror("missing");
expect(result.success).toBe(false);
expect(result.error).toBe("Repository not found");
});
});
describe("pushMirrorNow", () => {
it("pushes forced heads/tags refspecs without --mirror and records success", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([repoRow]);
rawMock.mockResolvedValue(
"To https://dest.example.com/target.git\n * [new branch] main -> main\n v1 -> v1\n"
);
const result = await pushMirrorNow("repo-1");
expect(result.success).toBe(true);
expect(result.refsUpdated).toBe(2);
const args = rawMock.mock.calls[0][0];
expect(args[0]).toBe("push");
expect(args).toContain("+refs/heads/*:refs/heads/*");
expect(args).toContain("+refs/tags/*:refs/tags/*");
expect(args).not.toContain("--mirror");
// Transient credential injection per attempt
expect(args).toContain("https://oauth2:enc:[email protected]/target.git");
// simple-git block timeout kills the git process (timeout guard)
expect(createSimpleGitMock).toHaveBeenCalledWith(
expect.objectContaining({
baseDir: "/data/repos/demo.git",
timeout: { block: 300_000 },
})
);
// Status transitions: pushing -> success with timestamp
expect(state.sets[0]).toMatchObject({ pushMirrorStatus: "pushing" });
expect(state.sets[1]).toMatchObject({
pushMirrorStatus: "success",
lastPushMirrorAt: expect.any(Date),
});
});
it("does nothing when push mirror is not configured", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([
{ id: "repo-1", diskPath: "/data/repos/demo.git", pushMirrorEnabled: false, pushMirrorUrl: null },
]);
const result = await pushMirrorNow("repo-1");
expect(result.success).toBe(false);
expect(result.error).toBe("Push mirror not configured");
expect(rawMock).not.toHaveBeenCalled();
expect(state.sets).toHaveLength(0);
});
it("marks failed and redacts credentials from git errors", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([repoRow]);
rawMock.mockRejectedValue(
new Error(
"fatal: unable to access 'https://oauth2:enc:[email protected]/target.git/': The requested URL returned error: 403"
)
);
const result = await pushMirrorNow("repo-1");
expect(result.success).toBe(false);
expect(result.error).not.toContain("enc:tok-123");
expect(result.error).toContain("***");
expect(state.sets.at(-1)).toMatchObject({ pushMirrorStatus: "failed" });
});
it("honours PUSH_MIRROR_TIMEOUT_SECS for the process kill guard", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([repoRow]);
vi.stubEnv("PUSH_MIRROR_TIMEOUT_SECS", "42");
await pushMirrorNow("repo-1");
expect(createSimpleGitMock).toHaveBeenCalledWith(
expect.objectContaining({ timeout: { block: 42_000 } })
);
});
it("survives repository lookup failures", async () => {
mockDb = {
select: () => {
throw new Error("db down");
},
update: () => ({ set: () => ({ where: async () => ({}) }) }),
};
const result = await pushMirrorNow("repo-1");
expect(result.success).toBe(false);
expect(result.error).toBe("db down");
});
});
describe("processDuePushMirrors", () => {
it("processes never-pushed repos first, then stale ones, oldest first", async () => {
const state = makeDb();
mockDb = state.db;
// Selection: never-pushed [a,b], stale [c]; then one repo lookup per id.
state.batches.push(
[{ id: "a" }, { id: "b" }],
[{ id: "c" }],
[repoRow],
[repoRow],
[repoRow]
);
rawMock.mockResolvedValue("x -> y\n");
const result = await processDuePushMirrors({});
expect(result.total).toBe(3);
expect(result.pushed).toBe(3);
expect(result.failed).toBe(0);
expect(rawMock).toHaveBeenCalledTimes(3);
});
it("continues after individual failures and reports them", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push(
[{ id: "ok-1" }, { id: "bad-1" }, { id: "ok-2" }],
[],
[repoRow],
[repoRow],
[repoRow]
);
let call = 0;
rawMock.mockImplementation(async () => {
call += 1;
if (call === 2) throw new Error("remote rejected");
return "";
});
const result = await processDuePushMirrors({});
expect(result.pushed).toBe(2);
expect(result.failed).toBe(1);
expect(result.failedRepoIds).toEqual(["bad-1"]);
});
it("respects the limit across both queries", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([{ id: "a" }], [{ id: "c" }]);
rawMock.mockResolvedValue("");
const result = await processDuePushMirrors({ limit: 2 });
expect(result.total).toBe(2);
});
it("uses PUSH_MIRROR_MIN_INTERVAL_SECS default of 300 for staleness cutoff", async () => {
const state = makeDb();
mockDb = state.db;
state.batches.push([], []);
vi.stubEnv("PUSH_MIRROR_MIN_INTERVAL_SECS", "600");
await processDuePushMirrors({});
// No crash + empty selection is enough here; cutoff math is internal.
expect(true).toBe(true);
});
it("never throws when due-selection query fails", async () => {
mockDb = {
select: () => {
throw new Error("db down");
},
};
const result = await processDuePushMirrors({ limit: 5 });
expect(result).toEqual({
total: 0,
eligible: 0,
pushed: 0,
failed: 0,
failedRepoIds: [],
durationMs: expect.any(Number),
});
});
});
describe("redactCredentials", () => {
it("strips userinfo passwords from embedded URLs", () => {
const input = "error fetching https://user:s3cret@host/path and https://oauth2:[email protected]/x";
const output = redactCredentials(input);
expect(output).not.toContain("s3cret");
expect(output).not.toContain("t0k");
expect(output).toContain("https://user:***@host/path");
expect(output).toContain("https://oauth2:***@other.host/x");
});
});
});