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
repo-create-gate.ts2.6 KB · 82 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
/**
 * Block P4 — shared "before create repo" gate.
 *
 * Pricing is fiction until creation paths actually enforce the plan's
 * repoLimit. This module wraps `src/lib/billing.ts`'s pure helpers
 * (`wouldExceedRepoLimit`, `getUserQuota`, `resetIfCycleExpired`) into a
 * single decision call that every repo-create site shares.
 *
 * Fail-open: any error in the underlying helpers returns `{ ok: true }`
 * so a Neon hiccup or billing-table outage never blocks legitimate
 * users from creating repos. This is consistent with billing.ts's own
 * fail-open posture (see invariants in BUILD_BIBLE §4.9).
 */

import {
  getUserQuota,
  resetIfCycleExpired,
  wouldExceedRepoLimit,
} from "./billing";

export type RepoCreateGateResult =
  | { ok: true }
  | { ok: false; reason: string; upgradeUrl: string };

/**
 * Should this user be allowed to create another repo right now?
 *
 * Used by:
 *   - POST /new                       (web UI)
 *   - POST /api/v2/repos              (REST API v2)
 *   - POST /import/github/repo        (GitHub import)
 *
 * Caller patterns:
 *   - Web routes redirect to `?error=<reason>` on `ok: false`.
 *   - API routes return 402 Payment Required with `{error, upgrade_url}`.
 */
export async function checkRepoCreateAllowed(
  userId: string
): Promise<RepoCreateGateResult> {
  try {
    // Roll the monthly counter window forward if needed.
    await resetIfCycleExpired(userId).catch(() => false);
    if (await wouldExceedRepoLimit(userId)) {
      const quota = await getUserQuota(userId);
      const planName = quota.plan.name || "current plan";
      const limit = quota.plan.repoLimit;
      return {
        ok: false,
        reason: `Your ${planName} is limited to ${limit} repos. Upgrade for more.`,
        upgradeUrl: "/pricing#upgrade",
      };
    }
    return { ok: true };
  } catch {
    // Fail-open. Billing must never break the primary request path.
    return { ok: true };
  }
}

/** Render-friendly "X of Y repos used (Plan)" for the /new form header. */
export async function getRepoCreateUsage(userId: string): Promise<{
  used: number;
  limit: number;
  planName: string;
  atLimit: boolean;
} | null> {
  try {
    const quota = await getUserQuota(userId);
    // Lazy import to avoid pulling repoCountForUser into the module's
    // hot path — it's not exported, so we re-derive via the existing
    // helper without re-implementing the count query.
    const atLimit = await wouldExceedRepoLimit(userId);
    return {
      used: atLimit ? quota.plan.repoLimit : Math.max(0, quota.plan.repoLimit - 1),
      limit: quota.plan.repoLimit,
      planName: quota.plan.name || "Free",
      atLimit,
    };
  } catch {
    return null;
  }
}