2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IHookFunctions,
|
2020-09-17 14:20:12 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
IWebhookFunctions,
|
|
|
|
IWebhookResponseData,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { getAutomaticSecret, taigaApiRequest } from './GenericFunctions';
|
2020-09-17 14:20:12 -07:00
|
|
|
|
|
|
|
// import {
|
|
|
|
// createHmac,
|
|
|
|
// } from 'crypto';
|
|
|
|
|
|
|
|
export class TaigaTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Taiga Trigger',
|
|
|
|
name: 'taigaTrigger',
|
2021-07-14 15:02:30 -07:00
|
|
|
icon: 'file:taiga.svg',
|
2020-09-17 14:20:12 -07:00
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{"project:" + $parameter["projectSlug"]}}',
|
|
|
|
description: 'Handle Taiga events via webhook',
|
|
|
|
defaults: {
|
|
|
|
name: 'Taiga Trigger',
|
|
|
|
},
|
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
2021-07-14 15:02:30 -07:00
|
|
|
name: 'taigaApi',
|
2020-09-17 14:20:12 -07:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
webhooks: [
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
httpMethod: 'POST',
|
|
|
|
responseMode: 'onReceived',
|
|
|
|
path: 'webhook',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
displayName: 'Project Name or ID',
|
2020-09-17 14:20:12 -07:00
|
|
|
name: 'projectId',
|
|
|
|
type: 'options',
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
2020-09-17 14:20:12 -07:00
|
|
|
typeOptions: {
|
|
|
|
loadOptionsMethod: 'getUserProjects',
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
required: true,
|
|
|
|
},
|
2021-07-21 23:10:26 -07:00
|
|
|
{
|
|
|
|
displayName: 'Resources',
|
|
|
|
name: 'resources',
|
|
|
|
type: 'multiOptions',
|
|
|
|
required: true,
|
2022-08-17 08:50:24 -07:00
|
|
|
default: ['all'],
|
2021-07-21 23:10:26 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'All',
|
|
|
|
value: 'all',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Issue',
|
|
|
|
value: 'issue',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Milestone (Sprint)',
|
|
|
|
value: 'milestone',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Task',
|
|
|
|
value: 'task',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'User Story',
|
|
|
|
value: 'userstory',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Wikipage',
|
|
|
|
value: 'wikipage',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
description: 'Resources to listen to',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operations',
|
|
|
|
name: 'operations',
|
|
|
|
type: 'multiOptions',
|
|
|
|
required: true,
|
2022-08-17 08:50:24 -07:00
|
|
|
default: ['all'],
|
2021-07-21 23:10:26 -07:00
|
|
|
description: 'Operations to listen to',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'All',
|
|
|
|
value: 'all',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Create',
|
|
|
|
value: 'create',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Delete',
|
|
|
|
value: 'delete',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Update',
|
|
|
|
value: 'change',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-09-17 14:20:12 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the available projects to display them to user so that they can
|
2020-09-17 14:20:12 -07:00
|
|
|
// select them easily
|
|
|
|
async getUserProjects(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
|
|
|
|
const { id } = await taigaApiRequest.call(this, 'GET', '/users/me');
|
|
|
|
|
2020-09-17 14:20:27 -07:00
|
|
|
const projects = await taigaApiRequest.call(this, 'GET', '/projects', {}, { member: id });
|
2020-09-17 14:20:12 -07:00
|
|
|
for (const project of projects) {
|
|
|
|
const projectName = project.name;
|
|
|
|
const projectId = project.id;
|
|
|
|
returnData.push({
|
|
|
|
name: projectName,
|
|
|
|
value: projectId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
webhookMethods = {
|
|
|
|
default: {
|
|
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default') as string;
|
|
|
|
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
const endpoint = '/webhooks';
|
2020-09-17 14:20:12 -07:00
|
|
|
|
|
|
|
const webhooks = await taigaApiRequest.call(this, 'GET', endpoint);
|
|
|
|
|
|
|
|
for (const webhook of webhooks) {
|
|
|
|
if (webhook.url === webhookUrl) {
|
|
|
|
webhookData.webhookId = webhook.id;
|
|
|
|
webhookData.key = webhook.key;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('taigaApi');
|
2020-09-17 14:20:12 -07:00
|
|
|
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default') as string;
|
|
|
|
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
|
|
|
|
const projectId = this.getNodeParameter('projectId') as string;
|
|
|
|
|
|
|
|
const key = getAutomaticSecret(credentials);
|
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
name: `n8n-webhook:${webhookUrl}`,
|
|
|
|
url: webhookUrl,
|
2021-07-21 23:10:26 -07:00
|
|
|
key,
|
2020-09-17 14:20:12 -07:00
|
|
|
project: projectId,
|
|
|
|
};
|
|
|
|
const { id } = await taigaApiRequest.call(this, 'POST', '/webhooks', body);
|
|
|
|
|
|
|
|
webhookData.webhookId = id;
|
|
|
|
webhookData.key = key;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
try {
|
|
|
|
await taigaApiRequest.call(this, 'DELETE', `/webhooks/${webhookData.webhookId}`);
|
2020-09-17 14:20:27 -07:00
|
|
|
} catch (error) {
|
2020-09-17 14:20:12 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delete webhookData.webhookId;
|
|
|
|
delete webhookData.key;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
2021-07-21 23:10:26 -07:00
|
|
|
const body = this.getRequestObject().body as WebhookPayload;
|
|
|
|
|
|
|
|
const operations = this.getNodeParameter('operations', []) as Operations[];
|
|
|
|
const resources = this.getNodeParameter('resources', []) as Resources[];
|
2020-09-17 14:20:12 -07:00
|
|
|
|
2021-07-21 23:10:26 -07:00
|
|
|
if (!operations.includes('all') && !operations.includes(body.action)) {
|
|
|
|
return {};
|
|
|
|
}
|
2020-09-17 14:20:12 -07:00
|
|
|
|
2021-07-21 23:10:26 -07:00
|
|
|
if (!resources.includes('all') && !resources.includes(body.type)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Signature does not match payload hash
|
2020-09-17 14:20:12 -07:00
|
|
|
// https://github.com/taigaio/taiga-back/issues/1031
|
|
|
|
|
2021-07-21 23:10:26 -07:00
|
|
|
// const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
// const headerData = this.getHeaderData();
|
|
|
|
|
|
|
|
// // @ts-ignore
|
|
|
|
// const requestSignature = headerData['x-taiga-webhook-signature'];
|
2020-09-17 14:20:12 -07:00
|
|
|
|
|
|
|
// if (requestSignature === undefined) {
|
|
|
|
// return {};
|
|
|
|
// }
|
|
|
|
|
2021-07-21 23:10:26 -07:00
|
|
|
// const computedSignature = createHmac('sha1', webhookData.key as string).update(JSON.stringify(body)).digest('hex');
|
2020-09-17 14:20:12 -07:00
|
|
|
|
|
|
|
// if (requestSignature !== computedSignature) {
|
|
|
|
// return {};
|
|
|
|
// }
|
|
|
|
|
|
|
|
return {
|
2022-08-17 08:50:24 -07:00
|
|
|
workflowData: [this.helpers.returnJsonArray(body)],
|
2020-09-17 14:20:12 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|