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
merge-queue.tsx14.8 KB · 491 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
 * Block E5 — Merge queue UI + actions.
 *
 *   GET  /:owner/:repo/queue                  — queue history + current state
 *   POST /:owner/:repo/pulls/:n/enqueue       — enqueue a PR (requireAuth)
 *   POST /:owner/:repo/queue/:id/dequeue      — remove entry (owner OR enqueuer)
 *   POST /:owner/:repo/queue/process-next     — owner-only: run the head
 *
 * The "process-next" handler is v1 — it just re-runs gates against the base
 * and, if green, merges by updating the base branch ref. A full background
 * worker is future work; this keeps the feature usable without a daemon.
 */

import { Hono } from "hono";
import { and, eq } from "drizzle-orm";
import { db } from "../db";
import {
  mergeQueueEntries,
  prComments,
  pullRequests,
  repositories,
  users,
} from "../db/schema";
import { Layout } from "../views/layout";
import { RepoHeader, RepoNav } from "../views/components";
import { softAuth, requireAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import {
  enqueuePr,
  dequeueEntry,
  listQueueWithPrs,
  markHeadRunning,
  completeEntry,
  peekHead,
} from "../lib/merge-queue";
import { runAllGateChecks } from "../lib/gate";
import { resolveRef, getRepoPath } from "../git/repository";
import { audit } from "../lib/notify";

const queue = new Hono<AuthEnv>();
queue.use("*", softAuth);

async function loadRepo(ownerName: string, repoName: string) {
  try {
    const [row] = await db
      .select({
        id: repositories.id,
        name: repositories.name,
        ownerId: repositories.ownerId,
        defaultBranch: repositories.defaultBranch,
        starCount: repositories.starCount,
        forkCount: repositories.forkCount,
      })
      .from(repositories)
      .innerJoin(users, eq(repositories.ownerId, users.id))
      .where(and(eq(users.username, ownerName), eq(repositories.name, repoName)))
      .limit(1);
    return row || null;
  } catch {
    return null;
  }
}

function relTime(d: Date | string): string {
  const t = typeof d === "string" ? new Date(d) : d;
  const diffMs = Date.now() - t.getTime();
  const mins = Math.floor(diffMs / 60000);
  if (mins < 1) return "just now";
  if (mins < 60) return `${mins}m ago`;
  const hrs = Math.floor(mins / 60);
  if (hrs < 24) return `${hrs}h ago`;
  const days = Math.floor(hrs / 24);
  if (days < 30) return `${days}d ago`;
  return t.toLocaleDateString();
}

// ---------- Queue list ----------

queue.get("/:owner/:repo/queue", async (c) => {
  const user = c.get("user");
  const { owner, repo } = c.req.param();
  const repoRow = await loadRepo(owner, repo);
  if (!repoRow) {
    return c.html(
      <Layout title="Not found" user={user}>
        <div class="empty-state">
          <h2>Repository not found</h2>
        </div>
      </Layout>,
      404
    );
  }

  const entries = await listQueueWithPrs(repoRow.id);
  const byBranch = new Map<string, typeof entries>();
  for (const e of entries) {
    const arr = byBranch.get(e.baseBranch) || [];
    arr.push(e);
    byBranch.set(e.baseBranch, arr);
  }

  const isOwner = !!user && user.id === repoRow.ownerId;
  const success = c.req.query("success");
  const error = c.req.query("error");

  const stateBadge = (s: string) => {
    const style: Record<string, string> = {
      queued: "background:#30363d;color:#c9d1d9",
      running: "background:#1f6feb;color:white",
      merged: "background:#8957e5;color:white",
      failed: "background:#da3633;color:white",
      dequeued: "background:#484f58;color:#c9d1d9",
    };
    return (
      <span
        style={`${style[s] || style.queued};padding:2px 8px;border-radius:3px;font-size:11px;text-transform:uppercase;font-weight:600`}
      >
        {s}
      </span>
    );
  };

  return c.html(
    <Layout title={`Merge queue — ${owner}/${repo}`} user={user}>
      <RepoHeader
        owner={owner}
        repo={repo}
        starCount={repoRow.starCount}
        forkCount={repoRow.forkCount}
        currentUser={user?.username || null}
      />
      <RepoNav owner={owner} repo={repo} active="pulls" />

      <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px">
        <h3>Merge queue</h3>
        <a href={`/${owner}/${repo}/pulls`} class="btn btn-sm">
          Back to PRs
        </a>
      </div>

      <p style="font-size:13px;color:var(--text-muted);margin-bottom:16px">
        Serialised merges: PRs queued here re-run gates against the latest base
        before being merged. This prevents green-in-isolation / red-after-merge
        races.
      </p>

      {success && <div class="auth-success">{decodeURIComponent(success)}</div>}
      {error && <div class="auth-error">{decodeURIComponent(error)}</div>}

      {entries.length === 0 ? (
        <div class="empty-state">
          <p>Queue is empty. Enqueue a PR from the pull-request page.</p>
        </div>
      ) : (
        Array.from(byBranch.entries()).map(([branch, items]) => {
          const active = items.filter(
            (i) => i.state === "queued" || i.state === "running"
          );
          return (
            <div class="panel" style="margin-bottom:20px;overflow:hidden">
              <div style="padding:12px 14px;background:var(--bg-tertiary);display:flex;justify-content:space-between;align-items:center">
                <div style="font-weight:600">
                  Base: <code>{branch}</code>{" "}
                  <span style="font-size:12px;color:var(--text-muted);font-weight:400;margin-left:8px">
                    {active.length} active
                  </span>
                </div>
                {isOwner && active.length > 0 && (
                  <form
                    method="post"
                    action={`/${owner}/${repo}/queue/process-next?base=${encodeURIComponent(branch)}`}
                  >
                    <button type="submit" class="btn btn-sm btn-primary">
                      Process next
                    </button>
                  </form>
                )}
              </div>
              {items.map((it) => (
                <div
                  class="panel-item"
                  style="justify-content:space-between;align-items:flex-start"
                >
                  <div style="flex:1;min-width:0">
                    <div style="display:flex;gap:8px;align-items:center;flex-wrap:wrap">
                      {stateBadge(it.state)}
                      {it.prNumber != null ? (
                        <a
                          href={`/${owner}/${repo}/pulls/${it.prNumber}`}
                          style="font-weight:600"
                        >
                          #{it.prNumber} {it.prTitle}
                        </a>
                      ) : (
                        <span style="color:var(--text-muted)">(PR gone)</span>
                      )}
                    </div>
                    <div style="font-size:12px;color:var(--text-muted);margin-top:4px">
                      pos {it.position} ·{" "}
                      {it.prHeadBranch ? <code>{it.prHeadBranch}</code> : ""} ·
                      enqueued {relTime(it.enqueuedAt)}
                      {it.startedAt
                        ? ` · started ${relTime(it.startedAt)}`
                        : ""}
                      {it.finishedAt
                        ? ` · finished ${relTime(it.finishedAt)}`
                        : ""}
                    </div>
                    {it.errorMessage && (
                      <div style="font-size:12px;color:var(--red);margin-top:4px">
                        {it.errorMessage}
                      </div>
                    )}
                  </div>
                  {(it.state === "queued" || it.state === "running") &&
                    user &&
                    (isOwner || user.id === it.enqueuedBy) && (
                      <form
                        method="post"
                        action={`/${owner}/${repo}/queue/${it.id}/dequeue`}
                        onsubmit="return confirm('Remove from queue?')"
                      >
                        <button type="submit" class="btn btn-sm">
                          Remove
                        </button>
                      </form>
                    )}
                </div>
              ))}
            </div>
          );
        })
      )}
    </Layout>
  );
});

// ---------- Enqueue a PR ----------

queue.post("/:owner/:repo/pulls/:number/enqueue", requireAuth, async (c) => {
  const user = c.get("user")!;
  const { owner, repo } = c.req.param();
  const prNum = parseInt(c.req.param("number"), 10);
  const repoRow = await loadRepo(owner, repo);
  if (!repoRow) return c.notFound();

  const [pr] = await db
    .select()
    .from(pullRequests)
    .where(
      and(
        eq(pullRequests.repositoryId, repoRow.id),
        eq(pullRequests.number, prNum)
      )
    )
    .limit(1);
  if (!pr || pr.state !== "open") {
    return c.redirect(
      `/${owner}/${repo}/pulls/${prNum}?error=${encodeURIComponent(
        "PR must be open to enqueue."
      )}`
    );
  }
  if (pr.isDraft) {
    return c.redirect(
      `/${owner}/${repo}/pulls/${prNum}?error=${encodeURIComponent(
        "Cannot enqueue a draft PR."
      )}`
    );
  }

  const result = await enqueuePr({
    repositoryId: repoRow.id,
    pullRequestId: pr.id,
    baseBranch: pr.baseBranch,
    enqueuedBy: user.id,
  });
  if (!result.ok) {
    return c.redirect(
      `/${owner}/${repo}/pulls/${prNum}?error=${encodeURIComponent(
        result.reason || "Enqueue failed"
      )}`
    );
  }

  await audit({
    userId: user.id,
    repositoryId: repoRow.id,
    action: "merge_queue.enqueue",
    targetId: pr.id,
    metadata: { prNumber: pr.number, baseBranch: pr.baseBranch },
  });

  return c.redirect(
    `/${owner}/${repo}/queue?success=${encodeURIComponent(
      `PR #${pr.number} enqueued`
    )}`
  );
});

// ---------- Dequeue ----------

queue.post("/:owner/:repo/queue/:id/dequeue", requireAuth, async (c) => {
  const user = c.get("user")!;
  const { owner, repo, id } = c.req.param();
  const repoRow = await loadRepo(owner, repo);
  if (!repoRow) return c.notFound();

  const [entry] = await db
    .select()
    .from(mergeQueueEntries)
    .where(
      and(
        eq(mergeQueueEntries.id, id),
        eq(mergeQueueEntries.repositoryId, repoRow.id)
      )
    )
    .limit(1);
  if (!entry) {
    return c.redirect(
      `/${owner}/${repo}/queue?error=${encodeURIComponent("Entry not found")}`
    );
  }
  const isOwner = user.id === repoRow.ownerId;
  if (!isOwner && entry.enqueuedBy !== user.id) {
    return c.redirect(
      `/${owner}/${repo}/queue?error=${encodeURIComponent(
        "Only the enqueuer or a repo owner can remove this entry."
      )}`
    );
  }

  const ok = await dequeueEntry(id);
  if (!ok) {
    return c.redirect(
      `/${owner}/${repo}/queue?error=${encodeURIComponent("Could not remove entry")}`
    );
  }

  await audit({
    userId: user.id,
    repositoryId: repoRow.id,
    action: "merge_queue.dequeue",
    targetId: entry.pullRequestId,
  });

  return c.redirect(
    `/${owner}/${repo}/queue?success=${encodeURIComponent("Entry removed")}`
  );
});

// ---------- Process next ----------

queue.post("/:owner/:repo/queue/process-next", requireAuth, async (c) => {
  const user = c.get("user")!;
  const { owner, repo } = c.req.param();
  const base = c.req.query("base");
  const repoRow = await loadRepo(owner, repo);
  if (!repoRow) return c.notFound();
  if (repoRow.ownerId !== user.id) {
    return c.redirect(
      `/${owner}/${repo}/queue?error=${encodeURIComponent(
        "Only repo owners can process the queue."
      )}`
    );
  }

  const targetBase = base || repoRow.defaultBranch || "main";
  const head = await peekHead(repoRow.id, targetBase);
  if (!head) {
    return c.redirect(
      `/${owner}/${repo}/queue?error=${encodeURIComponent(
        `No queued entries for base ${targetBase}`
      )}`
    );
  }

  const started = await markHeadRunning(repoRow.id, targetBase);
  if (!started) {
    return c.redirect(
      `/${owner}/${repo}/queue?error=${encodeURIComponent(
        "Could not transition head to running"
      )}`
    );
  }

  // Re-run gates against latest base.
  const [pr] = await db
    .select()
    .from(pullRequests)
    .where(eq(pullRequests.id, started.pullRequestId))
    .limit(1);
  if (!pr) {
    await completeEntry(started.id, "failed", "Pull request no longer exists.");
    return c.redirect(
      `/${owner}/${repo}/queue?error=${encodeURIComponent("PR vanished")}`
    );
  }
  if (pr.state !== "open") {
    await completeEntry(started.id, "failed", "Pull request is no longer open.");
    return c.redirect(
      `/${owner}/${repo}/queue?error=${encodeURIComponent("PR is no longer open")}`
    );
  }

  const headSha = await resolveRef(owner, repo, pr.headBranch);
  if (!headSha) {
    await completeEntry(started.id, "failed", "Head branch not found.");
    return c.redirect(
      `/${owner}/${repo}/queue?error=${encodeURIComponent("Head branch not found")}`
    );
  }

  const gateResult = await runAllGateChecks(
    owner,
    repo,
    pr.baseBranch,
    pr.headBranch,
    headSha,
    true
  );
  const hardFailures = gateResult.checks.filter(
    (check) => !check.passed && check.name !== "Merge check"
  );
  if (hardFailures.length > 0) {
    const msg = hardFailures
      .map((f) => `${f.name}: ${f.details}`)
      .join("; ");
    await completeEntry(started.id, "failed", msg);
    try {
      await db.insert(prComments).values({
        pullRequestId: pr.id,
        authorId: user.id,
        body: `**Merge queue:** gates failed on latest base — ${msg}`,
        isAiReview: false,
      });
    } catch {}
    return c.redirect(
      `/${owner}/${repo}/queue?error=${encodeURIComponent(msg)}`
    );
  }

  // Gates passed — merge by updating base ref to head.
  const repoDir = getRepoPath(owner, repo);
  const proc = Bun.spawn(
    [
      "git",
      "update-ref",
      `refs/heads/${pr.baseBranch}`,
      `refs/heads/${pr.headBranch}`,
    ],
    { cwd: repoDir, stdout: "pipe", stderr: "pipe" }
  );
  const exit = await proc.exited;
  if (exit !== 0) {
    await completeEntry(started.id, "failed", "update-ref failed");
    return c.redirect(
      `/${owner}/${repo}/queue?error=${encodeURIComponent(
        "Merge failed — unable to update base ref"
      )}`
    );
  }

  await db
    .update(pullRequests)
    .set({
      state: "merged",
      mergedAt: new Date(),
      mergedBy: user.id,
      updatedAt: new Date(),
    })
    .where(eq(pullRequests.id, pr.id));

  await completeEntry(started.id, "merged");

  await audit({
    userId: user.id,
    repositoryId: repoRow.id,
    action: "merge_queue.merged",
    targetId: pr.id,
    metadata: { prNumber: pr.number, baseBranch: pr.baseBranch },
  });

  return c.redirect(
    `/${owner}/${repo}/queue?success=${encodeURIComponent(
      `PR #${pr.number} merged via queue`
    )}`
  );
});

export default queue;