n8n/packages/nodes-base/nodes/Kitemaker/Kitemaker.node.ts

334 lines
8.4 KiB
TypeScript
Raw Normal View History

import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodeType,
refactor: Apply more `eslint-plugin-n8n-nodes-base` rules (#3624) * :arrow_up: Upgrade `eslint-plugin-n8n-nodes-base` * :package: Update `package-lock.json` * :wrench: Adjust renamed filesystem rules * :pencil2: Alphabetize ruleset * :zap: Categorize overrides * :zap: Set renamings in lint exceptions * :zap: Run baseline `lintfix` * :zap: Update linting scripts * :shirt: Apply `node-param-description-missing-from-dynamic-multi-options` * :shirt: Apply `cred-class-field-name-missing-oauth2` (#3627) * Rule working as intended * Removed comments * Move cred rule to different rule set * :shirt: Apply `node-param-array-type-assertion` * :shirt: Apply `node-dirname-against-convention` * Apply `cred-class-field-display-name-oauth2` (#3628) * Apply `node-execute-block-wrong-error-thrown` * Apply `node-class-description-display-name-unsuffixed-trigger-node` * Apply `node-class-description-name-unsuffixed-trigger-node` * Apply `cred-class-name-missing-oauth2-suffix` (#3636) * Rule working as intended, add exception to existing nodes * :shirt: Apply `cred-class-field-name-uppercase-first-char` (#3638) * :arrow_up: Upgrade to plugin version 1.2.28 * :package: Update `package-lock.json` * :shirt: Update lintings with 1.2.8 change * :shirt: Apply `cred-class-field-name-unsuffixed` * :shirt: Apply `cred-class-name-unsuffixed` * :shirt: Apply `node-class-description-credentials-name-unsuffixed` * :pencil2: Alphabetize rules * :heavy_minus_sign: Remove `nodelinter` package * :package: Update `package-lock.json` * :zap: Consolidate `lint` and `lintfix` scripts Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: agobrech <ael.gobrecht@gmail.com>
2022-07-04 02:12:08 -07:00
INodeTypeDescription,
NodeOperationError,
} from 'n8n-workflow';
import {
organizationOperations,
spaceFields,
spaceOperations,
userFields,
userOperations,
workItemFields,
workItemOperations,
} from './descriptions';
import { createLoadOptions, kitemakerRequest, kitemakerRequestAllItems } from './GenericFunctions';
import {
getAllSpaces,
getAllUsers,
getAllWorkItems,
getLabels,
getOrganization,
getSpaces,
getStatuses,
getUsers,
getWorkItem,
getWorkItems,
} from './queries';
import { createWorkItem, editWorkItem } from './mutations';
export class Kitemaker implements INodeType {
description: INodeTypeDescription = {
displayName: 'Kitemaker',
name: 'kitemaker',
icon: 'file:kitemaker.svg',
group: ['input'],
version: 1,
subtitle: '={{$parameter["resource"] + ": " + $parameter["operation"]}}',
description: 'Consume the Kitemaker GraphQL API',
defaults: {
name: 'Kitemaker',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'kitemakerApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
refactor: Apply more nodelinting rules (#3324) * :pencil2: Alphabetize lint rules * :fire: Remove duplicates * :zap: Update `lintfix` script * :shirt: Apply `node-param-operation-without-no-data-expression` (#3329) * :shirt: Apply `node-param-operation-without-no-data-expression` * :shirt: Add exceptions * :shirt: Apply `node-param-description-weak` (#3328) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-value-duplicate` (#3331) * :shirt: Apply `node-param-description-miscased-json` (#3337) * :shirt: Apply `node-param-display-name-excess-inner-whitespace` (#3335) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-type-options-missing-from-limit` (#3336) * Rule workig as intended * :pencil2: Uncomment rules Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-name-duplicate` (#3338) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-wrong-for-simplify` (#3334) * :zap: fix * :zap: exceptions * :zap: changed rule ignoring from file to line * :shirt: Apply `node-param-resource-without-no-data-expression` (#3339) * :shirt: Apply `node-param-display-name-untrimmed` (#3341) * :shirt: Apply `node-param-display-name-miscased-id` (#3340) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-resource-with-plural-option` (#3342) * :shirt: Apply `node-param-description-wrong-for-upsert` (#3333) * :zap: fix * :zap: replaced record with contact in description * :zap: fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-description-identical-to-name` (#3343) * :shirt: Apply `node-param-option-name-containing-star` (#3347) * :shirt: Apply `node-param-display-name-wrong-for-update-fields` (#3348) * :shirt: Apply `node-param-option-name-wrong-for-get-all` (#3345) * :zap: fix * :zap: exceptions * :shirt: Apply node-param-display-name-wrong-for-simplify (#3344) * Rule working as intended * Uncomented other rules * :shirt: Undo and add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :zap: Alphabetize lint rules * :zap: Restore `lintfix` script Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
2022-05-20 14:47:24 -07:00
noDataExpression: true,
options: [
{
name: 'Organization',
value: 'organization',
},
{
name: 'Space',
value: 'space',
},
{
name: 'User',
value: 'user',
},
{
name: 'Work Item',
value: 'workItem',
},
],
default: 'workItem',
required: true,
},
...organizationOperations,
...spaceOperations,
...spaceFields,
...userOperations,
...userFields,
...workItemOperations,
...workItemFields,
],
};
methods = {
loadOptions: {
async getLabels(this: ILoadOptionsFunctions) {
const responseData = await kitemakerRequest.call(this, { query: getLabels });
const {
data: {
organization: { spaces },
},
} = responseData;
return createLoadOptions(spaces[0].labels);
},
async getSpaces(this: ILoadOptionsFunctions) {
const responseData = await kitemakerRequest.call(this, { query: getSpaces });
const {
data: {
organization: { spaces },
},
} = responseData;
return createLoadOptions(spaces);
},
async getStatuses(this: ILoadOptionsFunctions) {
const spaceId = this.getNodeParameter('spaceId', 0) as string;
if (!spaceId.length) {
throw new NodeOperationError(
this.getNode(),
'Please choose a space to set for the work item to create.',
);
}
const responseData = await kitemakerRequest.call(this, { query: getStatuses });
const {
data: {
organization: { spaces },
},
} = responseData;
const space = spaces.find((e: { [x: string]: string }) => e.id === spaceId);
return createLoadOptions(space.statuses);
},
async getUsers(this: ILoadOptionsFunctions) {
const responseData = await kitemakerRequest.call(this, { query: getUsers });
const {
data: {
organization: { users },
},
} = responseData;
return createLoadOptions(users);
},
async getWorkItems(this: ILoadOptionsFunctions) {
const spaceId = this.getNodeParameter('spaceId', 0) as string;
const responseData = await kitemakerRequest.call(this, {
query: getWorkItems,
variables: { spaceId },
});
const {
data: {
workItems: { workItems },
},
} = responseData;
return createLoadOptions(workItems);
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
let responseData;
const returnData: INodeExecutionData[] = [];
// https://github.com/kitemakerhq/docs/blob/main/kitemaker.graphql
for (let i = 0; i < items.length; i++) {
if (resource === 'organization') {
// *********************************************************************
// organization
// *********************************************************************
if (operation === 'get') {
// ----------------------------------
// organization: get
// ----------------------------------
responseData = await kitemakerRequest.call(this, {
query: getOrganization,
});
responseData = responseData.data.organization;
}
} else if (resource === 'space') {
// *********************************************************************
// space
// *********************************************************************
if (operation === 'getAll') {
// ----------------------------------
// space: getAll
// ----------------------------------
const allItems = await kitemakerRequestAllItems.call(this, {
query: getAllSpaces,
variables: {},
});
responseData = allItems;
}
} else if (resource === 'user') {
// *********************************************************************
// user
// *********************************************************************
if (operation === 'getAll') {
// ----------------------------------
// user: getAll
// ----------------------------------
const allItems = await kitemakerRequestAllItems.call(this, {
query: getAllUsers,
variables: {},
});
responseData = allItems;
}
} else if (resource === 'workItem') {
// *********************************************************************
// workItem
// *********************************************************************
if (operation === 'create') {
// ----------------------------------
// workItem: create
// ----------------------------------
const input = {
title: this.getNodeParameter('title', i) as string,
statusId: this.getNodeParameter('statusId', i) as string[],
};
if (!input.statusId.length) {
throw new NodeOperationError(
this.getNode(),
'Please enter a status to set for the work item to create.',
{ itemIndex: i },
);
}
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (Object.keys(additionalFields).length) {
Object.assign(input, additionalFields);
}
responseData = await kitemakerRequest.call(this, {
query: createWorkItem,
variables: { input },
});
responseData = responseData.data.createWorkItem.workItem;
} else if (operation === 'get') {
// ----------------------------------
// workItem: get
// ----------------------------------
const workItemId = this.getNodeParameter('workItemId', i) as string;
responseData = await kitemakerRequest.call(this, {
query: getWorkItem,
variables: { workItemId },
});
responseData = responseData.data.workItem;
} else if (operation === 'getAll') {
// ----------------------------------
// workItem: getAll
// ----------------------------------
const allItems = await kitemakerRequestAllItems.call(this, {
query: getAllWorkItems,
variables: {
spaceId: this.getNodeParameter('spaceId', i) as string,
},
});
responseData = allItems;
} else if (operation === 'update') {
// ----------------------------------
// workItem: update
// ----------------------------------
const input = {
id: this.getNodeParameter('workItemId', i),
};
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
if (!Object.keys(updateFields).length) {
throw new NodeOperationError(
this.getNode(),
'Please enter at least one field to update for the work item.',
{ itemIndex: i },
);
}
Object.assign(input, updateFields);
responseData = await kitemakerRequest.call(this, {
query: editWorkItem,
variables: { input },
});
responseData = responseData.data.editWorkItem.workItem;
}
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: i } },
);
returnData.push(...executionData);
}
return this.prepareOutputData(returnData);
}
}