swadhinbiswas feat: optimize Docker CI/CD builds, refine site-wide UI aesthetic, and remediate secrets · 8/19/2026
c3e5c18
72 lines · 3.2 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
import "dotenv/config";
import { getDb } from "../src/lib/db/index.js";
import { instances, cachedRepos, cachedUsers } from "../src/lib/db/schema.js";
import { fetchAllPublicRepos } from "../src/lib/instance-client.js";
import { eq } from "drizzle-orm";
import { nanoid } from "nanoid";

async function runSync() {
  console.log("[Sync] Starting global instance synchronization...");
  const db = getDb() as any;
  
  const allInstances = await db.query.instances.findMany();
  console.log(`[Sync] Found ${allInstances.length} instances to sync.`);

  for (const instance of allInstances) {
    console.log(`[Sync] Syncing instance: ${instance.name} (${instance.url})`);
    try {
      const repos = await fetchAllPublicRepos(instance.url);
      const ownersMap = new Map();

      for (const r of repos) {
        if (r.owner && !ownersMap.has(r.owner.username)) {
          ownersMap.set(r.owner.username, r.owner);
        }

        const existing = await db.query.cachedRepos.findFirst({ where: eq(cachedRepos.remoteId, r.id) }).catch(()=>null);
        if (existing) {
          await db.update(cachedRepos).set({ 
            fullName: r.fullName, name: r.name, description: r.description, language: r.language, 
            topics: JSON.stringify(r.topics), starCount: r.starCount, forkCount: r.forkCount, 
            httpCloneUrl: r.httpCloneUrl, updatedAt: r.updatedAt 
          }).where(eq(cachedRepos.id, existing.id));
        } else {
          await db.insert(cachedRepos).values({ 
            id: nanoid(), instanceId: instance.id, remoteId: r.id, fullName: r.fullName, name: r.name, 
            ownerUsername: r.owner.username, ownerDisplayName: r.owner.displayName, ownerAvatarUrl: r.owner.avatarUrl, 
            description: r.description, visibility: r.visibility, language: r.language, topics: JSON.stringify(r.topics), 
            starCount: r.starCount, forkCount: r.forkCount, httpCloneUrl: r.httpCloneUrl, updatedAt: r.updatedAt 
          });
        }
      }

      for (const owner of ownersMap.values()) {
        const existingUser = await db.query.cachedUsers.findFirst({ where: eq(cachedUsers.username, owner.username) }).catch(() => null);
        if (existingUser) {
          await db.update(cachedUsers).set({
            displayName: owner.displayName, avatarUrl: owner.avatarUrl,
          }).where(eq(cachedUsers.id, existingUser.id));
        } else {
          await db.insert(cachedUsers).values({
            id: nanoid(), instanceId: instance.id, remoteId: owner.id || owner.username,
            username: owner.username, displayName: owner.displayName, avatarUrl: owner.avatarUrl,
          });
        }
      }

      await db.update(instances).set({ 
        repoCount: repos.length, status: "online", lastSyncAt: new Date().toISOString() 
      }).where(eq(instances.id, instance.id));
      
      console.log(`[Sync] ✓ Success: ${instance.name} (${repos.length} repos updated)`);
    } catch (e: any) {
      console.error(`[Sync] ✗ Failed: ${instance.name} - ${e.message}`);
      await db.update(instances).set({ status: "error" }).where(eq(instances.id, instance.id)).catch(()=>{});
    }
  }
  console.log("[Sync] Global synchronization complete.");
  process.exit(0);
}

runSync();