n8n/packages/nodes-base/nodes/ClickUp/ClickUpTrigger.node.ts

344 lines
7.7 KiB
TypeScript
Raw Normal View History

import type {
IHookFunctions,
IWebhookFunctions,
2020-01-18 14:19:31 -08:00
IDataObject,
ILoadOptionsFunctions,
INodePropertyOptions,
2020-01-18 14:19:31 -08:00
INodeType,
INodeTypeDescription,
2020-01-18 14:19:31 -08:00
IWebhookResponseData,
} from 'n8n-workflow';
import { clickupApiRequest } from './GenericFunctions';
2020-01-18 14:19:31 -08:00
import { createHmac } from 'crypto';
2020-01-18 14:19:31 -08:00
export class ClickUpTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'ClickUp Trigger',
name: 'clickUpTrigger',
icon: 'file:clickup.svg',
2020-01-18 14:19:31 -08:00
group: ['trigger'],
version: 1,
description: 'Handle ClickUp events via webhooks (Beta)',
defaults: {
name: 'ClickUp Trigger',
},
inputs: [],
outputs: ['main'],
credentials: [
{
2020-10-08 07:34:57 -07:00
name: 'clickUpApi',
2020-01-18 14:19:31 -08:00
required: true,
displayOptions: {
show: {
authentication: ['accessToken'],
},
},
},
{
name: 'clickUpOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: ['oAuth2'],
},
},
},
2020-01-18 14:19:31 -08:00
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Access Token',
value: 'accessToken',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'accessToken',
},
2020-01-18 14:19:31 -08:00
{
refactor: Apply more `eslint-plugin-n8n-nodes-base` autofixable rules (#3432) * :zap: Update `lintfix` script * :shirt: Remove unneeded lint exceptions * :shirt: Run baseline `lintfix` * :shirt: Apply `node-param-description-miscased-url` (#3441) * :shirt: Apply `rule node-param-placeholder-miscased-id` (#3443) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-name-wrong-for-upsert` (#3446) * :shirt: Apply `node-param-min-value-wrong-for-limit` (#3442) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * Apply `node-param-display-name-wrong-for-dynamic-options` (#3454) * :hammer: fix * :zap: Fix `Assigned To` fields Co-authored-by: Michael Kret <michael.k@radency.com> * :shirt: Apply `rule node-param-default-wrong-for-number` (#3453) * :shirt: Apply `node-param-default-wrong-for-string` (#3452) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * Apply `node-param-display-name-miscased` (#3449) * :hammer: fix * :hammer: exceptions * :zap: review fixes * :shirt: Apply `node-param-description-lowercase-first-char` (#3451) * :zap: fix * :zap: review fixes * :zap: fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-wrong-for-dynamic-options` (#3456) * Rule working as intended * Add rule * :fire: Remove repetitions * :shirt: Add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Small fix for `node-param-description-wrong-for-dynamic-options` * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3460) * :shirt: Apply `node-param-description-line-break-html-tag` (#3462) * :shirt: Run baseline `lintfix` * :shirt: Apply `node-param-options-type-unsorted-items` (#3459) * :zap: fix * :hammer: exceptions * Add exception for Salesmate and Zoom Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :zap: Restore `lintfix` command Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com>
2022-06-03 10:23:49 -07:00
displayName: 'Team Name or ID',
2020-01-18 14:19:31 -08:00
name: 'team',
type: 'options',
description:
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
2020-01-18 14:19:31 -08:00
typeOptions: {
loadOptionsMethod: 'getTeams',
},
required: true,
default: '',
},
{
displayName: 'Events',
name: 'events',
type: 'multiOptions',
required: true,
default: [],
options: [
{
name: '*',
value: '*',
},
{
name: 'folder.created',
value: 'folderCreated',
2020-01-18 14:19:31 -08:00
},
{
name: 'folder.deleted',
value: 'folderDeleted',
2020-01-18 14:19:31 -08:00
},
{
name: 'folder.updated',
value: 'folderUpdated',
2020-01-18 14:19:31 -08:00
},
{
name: 'goal.created',
value: 'goalCreated',
2020-01-18 14:19:31 -08:00
},
{
name: 'goal.deleted',
value: 'goalDeleted',
2020-01-18 14:19:31 -08:00
},
refactor: Apply more `eslint-plugin-n8n-nodes-base` rules (#3534) * :zap: Update `lintfix` script * :zap: Run baseline `lintfix` * :fire: Remove unneeded exceptions (#3538) * :fire: Remove exceptions for `node-param-default-wrong-for-simplify` * :fire: Remove exceptions for `node-param-placeholder-miscased-id` * :zap: Update version * :shirt: Apply `node-param-placeholder-missing` (#3542) * :shirt: Apply `filesystem-wrong-cred-filename` (#3543) * :shirt: Apply `node-param-description-missing-from-dynamic-options` (#3545) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-class-description-empty-string` (#3546) * :shirt: Apply `node-class-description-icon-not-svg` (#3548) * :shirt: Apply `filesystem-wrong-node-filename` (#3549) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Expand lintings to credentials (#3550) * :shirt: Apply `node-param-multi-options-type-unsorted-items` (#3552) * :zap: fix * :zap: Minor fixes Co-authored-by: Michael Kret <michael.k@radency.com> * :shirt: Apply `node-param-description-wrong-for-dynamic-multi-options` (#3541) * :zap: Add new lint rule, node-param-description-wrong-for-dynamic-multi-options * :zap: Fix with updated linting rules * :zap: Minor fixes Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-boolean-without-whether` (#3553) * :zap: fix * Update packages/nodes-base/nodes/Clockify/ProjectDescription.ts Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply node-param-display-name-wrong-for-dynamic-multi-options (#3537) * :shirt: Add exceptions * :shirt: Add exception * :pencil2: Alphabetize rules * :zap: 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
{
name: 'goal.updated',
value: 'goalUpdated',
},
2020-01-18 14:19:31 -08:00
{
name: 'keyResult.created',
value: 'keyResultCreated',
2020-01-18 14:19:31 -08:00
},
{
name: 'keyResult.deleted',
value: 'keyResultDelete',
2020-01-18 14:19:31 -08:00
},
{
name: 'keyResult.updated',
value: 'keyResultUpdated',
2020-01-18 14:19:31 -08:00
},
{
name: 'list.created',
value: 'listCreated',
2020-01-18 14:19:31 -08:00
},
{
name: 'list.deleted',
value: 'listDeleted',
2020-01-18 14:19:31 -08:00
},
{
name: 'list.updated',
value: 'listUpdated',
2020-01-18 14:19:31 -08:00
},
{
name: 'space.created',
value: 'spaceCreated',
2020-01-18 14:19:31 -08:00
},
{
name: 'space.deleted',
value: 'spaceDeleted',
2020-01-18 14:19:31 -08:00
},
{
name: 'space.updated',
value: 'spaceUpdated',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.assignee.updated',
value: 'taskAssigneeUpdated',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.comment.posted',
value: 'taskCommentPosted',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.comment.updated',
value: 'taskCommentUpdated',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.created',
value: 'taskCreated',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.deleted',
value: 'taskDeleted',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.dueDate.updated',
value: 'taskDueDateUpdated',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.moved',
value: 'taskMoved',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.status.updated',
value: 'taskStatusUpdated',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.tag.updated',
value: 'taskTagUpdated',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.timeEstimate.updated',
value: 'taskTimeEstimateUpdated',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.timeTracked.updated',
value: 'taskTimeTrackedUpdated',
2020-01-18 14:19:31 -08:00
},
{
name: 'task.updated',
value: 'taskUpdated',
2020-01-18 14:19:31 -08:00
},
],
},
{
displayName: 'Filters',
name: 'filters',
type: 'collection',
placeholder: 'Add Field',
default: {},
options: [
{
displayName: 'Folder ID',
name: 'folderId',
type: 'string',
default: '',
},
{
displayName: 'List ID',
name: 'listId',
type: 'string',
default: '',
},
{
displayName: 'Space ID',
name: 'spaceId',
type: 'string',
default: '',
},
2020-01-18 14:19:31 -08:00
{
displayName: 'Task ID',
name: 'taskId',
type: 'string',
default: '',
},
],
},
],
};
methods = {
loadOptions: {
// Get all the available teams to display them to user so that they can
2020-01-18 14:19:31 -08:00
// select them easily
async getTeams(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const { teams } = await clickupApiRequest.call(this, 'GET', '/team');
for (const team of teams) {
const teamName = team.name;
const teamId = team.id;
returnData.push({
name: teamName,
value: teamId,
});
}
return returnData;
},
},
};
2020-01-18 14:19:31 -08:00
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const teamId = this.getNodeParameter('team') as string;
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId === undefined) {
return false;
}
const endpoint = `/team/${teamId}/webhook`;
const { webhooks } = await clickupApiRequest.call(this, 'GET', endpoint);
if (Array.isArray(webhooks)) {
for (const webhook of webhooks) {
if (webhook.id === webhookData.webhookId) {
return true;
}
}
}
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const filters = this.getNodeParameter('filters') as IDataObject;
const teamId = this.getNodeParameter('team') as string;
const events = this.getNodeParameter('events') as string[];
const endpoint = `/team/${teamId}/webhook`;
const body: IDataObject = {
endpoint: webhookUrl,
events,
};
if (events.includes('*')) {
body.events = '*';
}
if (filters.listId) {
body.list_id = (filters.listId as string).replace('#', '');
2020-01-18 14:19:31 -08:00
}
if (filters.taskId) {
body.task_id = (filters.taskId as string).replace('#', '');
2020-01-18 14:19:31 -08:00
}
if (filters.spaceId) {
body.space_id = (filters.spaceId as string).replace('#', '');
2020-01-18 14:19:31 -08:00
}
if (filters.folderId) {
body.folder_id = (filters.folderId as string).replace('#', '');
2020-01-18 14:19:31 -08:00
}
const { webhook } = await clickupApiRequest.call(this, 'POST', endpoint, body);
webhookData.webhookId = webhook.id;
webhookData.secret = webhook.secret;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const endpoint = `/webhook/${webhookData.webhookId}`;
try {
await clickupApiRequest.call(this, 'DELETE', endpoint);
} catch (error) {
2020-01-18 14:19:31 -08:00
return false;
}
delete webhookData.webhookId;
delete webhookData.secret;
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const webhookData = this.getWorkflowStaticData('node');
const headerData = this.getHeaderData() as IDataObject;
const req = this.getRequestObject();
const computedSignature = createHmac('sha256', webhookData.secret as string)
.update(JSON.stringify(req.body))
.digest('hex');
2020-01-18 14:19:31 -08:00
if (headerData['x-signature'] !== computedSignature) {
// Signature is not valid so ignore call
return {};
}
return {
workflowData: [this.helpers.returnJsonArray(req.body as IDataObject)],
2020-01-18 14:19:31 -08:00
};
}
}