d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
105 lines
3.4 KiB
TypeScript
105 lines
3.4 KiB
TypeScript
import type { Team } from '@linear/sdk'
|
|
import { LinearClient } from '@linear/sdk'
|
|
import { createLogger } from '@sim/logger'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { linearTeamsSelectorContract } from '@/lib/api/contracts/selectors'
|
|
import { parseRequest } from '@/lib/api/server'
|
|
import { authorizeCredentialUse } from '@/lib/auth/credential-access'
|
|
import { generateRequestId } from '@/lib/core/utils/request'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
const logger = createLogger('LinearTeamsAPI')
|
|
|
|
/** Linear's maximum page size for a single connection request. */
|
|
const LINEAR_PAGE_SIZE = 250
|
|
|
|
/**
|
|
* Upper bound on pages to drain from the teams connection. At 250 teams/page
|
|
* this covers 2,500 teams; the cap guards against runaway loops on a broken
|
|
* `hasNextPage` rather than a realistic limit.
|
|
*/
|
|
const MAX_TEAMS_PAGES = 10
|
|
|
|
/**
|
|
* Drains the full Linear teams connection by following
|
|
* `pageInfo.endCursor` until `hasNextPage` is false. Bounded by
|
|
* `MAX_TEAMS_PAGES`; logs a warning if the cap is hit so a truncated list is
|
|
* visible rather than silently dropped.
|
|
*/
|
|
async function fetchAllTeams(linearClient: LinearClient): Promise<Team[]> {
|
|
const teams: Team[] = []
|
|
let after: string | undefined
|
|
|
|
for (let page = 0; page < MAX_TEAMS_PAGES; page++) {
|
|
const result = await linearClient.teams({ first: LINEAR_PAGE_SIZE, after })
|
|
teams.push(...result.nodes)
|
|
|
|
if (!result.pageInfo.hasNextPage) {
|
|
return teams
|
|
}
|
|
after = result.pageInfo.endCursor ?? undefined
|
|
if (!after) {
|
|
return teams
|
|
}
|
|
if (page === MAX_TEAMS_PAGES - 1) {
|
|
logger.warn('Linear teams pagination hit cap; team list may be incomplete', {
|
|
cap: MAX_TEAMS_PAGES,
|
|
fetched: teams.length,
|
|
})
|
|
}
|
|
}
|
|
|
|
return teams
|
|
}
|
|
|
|
export const POST = withRouteHandler(async (request: NextRequest) => {
|
|
try {
|
|
const requestId = generateRequestId()
|
|
const parsed = await parseRequest(linearTeamsSelectorContract, request, {})
|
|
if (!parsed.success) return parsed.response
|
|
const { credential, workflowId } = parsed.data.body
|
|
|
|
const authz = await authorizeCredentialUse(request, {
|
|
credentialId: credential,
|
|
workflowId,
|
|
})
|
|
if (!authz.ok || !authz.credentialOwnerUserId) {
|
|
return NextResponse.json({ error: authz.error || 'Unauthorized' }, { status: 403 })
|
|
}
|
|
|
|
const accessToken = await refreshAccessTokenIfNeeded(
|
|
credential,
|
|
authz.credentialOwnerUserId,
|
|
requestId
|
|
)
|
|
if (!accessToken) {
|
|
logger.error('Failed to get access token', {
|
|
credentialId: credential,
|
|
userId: authz.credentialOwnerUserId,
|
|
})
|
|
return NextResponse.json(
|
|
{ error: 'Could not retrieve access token', authRequired: true },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const linearClient = new LinearClient({ accessToken })
|
|
const allTeams = await fetchAllTeams(linearClient)
|
|
const teams = allTeams.map((team: Team) => ({
|
|
id: team.id,
|
|
name: team.name,
|
|
}))
|
|
|
|
return NextResponse.json({ teams })
|
|
} catch (error) {
|
|
logger.error('Error processing Linear teams request:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to retrieve Linear teams', details: (error as Error).message },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
})
|