Repository navigation
Expand file tree
/
Copy pathgithubSyncService.ts
More file actions
395 lines (354 loc) Β· 12.9 KB
/
Copy pathgithubSyncService.ts
File metadata and controls
395 lines (354 loc) Β· 12.9 KB
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
import fs from 'fs';
import path from 'path';
import os from 'os';
import { exec } from 'child_process';
import util from 'util';
const execPromise = util.promisify(exec);
export interface GitHubSyncTarget {
repo: string;
token: string;
}
export interface SyncFileInput {
path: string; // Repository relative path e.g. "leetcode/LeetCode_Daily_Report_2026-10-19.csv"
content?: string | Buffer;
localFilePath?: string;
}
/**
* Returns all configured GitHub repository sync targets.
* Ensures both TK4743/taskmanager and Tharun4743/taskmanager stay in 100% parity.
*/
export function getGitHubSyncTargets(): GitHubSyncTarget[] {
const targets: GitHubSyncTarget[] = [];
const seenRepos = new Set<string>();
// 1. Explicit tokens configured via GitHub Workflow secrets or environment
const tkToken = process.env.GITHUB_TOKEN_TK4743;
if (tkToken) {
targets.push({ repo: 'TK4743/taskmanager', token: tkToken });
seenRepos.add('TK4743/taskmanager');
}
const tharunToken = process.env.GITHUB_TOKEN_THARUN;
if (tharunToken) {
targets.push({ repo: 'Tharun4743/taskmanager', token: tharunToken });
seenRepos.add('Tharun4743/taskmanager');
}
// 2. Standard GITHUB_TOKEN (e.g. from GitHub Actions secrets.GITHUB_TOKEN or env)
const defaultToken = process.env.GITHUB_TOKEN;
if (defaultToken) {
const targetRepo = process.env.GITHUB_REPOSITORY || process.env.GITHUB_REPO || 'Tharun4743/taskmanager';
if (!seenRepos.has(targetRepo)) {
targets.push({ repo: targetRepo, token: defaultToken });
seenRepos.add(targetRepo);
}
}
return targets;
}
/**
* Safely writes a file to disk:
* First attempts process.cwd() (for local and persistent servers).
* If process.cwd() is read-only (like Vercel serverless /var/task), falls back to os.tmpdir().
*/
export function safeWriteExportFile(relPath: string, content: string | Buffer): { absPath: string; isTemp: boolean } {
const cleanRel = relPath.replace(/\\/g, '/').replace(/^\/+/, '');
const primaryPath = path.join(process.cwd(), cleanRel);
try {
const dir = path.dirname(primaryPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(primaryPath, content);
return { absPath: primaryPath, isTemp: false };
} catch (err: any) {
// Read-only filesystem (e.g. Vercel / AWS Lambda) -> write to tmpdir
const tmpPath = path.join(os.tmpdir(), cleanRel);
const tmpDir = path.dirname(tmpPath);
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, { recursive: true });
}
fs.writeFileSync(tmpPath, content);
return { absPath: tmpPath, isTemp: true };
}
}
/**
* Uploads/commits multiple files atomically to all GitHub sync target repositories.
* Uses GitHub Git Database API (Blobs -> Tree -> Commit -> Ref Update)
* to avoid 409 commit collisions and reduce multiple file commits to a single clean commit.
*/
export async function commitFilesToGitHub(
files: SyncFileInput[],
commitMsg: string
): Promise<Record<string, boolean>> {
const targets = getGitHubSyncTargets();
const results: Record<string, boolean> = {};
if (targets.length === 0 || files.length === 0) {
return results;
}
// Resolve content for each file
const preparedFiles: Array<{ path: string; base64: string }> = [];
for (const f of files) {
const cleanPath = f.path.replace(/\\/g, '/').replace(/^\/+/, '');
let base64 = '';
if (f.content !== undefined) {
base64 = Buffer.isBuffer(f.content)
? f.content.toString('base64')
: Buffer.from(f.content, 'utf-8').toString('base64');
} else if (f.localFilePath && fs.existsSync(f.localFilePath)) {
base64 = fs.readFileSync(f.localFilePath).toString('base64');
}
if (base64) {
preparedFiles.push({ path: cleanPath, base64 });
}
}
if (preparedFiles.length === 0) {
return results;
}
for (const target of targets) {
try {
const headers = {
Authorization: `Bearer ${target.token}`,
Accept: 'application/vnd.github.v3+json',
'User-Agent': 'IT-TaskManager-Sync',
'Content-Type': 'application/json'
};
// 1. Get HEAD commit sha of main branch
const refRes = await fetch(`https://api.github.com/repos/${target.repo}/git/ref/heads/main`, {
headers,
signal: AbortSignal.timeout(20000)
});
if (!refRes.ok) {
console.warn(`[GitHub DualSync] Failed to fetch HEAD for ${target.repo} (${refRes.status}):`, await refRes.text());
results[target.repo] = false;
continue;
}
const refData = (await refRes.json()) as { object: { sha: string } };
const commitSha = refData.object.sha;
// 2. Get base tree sha
const commitRes = await fetch(`https://api.github.com/repos/${target.repo}/git/commits/${commitSha}`, {
headers,
signal: AbortSignal.timeout(20000)
});
if (!commitRes.ok) {
console.warn(`[GitHub DualSync] Failed to fetch commit tree for ${target.repo}:`, await commitRes.text());
results[target.repo] = false;
continue;
}
const commitData = (await commitRes.json()) as { tree: { sha: string } };
const baseTreeSha = commitData.tree.sha;
// 3. Create Blobs for each file
const treeEntries: Array<{ path: string; mode: string; type: string; sha: string }> = [];
for (const item of preparedFiles) {
const blobRes = await fetch(`https://api.github.com/repos/${target.repo}/git/blobs`, {
method: 'POST',
headers,
body: JSON.stringify({
content: item.base64,
encoding: 'base64'
}),
signal: AbortSignal.timeout(120000)
});
if (!blobRes.ok) {
throw new Error(`Blob creation failed for ${item.path} on ${target.repo}: ${await blobRes.text()}`);
}
const blobData = (await blobRes.json()) as { sha: string };
treeEntries.push({
path: item.path,
mode: '100644',
type: 'blob',
sha: blobData.sha
});
}
// 4. Create new Git Tree
const treeRes = await fetch(`https://api.github.com/repos/${target.repo}/git/trees`, {
method: 'POST',
headers,
body: JSON.stringify({
base_tree: baseTreeSha,
tree: treeEntries
}),
signal: AbortSignal.timeout(25000)
});
if (!treeRes.ok) {
throw new Error(`Tree creation failed on ${target.repo}: ${await treeRes.text()}`);
}
const treeData = (await treeRes.json()) as { sha: string };
const newTreeSha = treeData.sha;
// 5. Create new Commit
const newCommitRes = await fetch(`https://api.github.com/repos/${target.repo}/git/commits`, {
method: 'POST',
headers,
body: JSON.stringify({
message: commitMsg,
tree: newTreeSha,
parents: [commitSha]
}),
signal: AbortSignal.timeout(25000)
});
if (!newCommitRes.ok) {
throw new Error(`Commit creation failed on ${target.repo}: ${await newCommitRes.text()}`);
}
const newCommitData = (await newCommitRes.json()) as { sha: string };
const newCommitSha = newCommitData.sha;
// 6. Update reference refs/heads/main
const patchRes = await fetch(`https://api.github.com/repos/${target.repo}/git/refs/heads/main`, {
method: 'PATCH',
headers,
body: JSON.stringify({
sha: newCommitSha,
force: false
}),
signal: AbortSignal.timeout(25000)
});
if (!patchRes.ok) {
throw new Error(`Ref update failed on ${target.repo}: ${await patchRes.text()}`);
}
console.log(`[GitHub DualSync] π Auto-pushed ${preparedFiles.length} file(s) to ${target.repo} via Git Tree API (${newCommitSha.slice(0, 7)}).`);
results[target.repo] = true;
} catch (err: any) {
console.warn(`[GitHub DualSync] Error updating ${target.repo} via Git Tree API:`, err.message);
// Fallback: try individual file upload via Contents API
let targetSuccess = true;
for (const item of preparedFiles) {
const ok = await updateSingleFileViaContentsAPI(target, item.path, item.base64, commitMsg);
if (!ok) targetSuccess = false;
}
results[target.repo] = targetSuccess;
}
}
return results;
}
/**
* Fallback / single-file update via GitHub Contents REST API
*/
async function updateSingleFileViaContentsAPI(
target: GitHubSyncTarget,
cleanPath: string,
base64Content: string,
commitMsg: string
): Promise<boolean> {
const url = `https://api.github.com/repos/${target.repo}/contents/${cleanPath}`;
const headers = {
Authorization: `Bearer ${target.token}`,
Accept: 'application/vnd.github.v3+json',
'User-Agent': 'IT-TaskManager-Sync',
'Content-Type': 'application/json'
};
try {
let sha: string | undefined;
const getRes = await fetch(url, { headers, signal: AbortSignal.timeout(15000) });
if (getRes.ok) {
const json = (await getRes.json()) as { sha: string };
sha = json.sha;
}
const putRes = await fetch(url, {
method: 'PUT',
headers,
body: JSON.stringify({
message: commitMsg,
content: base64Content,
sha,
branch: 'main'
}),
signal: AbortSignal.timeout(120000)
});
if (putRes.ok) {
console.log(`[GitHub Sync] π Updated ${cleanPath} on ${target.repo} via Contents API.`);
return true;
} else {
const errText = await putRes.text();
console.warn(`[GitHub Sync] Contents API error on ${target.repo} (${putRes.status}):`, errText);
return false;
}
} catch (e: any) {
console.warn(`[GitHub Sync] Error on ${target.repo}:`, e.message);
return false;
}
}
/**
* Updates a single file across all configured GitHub repositories.
* Backward compatible signature with existing updateGitHubFileViaAPI.
*/
export async function updateGitHubFileViaAPI(
filePathOrRelative: string,
commitMsg: string,
explicitContent?: string | Buffer
): Promise<boolean> {
let content: string | Buffer | undefined = explicitContent;
let relPath = filePathOrRelative;
if (content === undefined) {
if (fs.existsSync(filePathOrRelative)) {
content = fs.readFileSync(filePathOrRelative);
relPath = path.relative(process.cwd(), filePathOrRelative).replace(/\\/g, '/');
} else {
const tmpPath = path.join(os.tmpdir(), filePathOrRelative);
if (fs.existsSync(tmpPath)) {
content = fs.readFileSync(tmpPath);
relPath = filePathOrRelative.replace(/\\/g, '/');
} else {
console.warn(`[GitHub Sync] File does not exist: ${filePathOrRelative}`);
return false;
}
}
}
const results = await commitFilesToGitHub([{ path: relPath, content }], commitMsg);
return Object.values(results).some(Boolean);
}
/**
* Deletes a file across all configured GitHub repositories via Contents API.
*/
export async function deleteGitHubFileViaAPI(
relativePath: string,
commitMsg: string
): Promise<boolean> {
const targets = getGitHubSyncTargets();
const cleanPath = relativePath.replace(/\\/g, '/').replace(/^\/+/, '');
let anySuccess = false;
for (const target of targets) {
const url = `https://api.github.com/repos/${target.repo}/contents/${cleanPath}`;
const headers = {
Authorization: `Bearer ${target.token}`,
Accept: 'application/vnd.github.v3+json',
'User-Agent': 'IT-TaskManager-Sync',
'Content-Type': 'application/json'
};
try {
const getRes = await fetch(url, { headers, signal: AbortSignal.timeout(15000) });
if (!getRes.ok) continue;
const json = (await getRes.json()) as { sha: string };
const delRes = await fetch(url, {
method: 'DELETE',
headers,
body: JSON.stringify({
message: commitMsg,
sha: json.sha,
branch: 'main'
}),
signal: AbortSignal.timeout(15000)
});
if (delRes.ok) {
console.log(`[GitHub Sync] ποΈ Deleted ${cleanPath} on ${target.repo}`);
anySuccess = true;
}
} catch (e: any) {
console.warn(`[GitHub Sync] Failed to delete ${cleanPath} on ${target.repo}:`, e.message);
}
}
return anySuccess;
}
/**
* Executes local git push if running on a persistent environment with Git CLI.
*/
export async function tryLocalGitPush(folderPath: string, commitMsg: string): Promise<boolean> {
try {
await execPromise(`git add ${folderPath}`);
const statusRes = await execPromise(`git status --porcelain ${folderPath}`);
if (statusRes.stdout.trim()) {
await execPromise(`git commit -m "${commitMsg}"`);
await execPromise('git push origin main');
console.log(`[Local Git] π Auto-pushed ${folderPath} to GitHub via Git CLI: ${commitMsg}`);
return true;
}
return false;
} catch (err: any) {
// Non-blocking in cloud/serverless environments where Git CLI is not configured
return false;
}
}