import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getLinearApi } from "@automate.ax/integration-contracts/linear" import { LINEAR_USER_FIELDS } from "../lib/fragments" import { LINEAR_ORGANIZATION_SCHEMA, LINEAR_USER_SCHEMA, } from "@automate.ax/integration-contracts/linear" import { LINEAR_READ_ACCOUNT_OPTIONS } from "../lib/scopes" const VIEWER_RESPONSE_SCHEMA = z.object({ viewer: LINEAR_USER_SCHEMA }) const ORGANIZATION_RESPONSE_SCHEMA = z.object({ organization: LINEAR_ORGANIZATION_SCHEMA, }) /** Gets the Linear user represented by the connected OAuth account. */ export const getLinearViewer = defineAction("Get Linear viewer") .describe("Gets the authenticated Linear user and profile details.") .account("linear", LINEAR_READ_ACCOUNT_OPTIONS) .output(LINEAR_USER_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account }) => getLinearApi(account.secret) .graphql(`query LinearViewer { viewer { ${LINEAR_USER_FIELDS} } }`, { responseSchema: VIEWER_RESPONSE_SCHEMA, }) .then(({ viewer }) => viewer), ) /** Gets the Linear workspace represented by the connected OAuth account. */ export const getLinearWorkspace = defineAction("Get Linear workspace") .describe("Gets the connected Linear workspace and usage metadata.") .account("linear", LINEAR_READ_ACCOUNT_OPTIONS) .output(LINEAR_ORGANIZATION_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account }) => getLinearApi(account.secret) .graphql( `query LinearOrganization { organization { id createdAt updatedAt archivedAt name urlKey logoUrl roadmapEnabled userCount createdIssueCount } }`, { responseSchema: ORGANIZATION_RESPONSE_SCHEMA }, ) .then(({ organization }) => organization), )