CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
Blame · Line-by-line history
bot-user.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| a7460bf | 1 | /** |
| 2 | * Bot-user helper — resolves the UUID for the synthetic `gluecron[bot]` | |
| 3 | * account so autopilot / AI-review actions are credited to it rather than | |
| 4 | * to the PR / issue author. | |
| 5 | * | |
| 6 | * The row is seeded by drizzle/0078_bot_user.sql. If the migration has not | |
| 7 | * run yet (e.g. a freshly-cloned dev environment) the helper returns `null` | |
| 8 | * and callers must fall back to a real user id — the same behaviour as before | |
| 9 | * this feature shipped. | |
| 10 | * | |
| 11 | * The result is module-level cached so every autopilot tick after the first | |
| 12 | * one pays zero DB overhead. | |
| 13 | */ | |
| 14 | ||
| 15 | import { eq } from "drizzle-orm"; | |
| 16 | import { db } from "../db"; | |
| 17 | import { users } from "../db/schema"; | |
| 18 | ||
| 19 | export const BOT_USERNAME = "gluecron[bot]"; | |
| 20 | ||
| 21 | let _botUserId: string | null | undefined = undefined; // undefined = not yet fetched | |
| 22 | ||
| 23 | /** | |
| 24 | * Lazily resolve and cache the `gluecron[bot]` user's UUID. | |
| 25 | * | |
| 26 | * Returns `null` when the row does not exist (migration not yet applied). | |
| 27 | * Callers should fall back to `authorId` from the related PR/issue. | |
| 28 | */ | |
| 29 | export async function getBotUserId(): Promise<string | null> { | |
| 30 | if (_botUserId !== undefined) return _botUserId; | |
| 31 | try { | |
| 32 | const [row] = await db | |
| 33 | .select({ id: users.id }) | |
| 34 | .from(users) | |
| 35 | .where(eq(users.username, BOT_USERNAME)) | |
| 36 | .limit(1); | |
| 37 | _botUserId = row?.id ?? null; | |
| 38 | } catch { | |
| 39 | // DB unavailable — leave undefined so next call retries. | |
| 40 | return null; | |
| 41 | } | |
| 42 | return _botUserId; | |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Resolve the bot user ID, falling back to `fallbackId` if the bot row | |
| 47 | * does not exist yet. The fallback keeps every call site backward- | |
| 48 | * compatible with pre-migration environments. | |
| 49 | */ | |
| 50 | export async function getBotUserIdOrFallback( | |
| 51 | fallbackId: string | |
| 52 | ): Promise<string> { | |
| 53 | const botId = await getBotUserId(); | |
| 54 | return botId ?? fallbackId; | |
| 55 | } |