2024-08-29 06:55:53 -07:00
|
|
|
import {
|
|
|
|
type IHookFunctions,
|
|
|
|
type IWebhookFunctions,
|
|
|
|
type INodeType,
|
|
|
|
type INodeTypeDescription,
|
|
|
|
type IWebhookResponseData,
|
|
|
|
NodeConnectionType,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2019-07-12 08:54:15 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { apiRequest } from './GenericFunctions';
|
2019-07-12 08:54:15 -07:00
|
|
|
|
2020-10-01 05:01:39 -07:00
|
|
|
// import { createHmac } from 'crypto';
|
2019-07-12 08:54:15 -07:00
|
|
|
|
|
|
|
export class TrelloTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Trello Trigger',
|
|
|
|
name: 'trelloTrigger',
|
2021-01-21 02:32:24 -08:00
|
|
|
icon: 'file:trello.svg',
|
2019-07-12 08:54:15 -07:00
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Starts the workflow when Trello events occur',
|
2019-07-12 08:54:15 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'Trello Trigger',
|
|
|
|
},
|
|
|
|
inputs: [],
|
2024-08-29 06:55:53 -07:00
|
|
|
outputs: [NodeConnectionType.Main],
|
2019-07-12 08:54:15 -07:00
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'trelloApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
webhooks: [
|
|
|
|
{
|
|
|
|
name: 'setup',
|
2020-05-25 02:40:36 -07:00
|
|
|
httpMethod: 'HEAD',
|
2019-08-28 08:16:09 -07:00
|
|
|
responseMode: 'onReceived',
|
2019-07-12 08:54:15 -07:00
|
|
|
path: 'webhook',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
httpMethod: 'POST',
|
2019-08-28 08:16:09 -07:00
|
|
|
responseMode: 'onReceived',
|
2019-07-12 08:54:15 -07:00
|
|
|
path: 'webhook',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Model ID',
|
|
|
|
name: 'id',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: '4d5ea62fd76aa1136000000c',
|
|
|
|
required: true,
|
|
|
|
description: 'ID of the model of which to subscribe to events',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
webhookMethods = {
|
|
|
|
default: {
|
|
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('trelloApi');
|
2019-07-12 08:54:15 -07:00
|
|
|
|
2020-02-13 18:33:26 -08:00
|
|
|
// Check all the webhooks which exist already if it is identical to the
|
|
|
|
// one that is supposed to get created.
|
|
|
|
const endpoint = `tokens/${credentials.apiToken}/webhooks`;
|
2019-07-12 08:54:15 -07:00
|
|
|
|
|
|
|
const responseData = await apiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
|
2020-02-13 18:33:26 -08:00
|
|
|
const idModel = this.getNodeParameter('id') as string;
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
2019-07-12 08:54:15 -07:00
|
|
|
|
2020-02-13 18:33:26 -08:00
|
|
|
for (const webhook of responseData) {
|
|
|
|
if (webhook.idModel === idModel && webhook.callbackURL === webhookUrl) {
|
|
|
|
// Set webhook-id to be sure that it can be deleted
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
webhookData.webhookId = webhook.id as string;
|
2019-07-12 08:54:15 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('trelloApi');
|
2019-07-12 08:54:15 -07:00
|
|
|
|
|
|
|
const idModel = this.getNodeParameter('id') as string;
|
|
|
|
|
|
|
|
const endpoint = `tokens/${credentials.apiToken}/webhooks`;
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
description: `n8n Webhook - ${idModel}`,
|
|
|
|
callbackURL: webhookUrl,
|
|
|
|
idModel,
|
|
|
|
};
|
|
|
|
|
|
|
|
const responseData = await apiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
|
|
|
|
if (responseData.id === undefined) {
|
|
|
|
// Required data is missing so was not successful
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
webhookData.webhookId = responseData.id as string;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
|
|
|
|
if (webhookData.webhookId !== undefined) {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('trelloApi');
|
2019-07-12 08:54:15 -07:00
|
|
|
|
|
|
|
const endpoint = `tokens/${credentials.apiToken}/webhooks/${webhookData.webhookId}`;
|
|
|
|
|
|
|
|
const body = {};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await apiRequest.call(this, 'DELETE', endpoint, body);
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
2019-07-12 08:54:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from the static workflow data so that it is clear
|
2023-03-03 09:49:19 -08:00
|
|
|
// that no webhooks are registered anymore
|
2019-07-12 08:54:15 -07:00
|
|
|
delete webhookData.webhookId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-10-11 04:02:44 -07:00
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
2019-07-12 08:54:15 -07:00
|
|
|
const webhookName = this.getWebhookName();
|
|
|
|
|
|
|
|
if (webhookName === 'setup') {
|
|
|
|
// Is a create webhook confirmation request
|
|
|
|
const res = this.getResponseObject();
|
|
|
|
res.status(200).end();
|
|
|
|
return {
|
|
|
|
noWebhookResponse: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const bodyData = this.getBodyData();
|
|
|
|
|
|
|
|
// TODO: Check why that does not work as expected even though it gets done as described
|
2022-05-15 10:48:17 -07:00
|
|
|
// https://developers.trello.com/page/webhooks
|
|
|
|
|
|
|
|
//const credentials = await this.getCredentials('trelloApi');
|
2019-07-12 08:54:15 -07:00
|
|
|
// // Check if the request is valid
|
|
|
|
// const headerData = this.getHeaderData() as IDataObject;
|
|
|
|
// const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
// const checkContent = JSON.stringify(bodyData) + webhookUrl;
|
|
|
|
// const computedSignature = createHmac('sha1', credentials.oauthSecret as string).update(checkContent).digest('base64');
|
|
|
|
// if (headerData['x-trello-webhook'] !== computedSignature) {
|
|
|
|
// // Signature is not valid so ignore call
|
|
|
|
// return {};
|
|
|
|
// }
|
|
|
|
|
|
|
|
return {
|
2022-08-17 08:50:24 -07:00
|
|
|
workflowData: [this.helpers.returnJsonArray(bodyData)],
|
2019-07-12 08:54:15 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|