Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesPull RequestsActionsSecurityInsightsSettings
✨ AI
More
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
import-helper.ts8.1 KB · 258 lines
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
/**
 * Small helpers for the GitHub import flow (/import).
 *
 * Pure parsing/normalization helpers live at the top of the file.
 * `importOneRepo` at the bottom wraps the clone + DB insert so that both
 * the single-repo and bulk importers can share one code path.
 */

import { and, eq } from "drizzle-orm";
import { mkdir, rm } from "fs/promises";
import { join } from "path";
import { db } from "../db";
import { repositories } from "../db/schema";
import { config } from "../lib/config";

/**
 * Count commits across ALL refs in a bare repo. Returns 0 for an empty repo
 * (no refs / unborn HEAD) or on any git error.
 *
 * This is the gate that makes an import trustworthy: `git clone --bare
 * --mirror` of an empty, unreachable, or private-without-a-valid-token
 * source EXITS 0 yet produces a repo with zero objects. Without this check
 * the import inserted a DB row and reported "success" for a phantom empty
 * repo — which is how a Vapron import "succeeded" while transferring nothing,
 * a silent failure that's actively dangerous once the user deletes the
 * GitHub source trusting the import worked.
 */
export async function countRepoCommits(destPath: string): Promise<number> {
  try {
    const proc = Bun.spawn(
      ["git", "-C", destPath, "rev-list", "--all", "--count"],
      {
        stdout: "pipe",
        stderr: "pipe",
        env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
      }
    );
    const out = await new Response(proc.stdout).text();
    const code = await proc.exited;
    if (code !== 0) return 0;
    const n = parseInt(out.trim(), 10);
    return Number.isFinite(n) && n > 0 ? n : 0;
  } catch {
    return 0;
  }
}

export interface ParsedGithubUrl {
  owner: string;
  repo: string;
}

/**
 * Parse a GitHub URL into { owner, repo }. Accepts:
 *   - https://github.com/foo/bar
 *   - https://github.com/foo/bar.git
 *   - http://github.com/foo/bar/
 *   - git@github.com:foo/bar.git
 *   - github.com/foo/bar
 *   - foo/bar
 *
 * Returns null if the URL cannot be parsed.
 */
export function parseGithubUrl(raw: string): ParsedGithubUrl | null {
  const input = (raw || "").trim();
  if (!input) return null;

  // SSH form: git@github.com:owner/repo(.git)?
  const ssh = input.match(/^git@github\.com:([^/]+)\/([^/\s]+?)(?:\.git)?\/?$/i);
  if (ssh) return { owner: ssh[1], repo: stripDotGit(ssh[2]) };

  // HTTP(S) / bare host form
  const http = input.match(
    /^(?:https?:\/\/)?(?:www\.)?github\.com\/([^/\s]+)\/([^/\s?#]+?)(?:\.git)?\/?(?:[?#].*)?$/i
  );
  if (http) return { owner: http[1], repo: stripDotGit(http[2]) };

  // owner/repo shorthand
  const short = input.match(/^([^/\s]+)\/([^/\s]+?)(?:\.git)?$/);
  if (short) return { owner: short[1], repo: stripDotGit(short[2]) };

  return null;
}

function stripDotGit(name: string): string {
  return name.replace(/\.git$/i, "");
}

/**
 * Repository names on gluecron follow GitHub's rough rules: letters,
 * digits, hyphens, underscores, dots. We normalize by replacing anything
 * else with a hyphen so an imported repo is always addressable.
 */
export function sanitizeRepoName(name: string): string {
  const cleaned = name.replace(/[^A-Za-z0-9._-]/g, "-").replace(/^-+|-+$/g, "");
  return cleaned || "imported-repo";
}

/**
 * Build the clone URL that `git clone --bare --mirror` will use. When a
 * token is supplied we inject it so private repos are reachable.
 */
export function buildCloneUrl(cloneUrl: string, token: string | null): string {
  if (!token) return cloneUrl;
  return cloneUrl.replace("https://github.com/", `https://${token}@github.com/`);
}

/**
 * Strip any secret (token) from a string before it gets returned to the UI
 * or written to logs. Defense in depth: we already avoid putting tokens into
 * messages, but git/HTTP errors may echo the URL we passed in.
 */
export function scrubSecrets(input: string, token: string | null): string {
  if (!input) return input;
  let out = input;
  if (token) out = out.split(token).join("***");
  // Also redact any `https://<creds>@github.com/...` form the URL may leak.
  out = out.replace(
    /https:\/\/[^@\s]+@github\.com/gi,
    "https://***@github.com"
  );
  return out;
}

export interface ImportOneRepoInput {
  cloneUrl: string;
  targetName: string;
  ownerId: string;
  ownerUsername: string;
  token?: string | null;
  description?: string | null;
  isPrivate?: boolean;
  defaultBranch?: string;
}

export type ImportOneRepoStatus = "success" | "skipped-exists" | "failed";

export interface ImportOneRepoResult {
  status: ImportOneRepoStatus;
  name: string;
  notes: string;
}

/**
 * Clone one GitHub repo into this user's namespace and insert the DB row.
 *
 * Resilient: returns a result object instead of throwing, so bulk callers
 * can continue past a failure. Never includes the token in the returned
 * notes — all output is passed through `scrubSecrets`.
 */
export async function importOneRepo(
  input: ImportOneRepoInput
): Promise<ImportOneRepoResult> {
  const {
    cloneUrl,
    targetName,
    ownerId,
    ownerUsername,
    token = null,
    description = null,
    isPrivate = false,
    defaultBranch = "main",
  } = input;

  const safeName = sanitizeRepoName(targetName);

  try {
    // Uniqueness in the caller's namespace (owner+name).
    const [existing] = await db
      .select()
      .from(repositories)
      .where(
        and(eq(repositories.ownerId, ownerId), eq(repositories.name, safeName))
      )
      .limit(1);

    if (existing) {
      // A prior import can leave an EMPTY repo behind (see countRepoCommits).
      // Don't cheerfully report "already exists" for a broken empty shell —
      // tell the user it's empty so they can delete + re-import to populate it.
      const existingCommits = await countRepoCommits(existing.diskPath);
      if (existingCommits === 0) {
        return {
          status: "failed",
          name: safeName,
          notes:
            "A repo by this name already exists but is EMPTY — a previous import left an empty shell. Delete it and re-import to populate it.",
        };
      }
      return {
        status: "skipped-exists",
        name: safeName,
        notes: `Already exists in your namespace (${existingCommits} commits)`,
      };
    }

    const destPath = join(config.gitReposPath, ownerUsername, `${safeName}.git`);
    await mkdir(join(config.gitReposPath, ownerUsername), { recursive: true });

    const authedCloneUrl = buildCloneUrl(cloneUrl, token);

    const proc = Bun.spawn(
      ["git", "clone", "--bare", "--mirror", authedCloneUrl, destPath],
      {
        stdout: "pipe",
        stderr: "pipe",
        env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
      }
    );
    const stderr = await new Response(proc.stderr).text();
    const exitCode = await proc.exited;

    if (exitCode !== 0) {
      return {
        status: "failed",
        name: safeName,
        notes: `git clone failed: ${scrubSecrets(stderr, token).slice(0, 200)}`,
      };
    }

    // Gate success on the clone having actually transferred history. `git
    // clone --mirror` can exit 0 yet leave an empty repo; without this check
    // that got a DB row + "success" — a phantom empty repo. Clean up the
    // empty dir (so a retry isn't blocked by "already exists") and fail loud.
    const commitCount = await countRepoCommits(destPath);
    if (commitCount === 0) {
      await rm(destPath, { recursive: true, force: true }).catch(() => {});
      return {
        status: "failed",
        name: safeName,
        notes:
          "Clone produced an EMPTY repository — nothing was imported. The source may be empty, or private and the token lacks access. No repo was created; fix access and retry.",
      };
    }

    await db.insert(repositories).values({
      name: safeName,
      ownerId,
      description,
      isPrivate,
      defaultBranch: defaultBranch || "main",
      diskPath: destPath,
      starCount: 0,
    });

    return {
      status: "success",
      name: safeName,
      notes: `Cloned + indexed (${commitCount} commits)`,
    };
  } catch (err) {
    return {
      status: "failed",
      name: safeName,
      notes: scrubSecrets(String(err), token).slice(0, 200),
    };
  }
}