Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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
github-import.tsx11.5 KB · 398 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
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
396
397
398
/**
 * Block L — GitHub import UI.
 *
 * GET  /new/import — form (PAT + owner/repo + target name + visibility).
 * POST /new/import — clones the repo via `git clone --mirror`, then walks
 *                    GitHub metadata synchronously. Redirects to the target
 *                    repo page on success.
 *
 * v1 is synchronous: we return when the clone + walk finishes (caps keep
 * this bounded). Later this can be moved to a background worker.
 */

import { Hono } from "hono";
import { desc, eq } from "drizzle-orm";
import { join } from "path";
import { db } from "../db";
import {
  githubImports,
  repositories,
  users,
} from "../db/schema";
import { requireAuth, softAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import { Layout } from "../views/layout";
import { config } from "../lib/config";
import { repoExists } from "../git/repository";
import { audit } from "../lib/notify";
import {
  buildAuthedCloneUrl,
  createImportRow,
  finaliseImportRow,
  redactCloneUrl,
  runImport,
  type ImportStats,
} from "../lib/github-import";

const githubImport = new Hono<AuthEnv>();

githubImport.use("*", softAuth);

// ---------------------------------------------------------------------------
// Form
// ---------------------------------------------------------------------------

githubImport.get("/new/import", requireAuth, async (c) => {
  const user = c.get("user")!;
  const error = c.req.query("error");

  // Show recent imports by this user so they can see what's been pulled.
  let recent: Array<{
    id: string;
    sourceOwner: string;
    sourceRepo: string;
    status: string;
    stats: string;
    startedAt: Date;
    repositoryId: string | null;
  }> = [];
  try {
    const rows = await db
      .select()
      .from(githubImports)
      .where(eq(githubImports.userId, user.id))
      .orderBy(desc(githubImports.startedAt))
      .limit(10);
    recent = rows.map((r) => ({
      id: r.id,
      sourceOwner: r.sourceOwner,
      sourceRepo: r.sourceRepo,
      status: r.status,
      stats: r.stats,
      startedAt: r.startedAt,
      repositoryId: r.repositoryId,
    }));
  } catch {
    recent = [];
  }

  return c.html(
    <Layout title="Import from GitHub" user={user}>
      <div class="new-repo-form">
        <h2>Import from GitHub</h2>
        <p style="color: var(--muted); margin-bottom: 16px">
          Clone a GitHub repository into your Gluecron namespace, including
          issues, pull requests, comments, releases, and labels.
        </p>
        {error && <div class="auth-error">{decodeURIComponent(error)}</div>}
        <form method="POST" action="/new/import">
          <div class="form-group">
            <label for="source">GitHub repo (owner/repo)</label>
            <input
              type="text"
              id="source"
              name="source"
              required
              placeholder="octocat/hello-world"
              autocomplete="off"
            />
          </div>
          <div class="form-group">
            <label for="token">GitHub personal access token</label>
            <input
              type="password"
              id="token"
              name="token"
              required
              placeholder="github_pat_... or ghp_..."
              autocomplete="off"
            />
            <small style="color: var(--muted)">
              Needs <code>repo</code> scope for private repos, or <code>public_repo</code>.
              Never stored.
            </small>
          </div>
          <div class="form-group">
            <label for="name">Target name on Gluecron</label>
            <input
              type="text"
              id="name"
              name="name"
              required
              pattern="^[a-zA-Z0-9._-]+$"
              placeholder="hello-world"
              autocomplete="off"
            />
          </div>
          <div class="visibility-options">
            <label class="visibility-option">
              <input type="radio" name="visibility" value="public" checked />
              <div class="vis-label">Public</div>
              <div class="vis-desc">Anyone can see this repository</div>
            </label>
            <label class="visibility-option">
              <input type="radio" name="visibility" value="private" />
              <div class="vis-label">Private</div>
              <div class="vis-desc">Only you can see this repository</div>
            </label>
          </div>
          <button type="submit" class="btn btn-primary">
            Start import
          </button>
        </form>

        {recent.length > 0 && (
          <div style="margin-top: 32px">
            <h3>Recent imports</h3>
            <table style="width: 100%; margin-top: 8px">
              <thead>
                <tr>
                  <th style="text-align: left">Source</th>
                  <th style="text-align: left">Status</th>
                  <th style="text-align: left">Stats</th>
                  <th style="text-align: left">When</th>
                </tr>
              </thead>
              <tbody>
                {recent.map((r) => (
                  <tr>
                    <td>
                      <code>
                        {r.sourceOwner}/{r.sourceRepo}
                      </code>
                    </td>
                    <td>
                      <span class={`badge badge-${r.status}`}>{r.status}</span>
                    </td>
                    <td>
                      <code style="font-size: 11px">{r.stats}</code>
                    </td>
                    <td style="font-size: 12px; color: var(--muted)">
                      {r.startedAt.toISOString().slice(0, 19).replace("T", " ")}
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        )}
      </div>
    </Layout>
  );
});

// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------

githubImport.post("/new/import", requireAuth, async (c) => {
  const user = c.get("user")!;
  const body = await c.req.parseBody();
  const source = String(body.source || "").trim();
  const token = String(body.token || "").trim();
  const targetName = String(body.name || "").trim();
  const isPrivate = body.visibility === "private";

  const bail = (msg: string) =>
    c.redirect(`/new/import?error=${encodeURIComponent(msg)}`);

  const sourceMatch = source.match(/^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)$/);
  if (!sourceMatch) return bail("Source must be owner/repo");
  const [, sourceOwner, sourceRepo] = sourceMatch;

  if (!token) return bail("GitHub token is required");
  if (!/^[a-zA-Z0-9._-]+$/.test(targetName)) {
    return bail("Invalid target repository name");
  }
  if (await repoExists(user.username, targetName)) {
    return bail("Target repository already exists");
  }

  const cloneUrl = buildAuthedCloneUrl(token, sourceOwner, sourceRepo);
  if (!cloneUrl.ok) return bail(cloneUrl.error);

  // Create the import ledger row up front so the user can see progress.
  const importId = await createImportRow({
    userId: user.id,
    sourceOwner,
    sourceRepo,
  });

  // Clone the git content first. `git clone --mirror` gives us every ref.
  const destPath = join(
    config.gitReposPath,
    user.username,
    `${targetName}.git`
  );
  if (importId) {
    await finaliseImportRow(importId, { status: "cloning" });
  }

  const proc = Bun.spawn(["git", "clone", "--mirror", cloneUrl.data, destPath], {
    stdout: "pipe",
    stderr: "pipe",
    env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
  });
  const exitCode = await Promise.race([
    proc.exited,
    new Promise<number>((resolve) =>
      setTimeout(() => {
        try {
          proc.kill();
        } catch {
          // ignore
        }
        resolve(124);
      }, 10 * 60 * 1000)
    ),
  ]);

  if (exitCode !== 0) {
    const stderr = await new Response(proc.stderr).text().catch(() => "");
    const redacted = redactCloneUrl(stderr).slice(0, 500);
    if (importId) {
      await finaliseImportRow(importId, {
        status: "error",
        error: `git clone exited ${exitCode}: ${redacted}`,
      });
    }
    return bail(`Clone failed (exit ${exitCode})`);
  }

  // Create the target repo row.
  let newRepoId: string;
  try {
    const [newRepo] = await db
      .insert(repositories)
      .values({
        name: targetName,
        ownerId: user.id,
        description: `Imported from github.com/${sourceOwner}/${sourceRepo}`,
        isPrivate,
        defaultBranch: "main",
        diskPath: destPath,
      })
      .returning();
    if (!newRepo) throw new Error("insert returned nothing");
    newRepoId = newRepo.id;
  } catch (err) {
    if (importId) {
      await finaliseImportRow(importId, {
        status: "error",
        error: err instanceof Error ? err.message : "DB insert failed",
      });
    }
    return bail("Could not create Gluecron repo row");
  }

  // Green-by-default bootstrap (gates, protection, labels, welcome).
  try {
    const { bootstrapRepository } = await import("../lib/repo-bootstrap");
    await bootstrapRepository({
      repositoryId: newRepoId,
      ownerUserId: user.id,
      defaultBranch: "main",
      skipWelcomeIssue: true,
    });
  } catch {
    // non-fatal
  }

  if (importId) {
    await finaliseImportRow(importId, {
      repositoryId: newRepoId,
      status: "walking",
    });
  }

  // Walk GitHub metadata.
  let stats: ImportStats;
  let walkError: string | undefined;
  try {
    const res = await runImport({
      token,
      sourceOwner,
      sourceRepo,
      targetRepoId: newRepoId,
      importerUserId: user.id,
    });
    stats = res.stats;
    walkError = res.error;
  } catch (err) {
    stats = {
      labels: 0,
      issues: 0,
      pulls: 0,
      issueComments: 0,
      prComments: 0,
      releases: 0,
      stargazers: 0,
    };
    walkError = err instanceof Error ? err.message : "unknown error";
  }

  if (importId) {
    await finaliseImportRow(importId, {
      repositoryId: newRepoId,
      status: walkError ? "error" : "ok",
      stats,
      error: walkError,
    });
  }

  try {
    await audit({
      userId: user.id,
      repositoryId: newRepoId,
      action: "github_import",
      targetType: "repository",
      targetId: newRepoId,
      metadata: { source: `${sourceOwner}/${sourceRepo}`, stats, walkError },
    });
  } catch {
    // ignore
  }

  return c.redirect(`/${user.username}/${targetName}`);
});

// ---------------------------------------------------------------------------
// Status API (JSON) — useful for polling from the UI later.
// ---------------------------------------------------------------------------

githubImport.get("/api/imports/:id", requireAuth, async (c) => {
  const user = c.get("user")!;
  const { id } = c.req.param();
  try {
    const [row] = await db
      .select()
      .from(githubImports)
      .where(eq(githubImports.id, id))
      .limit(1);
    if (!row || row.userId !== user.id) {
      return c.json({ error: "not found" }, 404);
    }
    return c.json({
      id: row.id,
      source: `${row.sourceOwner}/${row.sourceRepo}`,
      status: row.status,
      stats: (() => {
        try {
          return JSON.parse(row.stats);
        } catch {
          return {};
        }
      })(),
      error: row.error,
      startedAt: row.startedAt,
      finishedAt: row.finishedAt,
    });
  } catch {
    return c.json({ error: "not available" }, 503);
  }
});

export default githubImport;

// Silence unused-import complaint for tests that need `users`.
void users;