swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
64 lines · 1.8 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
import { getDatabase, schema } from "@/db";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { logger } from "@/lib/logger";
import { resolveRepoPath } from "@/lib/git-storage";
import type { APIRoute } from "astro";
import { spawn } from "child_process";
// removed

// const exec = default... removed

export const GET: APIRoute = async ({ request }) => {
  const authHeader = request.headers.get("Authorization");
  const cronSecret = process.env.CRON_SECRET;
  if (!cronSecret) {
    return new Response("Server misconfigured", { status: 500 });
  }

  if (authHeader !== `Bearer ${cronSecret}`) {
    return new Response("Unauthorized", { status: 401 });
  }

  const db = getDatabase() as NodePgDatabase<typeof schema>;
  const repos = await db.query.repositories.findMany();

  logger.info({ count: repos.length }, "Starting garbage collection");

  let successCount = 0;
  let errorCount = 0;

  for (const repo of repos) {
    try {
      logger.debug({ owner: repo.ownerId, name: repo.name }, "Processing repository");
      // Run git gc --auto --quiet
      const repoPath = await resolveRepoPath(repo.diskPath);
      const child = spawn("git", ["gc", "--auto", "--quiet"], {
        cwd: repoPath,
        stdio: "ignore",
      });

      await new Promise<void>((resolve, reject) => {
        child.on("close", (code) => {
          if (code === 0) resolve();
          else reject(new Error(`Exit code ${code}`));
        });
        child.on("error", reject);
      });

      successCount++;
    } catch (e) {
      logger.error({ err: e, repo: repo.name }, "GC error");
      errorCount++;
    }
  }

  logger.info({ success: successCount, errors: errorCount }, "GC completed");

  return new Response(
    JSON.stringify({ success: successCount, errors: errorCount }),
    {
      headers: { "Content-Type": "application/json" },
    }
  );
};