MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / upsertLinearInstallation

Function upsertLinearInstallation

apps/web/src/lib/integrations/linear-service.ts:339–444  ·  view source on GitHub ↗
(
  {
    owner,
    organizationId,
    organizationName,
    botUserId,
  }: {
    owner: Owner;
    organizationId: string;
    organizationName: string;
    botUserId: string | null;
  },
  options: LinearUpsertOptions = {}
)

Source from the content-addressed store, hash-verified

337 * has committed; see `retirePreviousLinearWorkspace`.
338 */
339 previousWorkspace: PreviousLinearWorkspace | null;
340};
341
342type LinearUpsertOptions = {
343 /**
344 * Model resolved by the caller before the install transaction. `null` means the
345 * owner's policy allows no candidate model; `undefined` resolves it here.
346 */
347 defaultModel?: string | null;
348 getChatSdkAccessToken?: (organizationId: string) => Promise<string | null>;
349};
350
351/**
352 * Create or update a Linear `platform_integrations` row after a successful
353 * OAuth exchange. The OAuth access/refresh tokens live in the Chat SDK state
354 * adapter, written by the install callback after it has confirmed the workspace
355 * belongs to this owner; this row stores only non-secret metadata used by the
356 * tRPC router / UI and to gate bot processing via `metadata.bot_enabled`.
357 *
358 * When the same Kilo owner reinstalls onto a *different* Linear workspace
359 * (`existing.platform_installation_id !== organizationId`), this returns the
360 * previous workspace so the caller can retire it — revoke the OAuth token
361 * Linear-side and drop the Chat SDK installation and identity cache — once the
362 * connection write has committed. That retirement keeps the workspace list in
363 * the connection settings accurate: after it, no DB row connects that
364 * organizationId back to a Kilo owner.
365 */
366export async function upsertLinearInstallation(
367 {
368 owner,
369 organizationId,
370 organizationName,
371 botUserId,
372 }: {
373 owner: Owner;
374 organizationId: string;
375 organizationName: string;
376 botUserId: string | null;
377 },
378 options: LinearUpsertOptions = {},
379 database: typeof db | DrizzleTransaction = db
380): Promise<UpsertLinearInstallationResult> {
381 const existing = await getInstallation(owner, database);
382
383 const conflicting = await getConflictingLinearInstallation(owner, organizationId, database);
384 if (conflicting) {
385 throw new LinearWorkspaceAlreadyConnectedError(organizationName);
386 }
387
388 const previousWorkspace =
389 existing &&
390 existing.platform_installation_id &&
391 existing.platform_installation_id !== organizationId
392 ? {
393 organizationId: existing.platform_installation_id,
394 accessToken:
395 (await options.getChatSdkAccessToken?.(existing.platform_installation_id)) ?? null,
396 }

Callers 2

Calls 8

getDefaultAllowedModelFunction · 0.90
revokeLinearTokenFunction · 0.85
getInstallationFunction · 0.70
setMethod · 0.65
updateMethod · 0.65
valuesMethod · 0.65

Tested by

no test coverage detected