Spaces:
Paused
Paused
Add GitHub sync service (part 3)
Browse files
server/src/services/githubSyncService.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { execSync } from 'child_process';
|
| 2 |
+
import fs from 'fs';
|
| 3 |
+
import path from 'path';
|
| 4 |
+
import { logInfo, logError } from './auditLog';
|
| 5 |
+
import { db } from '../db/database';
|
| 6 |
+
|
| 7 |
+
const REPO_URL = 'https://github.com/JsonLord/TREK.git';
|
| 8 |
+
const BRANCH = 'custom-data';
|
| 9 |
+
const dataDir = path.join(__dirname, '../../data');
|
| 10 |
+
const dbPath = path.join(dataDir, 'travel.db');
|
| 11 |
+
const syncDir = path.join(dataDir, 'github-sync');
|
| 12 |
+
|
| 13 |
+
export async function runGithubSync(): Promise<void> {
|
| 14 |
+
const token = process.env.PAT_TOKEN;
|
| 15 |
+
if (!token) {
|
| 16 |
+
logError('GitHub Sync: PAT_TOKEN not found in environment');
|
| 17 |
+
return;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
const authenticatedRepoUrl = `https://x-access-token:${token}@github.com/JsonLord/TREK.git`;
|
| 21 |
+
|
| 22 |
+
try {
|
| 23 |
+
if (!fs.existsSync(syncDir)) {
|
| 24 |
+
logInfo(`GitHub Sync: Cloning repository into ${syncDir}`);
|
| 25 |
+
execSync(`git clone -b ${BRANCH} ${authenticatedRepoUrl} ${syncDir}`, { stdio: 'ignore' });
|
| 26 |
+
} else {
|
| 27 |
+
logInfo('GitHub Sync: Pulling latest changes');
|
| 28 |
+
execSync(`git -C ${syncDir} pull origin ${BRANCH}`, { stdio: 'ignore' });
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// Flush WAL to main DB file before copying
|
| 32 |
+
try {
|
| 33 |
+
db.exec('PRAGMA wal_checkpoint(TRUNCATE)');
|
| 34 |
+
} catch (e) {
|
| 35 |
+
logError(`GitHub Sync: Failed to flush WAL: ${e instanceof Error ? e.message : e}`);
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
const destDbPath = path.join(syncDir, 'travel.db');
|
| 39 |
+
fs.copyFileSync(dbPath, destDbPath);
|
| 40 |
+
|
| 41 |
+
// Check if there are changes
|
| 42 |
+
const status = execSync(`git -C ${syncDir} status --porcelain`, { encoding: 'utf8' });
|
| 43 |
+
if (!status) {
|
| 44 |
+
logInfo('GitHub Sync: No changes to push');
|
| 45 |
+
return;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
logInfo('GitHub Sync: Committing and pushing changes');
|
| 49 |
+
execSync(`git -C ${syncDir} add travel.db`, { stdio: 'ignore' });
|
| 50 |
+
execSync(`git -C ${syncDir} -c user.name="TREK Sync" -c user.email="sync@trek.local" commit -m "Update database: ${new Date().toISOString()}"`, { stdio: 'ignore' });
|
| 51 |
+
execSync(`git -C ${syncDir} push origin ${BRANCH}`, { stdio: 'ignore' });
|
| 52 |
+
logInfo('GitHub Sync: Success');
|
| 53 |
+
|
| 54 |
+
} catch (err: unknown) {
|
| 55 |
+
logError(`GitHub Sync failed: ${err instanceof Error ? err.message : err}`);
|
| 56 |
+
}
|
| 57 |
+
}
|