UNPKG

n8n

Version:

n8n Workflow Automation Tool

83 lines 4.18 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LinearIntegration = void 0; const backend_common_1 = require("@n8n/backend-common"); const di_1 = require("@n8n/di"); const agent_chat_integration_1 = require("../agent-chat-integration"); const esm_loader_1 = require("../esm-loader"); let LinearIntegration = class LinearIntegration extends agent_chat_integration_1.AgentChatIntegration { constructor(logger) { super(); this.logger = logger; this.type = 'linear'; this.credentialTypes = ['linearApi', 'linearOAuth2Api']; this.displayLabel = 'Linear'; this.displayIcon = 'linear'; } async createAdapter(ctx) { const auth = this.extractAuth(ctx.credential); const webhookSecret = this.extractSigningSecret(ctx.credential); const userName = await this.fetchDisplayName(auth); const { createLinearAdapter } = await (0, esm_loader_1.loadLinearAdapter)(); return createLinearAdapter({ ...(auth.kind === 'apiKey' ? { apiKey: auth.token } : { accessToken: auth.token }), webhookSecret, ...(userName ? { userName } : {}), }); } extractAuth(credential) { if (typeof credential.apiKey === 'string' && credential.apiKey) { return { kind: 'apiKey', token: credential.apiKey }; } const tokenData = credential.oauthTokenData; const oauthToken = tokenData?.access_token ?? tokenData?.accessToken; if (typeof oauthToken === 'string' && oauthToken) { return { kind: 'accessToken', token: oauthToken }; } throw new Error('Could not extract an API token from the Linear credential. ' + 'Please ensure the credential has a valid API key (linearApi) ' + 'or completed OAuth flow (linearOAuth2Api).'); } extractSigningSecret(credential) { const secret = credential.signingSecret; if (typeof secret === 'string' && secret) { return secret; } throw new Error('The Linear credential is missing a signing secret, which is required for ' + 'agent integrations. Edit the credential and add the signing secret from ' + 'your Linear webhook configuration (Settings → API → Webhooks → Signing secret).'); } async fetchDisplayName(auth) { const authHeader = auth.kind === 'accessToken' ? `Bearer ${auth.token}` : auth.token; try { const resp = await fetch('https://api.linear.app/graphql', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: authHeader }, body: JSON.stringify({ query: '{ viewer { displayName } }' }), }); if (!resp.ok) return undefined; const json = (await resp.json()); return json.data?.viewer?.displayName; } catch (error) { this.logger.debug(`[LinearIntegration] viewer lookup failed: ${error instanceof Error ? error.message : String(error)}`); return undefined; } } }; exports.LinearIntegration = LinearIntegration; exports.LinearIntegration = LinearIntegration = __decorate([ (0, di_1.Service)(), __metadata("design:paramtypes", [backend_common_1.Logger]) ], LinearIntegration); //# sourceMappingURL=linear-integration.js.map