n8n/packages/nodes-base/nodes/Bitbucket/BitbucketTrigger.node.ts

330 lines
8.7 KiB
TypeScript
Raw Normal View History

2021-10-20 20:35:51 -07:00
import { OptionsWithUri } from 'request';
2020-01-05 15:47:55 -08:00
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
2021-10-20 20:35:51 -07:00
ICredentialsDecrypted,
ICredentialTestFunctions,
2020-01-06 10:33:22 -08:00
IDataObject,
2020-01-05 15:47:55 -08:00
ILoadOptionsFunctions,
INodePropertyOptions,
2020-01-06 10:33:22 -08:00
INodeType,
INodeTypeDescription,
IWebhookResponseData,
2021-10-20 20:35:51 -07:00
NodeCredentialTestResult,
2020-01-05 15:47:55 -08:00
} from 'n8n-workflow';
import {
bitbucketApiRequest,
bitbucketApiRequestAllItems,
} from './GenericFunctions';
export class BitbucketTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Bitbucket Trigger',
2020-05-12 06:08:19 -07:00
name: 'bitbucketTrigger',
icon: 'file:bitbucket.svg',
2020-01-05 15:47:55 -08:00
group: ['trigger'],
version: 1,
description: 'Handle Bitbucket events via webhooks',
defaults: {
name: 'Bitbucket Trigger',
2020-01-06 10:33:22 -08:00
color: '#0052cc',
2020-01-05 15:47:55 -08:00
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'bitbucketApi',
required: true,
2021-10-20 20:35:51 -07:00
testedBy: 'bitbucketApiTest',
2020-10-22 06:46:03 -07:00
},
2020-01-05 15:47:55 -08:00
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
required: true,
options: [
{
name: 'Repository',
value: 'repository',
},
{
name: 'Workspace',
value: 'workspace',
},
2020-01-05 15:47:55 -08:00
],
default: 'workspace',
2020-01-06 10:33:22 -08:00
description: 'The resource to operate on.',
2020-01-05 15:47:55 -08:00
},
{
displayName: 'Workspace',
name: 'workspace',
2020-01-05 15:47:55 -08:00
type: 'options',
displayOptions: {
show: {
resource: [
'workspace',
'repository',
2020-10-22 06:46:03 -07:00
],
},
2020-01-05 15:47:55 -08:00
},
typeOptions: {
loadOptionsMethod: 'getWorkspaces',
2020-01-05 15:47:55 -08:00
},
required: true,
default: '',
description: 'The repository of which to listen to the events.',
2020-01-05 15:47:55 -08:00
},
{
displayName: 'Events',
name: 'events',
type: 'multiOptions',
displayOptions: {
show: {
resource: [
'workspace',
2020-10-22 06:46:03 -07:00
],
},
2020-01-05 15:47:55 -08:00
},
typeOptions: {
loadOptionsMethod: 'getWorkspaceEvents',
2020-01-05 15:47:55 -08:00
},
options: [],
required: true,
default: [],
2020-01-06 10:33:22 -08:00
description: 'The events to listen to.',
2020-01-05 15:47:55 -08:00
},
{
displayName: 'Repository',
name: 'repository',
type: 'options',
displayOptions: {
show: {
resource: [
2020-10-22 06:46:03 -07:00
'repository',
],
},
2020-01-05 15:47:55 -08:00
},
typeOptions: {
loadOptionsMethod: 'getRepositories',
loadOptionsDependsOn: [
'workspace',
],
2020-01-05 15:47:55 -08:00
},
required: true,
default: '',
2020-01-06 10:33:22 -08:00
description: 'The repository of which to listen to the events.',
2020-01-05 15:47:55 -08:00
},
{
displayName: 'Events',
name: 'events',
type: 'multiOptions',
displayOptions: {
show: {
resource: [
2020-10-22 06:46:03 -07:00
'repository',
],
},
2020-01-05 15:47:55 -08:00
},
typeOptions: {
loadOptionsMethod: 'getRepositoriesEvents',
},
options: [],
required: true,
default: [],
2020-01-06 10:33:22 -08:00
description: 'The events to listen to.',
2020-01-05 15:47:55 -08:00
},
],
};
methods = {
2021-10-20 20:35:51 -07:00
credentialTest: {
async bitbucketApiTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<NodeCredentialTestResult> {
const credentials = credential.data;
const options: OptionsWithUri = {
method: 'GET',
auth: {
user: credentials!.username as string,
password: credentials!.appPassword as string,
},
uri: 'https://api.bitbucket.org/2.0/user',
json: true,
timeout: 5000,
};
try {
const response = await this.helpers.request(options);
if (!response.username) {
return {
status: 'Error',
message: `Token is not valid: ${response.error}`,
};
}
} catch (error) {
return {
status: 'Error',
message: `Settings are not valid: ${error}`,
};
}
return {
status: 'OK',
message: 'Authentication successful!',
};
},
},
2020-01-05 15:47:55 -08:00
loadOptions: {
async getWorkspaceEvents(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
2020-01-05 15:47:55 -08:00
const returnData: INodePropertyOptions[] = [];
const events = await bitbucketApiRequestAllItems.call(this, 'values', 'GET', '/hook_events/workspace');
2020-01-05 15:47:55 -08:00
for (const event of events) {
returnData.push({
name: event.event,
value: event.event,
description: event.description,
2020-01-05 15:47:55 -08:00
});
}
return returnData;
},
async getRepositoriesEvents(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const events = await bitbucketApiRequestAllItems.call(this, 'values', 'GET', '/hook_events/repository');
for (const event of events) {
returnData.push({
name: event.event,
value: event.event,
description: event.description,
2020-01-05 15:47:55 -08:00
});
}
return returnData;
},
async getRepositories(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const workspace = this.getCurrentNodeParameter('workspace') as string;
const repositories = await bitbucketApiRequestAllItems.call(this, 'values', 'GET', `/repositories/${workspace}`);
2020-01-05 15:47:55 -08:00
for (const repository of repositories) {
returnData.push({
name: repository.slug,
value: repository.slug,
description: repository.description,
2020-01-05 15:47:55 -08:00
});
}
return returnData;
},
async getWorkspaces(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
2020-01-05 15:47:55 -08:00
const returnData: INodePropertyOptions[] = [];
const workspaces = await bitbucketApiRequestAllItems.call(this, 'values', 'GET', `/workspaces`);
for (const workspace of workspaces) {
2020-01-05 15:47:55 -08:00
returnData.push({
name: workspace.name,
value: workspace.slug,
2020-01-05 15:47:55 -08:00
});
}
return returnData;
},
},
};
// @ts-ignore
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
2020-01-06 10:33:22 -08:00
let endpoint = '';
2020-01-05 15:47:55 -08:00
const resource = this.getNodeParameter('resource', 0) as string;
const workspace = this.getNodeParameter('workspace', 0) as string;
const webhookUrl = this.getNodeWebhookUrl('default');
2020-01-05 15:47:55 -08:00
const webhookData = this.getWorkflowStaticData('node');
if (resource === 'workspace') {
endpoint = `/workspaces/${workspace}/hooks`;
2020-01-05 15:47:55 -08:00
}
if (resource === 'repository') {
const repository = this.getNodeParameter('repository', 0) as string;
endpoint = `/repositories/${workspace}/${repository}/hooks`;
2020-01-05 15:47:55 -08:00
}
const { values: hooks } = await bitbucketApiRequest.call(this, 'GET', endpoint);
for (const hook of hooks) {
if (webhookUrl === hook.url && hook.active === true) {
webhookData.webhookId = hook.uuid.replace('{', '').replace('}', '');
return true;
}
2020-01-05 15:47:55 -08:00
}
return false;
2020-01-05 15:47:55 -08:00
},
async create(this: IHookFunctions): Promise<boolean> {
let responseData;
2020-01-06 10:33:22 -08:00
let endpoint = '';
2020-01-05 15:47:55 -08:00
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const events = this.getNodeParameter('events') as string[];
const resource = this.getNodeParameter('resource', 0) as string;
const workspace = this.getNodeParameter('workspace', 0) as string;
2020-01-05 15:47:55 -08:00
if (resource === 'workspace') {
endpoint = `/workspaces/${workspace}/hooks`;
2020-01-05 15:47:55 -08:00
}
if (resource === 'repository') {
const repository = this.getNodeParameter('repository', 0) as string;
endpoint = `/repositories/${workspace}/${repository}/hooks`;
2020-01-05 15:47:55 -08:00
}
const body: IDataObject = {
2020-01-06 10:33:22 -08:00
description: 'n8n webhook',
2020-01-05 15:47:55 -08:00
url: webhookUrl,
active: true,
events,
};
responseData = await bitbucketApiRequest.call(this, 'POST', endpoint, body);
webhookData.webhookId = responseData.uuid.replace('{', '').replace('}', '');
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
2020-01-06 10:33:22 -08:00
let endpoint = '';
2020-01-05 15:47:55 -08:00
const webhookData = this.getWorkflowStaticData('node');
const workspace = this.getNodeParameter('workspace', 0) as string;
2020-01-05 15:47:55 -08:00
const resource = this.getNodeParameter('resource', 0) as string;
if (resource === 'workspace') {
endpoint = `/workspaces/${workspace}/hooks/${webhookData.webhookId}`;
2020-01-05 15:47:55 -08:00
}
if (resource === 'repository') {
const repository = this.getNodeParameter('repository', 0) as string;
endpoint = `/repositories/${workspace}/${repository}/hooks/${webhookData.webhookId}`;
2020-01-05 15:47:55 -08:00
}
try {
await bitbucketApiRequest.call(this, 'DELETE', endpoint);
} catch (error) {
2020-01-05 15:47:55 -08:00
return false;
}
delete webhookData.webhookId;
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject();
const headerData = this.getHeaderData() as IDataObject;
const webhookData = this.getWorkflowStaticData('node');
if (headerData['x-hook-uuid'] !== webhookData.webhookId) {
return {};
}
return {
workflowData: [
2020-10-22 06:46:03 -07:00
this.helpers.returnJsonArray(req.body),
2020-01-05 15:47:55 -08:00
],
};
}
}