n8n/packages/nodes-base/nodes/Linear/LinearTrigger.node.ts
Iván Ovejero 88dea330b9
refactor: Apply more eslint-plugin-n8n-nodes-base rules (#3534)
*  Update `lintfix` script

*  Run baseline `lintfix`

* 🔥 Remove unneeded exceptions (#3538)

* 🔥 Remove exceptions for `node-param-default-wrong-for-simplify`

* 🔥 Remove exceptions for `node-param-placeholder-miscased-id`

*  Update version

* 👕 Apply `node-param-placeholder-missing` (#3542)

* 👕 Apply `filesystem-wrong-cred-filename` (#3543)

* 👕 Apply `node-param-description-missing-from-dynamic-options` (#3545)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-class-description-empty-string` (#3546)

* 👕 Apply `node-class-description-icon-not-svg` (#3548)

* 👕 Apply `filesystem-wrong-node-filename` (#3549)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Expand lintings to credentials (#3550)

* 👕 Apply `node-param-multi-options-type-unsorted-items` (#3552)

*  fix

*  Minor fixes

Co-authored-by: Michael Kret <michael.k@radency.com>

* 👕 Apply `node-param-description-wrong-for-dynamic-multi-options` (#3541)

*  Add new lint rule, node-param-description-wrong-for-dynamic-multi-options

*  Fix with updated linting rules

*  Minor fixes

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-description-boolean-without-whether` (#3553)

*  fix

* Update packages/nodes-base/nodes/Clockify/ProjectDescription.ts

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply node-param-display-name-wrong-for-dynamic-multi-options (#3537)

* 👕 Add exceptions

* 👕 Add exception

* ✏️ Alphabetize rules

*  Restore `lintfix` command

Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
2022-06-20 07:54:01 -07:00

245 lines
5.3 KiB
TypeScript

import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
ILoadOptionsFunctions,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
} from 'n8n-workflow';
import {
capitalizeFirstLetter,
linearApiRequest,
} from './GenericFunctions';
export class LinearTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Linear Trigger',
name: 'linearTrigger',
icon: 'file:linear.svg',
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["triggerOn"]}}',
description: 'Starts the workflow when Linear events occur',
defaults: {
name: 'Linear Trigger',
color: '#D9DCF8',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'linearApi',
required: true,
testedBy: 'linearApiTest',
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Team Name or ID',
name: 'teamId',
type: 'options',
description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/nodes/expressions.html#expressions">expression</a>',
typeOptions: {
loadOptionsMethod: 'getTeams',
},
default: '',
},
{
displayName: 'Listen to Resources',
name: 'resources',
type: 'multiOptions',
options: [
{
name: 'Comment Reaction',
value: 'reaction',
},
{
name: 'Cycle',
value: 'cycle',
},
/* It's still on Alpha stage
{
name: 'Issue Attachment',
value: 'attachment',
},*/
{
name: 'Issue',
value: 'issue',
},
{
name: 'Issue Comment',
value: 'comment',
},
{
name: 'Issue Label',
value: 'issueLabel',
},
{
name: 'Project',
value: 'project',
},
],
default: [],
required: true,
},
],
};
methods = {
loadOptions: {
async getTeams(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const body = {
query:
`query Teams {
teams {
nodes {
id
name
}
}
}`,
};
const { data: { teams: { nodes } } } = await linearApiRequest.call(this, body);
for (const node of nodes) {
returnData.push({
name: node.name,
value: node.id,
});
}
return returnData;
},
},
};
//@ts-ignore (because of request)
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const teamId = this.getNodeParameter('teamId') as string;
const body = {
query:
`query {
webhooks {
nodes {
id
url
enabled
team {
id
name
}
}
}
}`,
};
// Check all the webhooks which exist already if it is identical to the
// one that is supposed to get created.
const { data: { webhooks: { nodes } } } = await linearApiRequest.call(this, body);
for (const node of nodes) {
if (node.url === webhookUrl &&
node.team.id === teamId &&
node.enabled === true) {
webhookData.webhookId = node.id as string;
return true;
}
}
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
const teamId = this.getNodeParameter('teamId') as string;
const resources = this.getNodeParameter('resources') as string[];
const body = {
query: `
mutation webhookCreate($url: String!, $teamId: String!, $resources: [String!]!) {
webhookCreate(
input: {
url: $url
teamId: $teamId
resourceTypes: $resources
}
) {
success
webhook {
id
enabled
}
}
}`,
variables: {
url: webhookUrl,
teamId,
resources: resources.map(capitalizeFirstLetter),
},
};
const { data: { webhookCreate: { success, webhook: { id } } } } = await linearApiRequest.call(this, body);
if (!success) {
return false;
}
webhookData.webhookId = id as string;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId !== undefined) {
const body = {
query: `
mutation webhookDelete($id: String!){
webhookDelete(
id: $id
) {
success
}
}`,
variables: {
id: webhookData.webhookId,
},
};
try {
await linearApiRequest.call(this, body);
} catch (error) {
return false;
}
// Remove from the static workflow data so that it is clear
// that no webhooks are registred anymore
delete webhookData.webhookId;
}
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const bodyData = this.getBodyData();
return {
workflowData: [
this.helpers.returnJsonArray(bodyData),
],
};
}
}