2020-01-18 14:19:31 -08:00
|
|
|
import {
|
|
|
|
IHookFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
2020-01-18 18:36:57 -08:00
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodePropertyOptions,
|
2020-01-18 14:19:31 -08:00
|
|
|
INodeType,
|
2020-01-18 18:36:57 -08:00
|
|
|
INodeTypeDescription,
|
2020-01-18 14:19:31 -08:00
|
|
|
IWebhookResponseData,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
clickupApiRequest,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2021-03-24 15:01:12 -07:00
|
|
|
import {
|
|
|
|
createHmac,
|
|
|
|
} from 'crypto';
|
2020-01-18 14:19:31 -08:00
|
|
|
|
|
|
|
export class ClickUpTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'ClickUp Trigger',
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'clickUpTrigger',
|
2021-03-24 15:01:12 -07:00
|
|
|
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',
|
|
|
|
color: '#7B68EE',
|
|
|
|
},
|
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
2020-10-08 07:34:57 -07:00
|
|
|
name: 'clickUpApi',
|
2020-01-18 14:19:31 -08:00
|
|
|
required: true,
|
2020-09-16 00:16:06 -07:00
|
|
|
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: [
|
2020-09-16 00:16:06 -07:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
displayName: 'Team',
|
|
|
|
name: 'team',
|
|
|
|
type: 'options',
|
|
|
|
typeOptions: {
|
|
|
|
loadOptionsMethod: 'getTeams',
|
|
|
|
},
|
|
|
|
required: true,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Events',
|
|
|
|
name: 'events',
|
|
|
|
type: 'multiOptions',
|
|
|
|
required: true,
|
|
|
|
default: [],
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: '*',
|
|
|
|
value: '*',
|
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'folder.created',
|
|
|
|
value: 'folderCreated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'folder.deleted',
|
|
|
|
value: 'folderDeleted',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'folder.updated',
|
|
|
|
value: 'folderUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'goal.created',
|
|
|
|
value: 'goalCreated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'goal.updated',
|
|
|
|
value: 'goalUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'goal.deleted',
|
|
|
|
value: 'goalDeleted',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'keyResult.created',
|
|
|
|
value: 'keyResultCreated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'keyResult.deleted',
|
|
|
|
value: 'keyResultDelete',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'keyResult.updated',
|
|
|
|
value: 'keyResultUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'list.created',
|
|
|
|
value: 'listCreated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'list.deleted',
|
|
|
|
value: 'listDeleted',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'list.updated',
|
|
|
|
value: 'listUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'space.created',
|
|
|
|
value: 'spaceCreated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'space.deleted',
|
|
|
|
value: 'spaceDeleted',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'space.updated',
|
|
|
|
value: 'spaceUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'task.assignee.updated',
|
|
|
|
value: 'taskAssigneeUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'task.comment.posted',
|
|
|
|
value: 'taskCommentPosted',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'task.comment.updated',
|
|
|
|
value: 'taskCommentUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'task.created',
|
|
|
|
value: 'taskCreated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'task.deleted',
|
|
|
|
value: 'taskDeleted',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'task.dueDate.updated',
|
|
|
|
value: 'taskDueDateUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'task.moved',
|
|
|
|
value: 'taskMoved',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'task.status.updated',
|
|
|
|
value: 'taskStatusUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'task.tag.updated',
|
|
|
|
value: 'taskTagUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'task.timeEstimate.updated',
|
|
|
|
value: 'taskTimeEstimateUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -08:00
|
|
|
name: 'task.timeTracked.updated',
|
|
|
|
value: 'taskTimeTrackedUpdated',
|
2020-01-18 14:19:31 -08:00
|
|
|
},
|
|
|
|
{
|
2020-01-18 18:36:57 -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: '',
|
|
|
|
},
|
2020-01-18 18:36:57 -08:00
|
|
|
{
|
|
|
|
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 he can
|
|
|
|
// 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;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// @ts-ignore
|
|
|
|
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) {
|
2021-03-24 15:01:12 -07:00
|
|
|
body.list_id = (filters.listId as string).replace('#', '');
|
2020-01-18 14:19:31 -08:00
|
|
|
}
|
|
|
|
if (filters.taskId) {
|
2021-03-24 15:01:12 -07:00
|
|
|
body.task_id = (filters.taskId as string).replace('#', '');
|
2020-01-18 14:19:31 -08:00
|
|
|
}
|
|
|
|
if (filters.spaceId) {
|
2021-03-24 15:01:12 -07:00
|
|
|
body.space_id = (filters.spaceId as string).replace('#', '');
|
2020-01-18 14:19:31 -08:00
|
|
|
}
|
|
|
|
if (filters.folderId) {
|
2021-03-24 15:01:12 -07:00
|
|
|
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);
|
2021-03-24 15:01:12 -07:00
|
|
|
} 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');
|
|
|
|
if (headerData['x-signature'] !== computedSignature) {
|
|
|
|
// Signature is not valid so ignore call
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
workflowData: [
|
|
|
|
this.helpers.returnJsonArray(req.body),
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|