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
/**
* Repository Mirror Sync Library
* Handles background synchronization of mirrored repositories
*/
import { getDatabase, schema } from "@/db";
import { eq } from "drizzle-orm";
import { logger } from "./logger";
import simpleGit, { SimpleGit } from "simple-git";
import path from "path";
const REPOS_BASE_PATH = process.env.REPOS_PATH || path.join(process.cwd(), "data", "repos");
interface SyncResult {
success: boolean;
refsUpdated: number;
error?: string;
}
/**
* Sync a single mirrored repository with its upstream
*/
export async function syncMirrorRepository(repoId: string): Promise<SyncResult> {
const db = getDatabase();
// Get repository
const repo = await db.query.repositories.findFirst({
where: eq(schema.repositories.id, repoId),
});
if (!repo) {
return { success: false, refsUpdated: 0, error: "Repository not found" };
}
if (!repo.isMirror || !repo.mirrorUrl) {
return { success: false, refsUpdated: 0, error: "Not a mirror repository" };
}
const repoPath = path.join(REPOS_BASE_PATH, repo.diskPath);
try {
// Mark as syncing
// @ts-expect-error - Drizzle multi-db union type issue
await db.update(schema.repositories)
.set({ mirrorSyncStatus: "syncing" })
.where(eq(schema.repositories.id, repoId));
const git: SimpleGit = simpleGit(repoPath);
// Fetch all refs from upstream with prune
logger.info({ repoId, mirrorUrl: repo.mirrorUrl }, "Starting mirror sync");
const fetchResult = await git.fetch(["--all", "--prune", "--tags"]);
// Count updated refs (simplified - fetch doesn't return detailed info easily)
const refsUpdated = fetchResult.updated?.length || 0;
// Update sync status
// @ts-expect-error - Drizzle multi-db union type issue
await db.update(schema.repositories)
.set({
mirrorSyncStatus: "success",
lastMirrorSyncAt: new Date(),
updatedAt: new Date(),
})
.where(eq(schema.repositories.id, repoId));
logger.info({ repoId, refsUpdated }, "Mirror sync completed");
return { success: true, refsUpdated };
} catch (error) {
const errorMessage = error instanceof Error ? error.message : "Unknown error";
logger.error({ repoId, error: errorMessage }, "Mirror sync failed");
// @ts-expect-error - Drizzle multi-db union type issue
await db.update(schema.repositories)
.set({ mirrorSyncStatus: "failed" })
.where(eq(schema.repositories.id, repoId));
return { success: false, refsUpdated: 0, error: errorMessage };
}
}
/**
* Sync all mirror repositories
*/
export async function syncAllMirrors(): Promise<{ synced: number; failed: number }> {
const db = getDatabase();
const mirrors = await db.query.repositories.findMany({
where: eq(schema.repositories.isMirror, true),
});
let synced = 0;
let failed = 0;
for (const mirror of mirrors) {
const result = await syncMirrorRepository(mirror.id);
if (result.success) {
synced++;
} else {
failed++;
}
}
logger.info({ synced, failed, total: mirrors.length }, "Mirror sync batch completed");
return { synced, failed };
}
/**
* Initialize a new mirror repository
*/
export async function initializeMirror(repoId: string, mirrorUrl: string): Promise<SyncResult> {
const db = getDatabase();
const repo = await db.query.repositories.findFirst({
where: eq(schema.repositories.id, repoId),
});
if (!repo) {
return { success: false, refsUpdated: 0, error: "Repository not found" };
}
const repoPath = path.join(REPOS_BASE_PATH, repo.diskPath);
try {
const git: SimpleGit = simpleGit(repoPath);
// Add upstream remote
await git.addRemote("upstream", mirrorUrl);
// Mark as mirror in DB
// @ts-expect-error - Drizzle multi-db union type issue
await db.update(schema.repositories)
.set({
isMirror: true,
mirrorUrl: mirrorUrl,
mirrorSyncStatus: "pending",
})
.where(eq(schema.repositories.id, repoId));
// Perform initial sync
return await syncMirrorRepository(repoId);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : "Unknown error";
return { success: false, refsUpdated: 0, error: errorMessage };
}
}