ci: Validate load options methods in nodes-base (no-changelog) (#5862)

This commit is contained in:
Iván Ovejero 2023-04-12 15:46:11 +02:00 committed by GitHub
parent 4d5756cd01
commit 5227ccd75a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 106 additions and 16 deletions

View file

@ -9,25 +9,68 @@ LoggerProxy.init({
warn: console.warn.bind(console),
});
function findReferencedMethods(obj, refs = {}, latestName = '') {
for (const key in obj) {
if (key === 'name' && 'group' in obj) {
latestName = obj[key];
}
if (typeof obj[key] === 'object') {
findReferencedMethods(obj[key], refs, latestName);
}
if (key === 'loadOptionsMethod') {
refs[latestName] = refs[latestName]
? [...new Set([...refs[latestName], obj[key]])]
: [obj[key]];
}
}
return refs;
}
(async () => {
const loader = new PackageDirectoryLoader(packageDir);
await loader.loadAll();
await loader.loadAll({ withLoadOptionsMethods: true });
const credentialTypes = Object.values(loader.credentialTypes).map((data) => data.type);
const nodeTypes = Object.values(loader.nodeTypes)
const loaderNodeTypes = Object.values(loader.nodeTypes);
const definedMethods = loaderNodeTypes.reduce((acc, cur) => {
NodeHelpers.getVersionedNodeTypeAll(cur.type).forEach((type) => {
const methods = type.description?.__loadOptionsMethods;
if (!methods) return;
const { name } = type.description;
acc[name] = acc[name] ? acc[name].push(methods) : methods;
});
return acc;
}, {});
const nodeTypes = loaderNodeTypes
.map((data) => {
const nodeType = NodeHelpers.getVersionedNodeType(data.type);
NodeHelpers.applySpecialNodeParameters(nodeType);
return data.type;
})
.flatMap((nodeData) => {
const allNodeTypes = NodeHelpers.getVersionedNodeTypeAll(nodeData);
return allNodeTypes.map((element) => element.description);
return NodeHelpers.getVersionedNodeTypeAll(nodeData).map((item) => {
const { __loadOptionsMethods, ...rest } = item.description;
return rest;
});
});
const referencedMethods = findReferencedMethods(nodeTypes);
await Promise.all([
writeJSON('types/credentials.json', credentialTypes),
writeJSON('types/nodes.json', nodeTypes),
writeJSON('methods/defined.json', definedMethods),
writeJSON('methods/referenced.json', referencedMethods),
]);
})();

View file

@ -44,6 +44,8 @@ export abstract class DirectoryLoader {
types: Types = { nodes: [], credentials: [] };
withLoadOptionsMethods = false; // only for validation during build
constructor(
readonly directory: string,
protected readonly excludeNodes: string[] = [],
@ -103,6 +105,7 @@ export abstract class DirectoryLoader {
const currentVersionNode = tempNode.nodeVersions[tempNode.currentVersion];
this.addCodex({ node: currentVersionNode, filePath, isCustom });
nodeVersion = tempNode.currentVersion;
if (this.withLoadOptionsMethods) this.addLoadOptionsMethods(currentVersionNode);
if (currentVersionNode.hasOwnProperty('executeSingle')) {
Logger.warn(
@ -111,6 +114,7 @@ export abstract class DirectoryLoader {
);
}
} else {
if (this.withLoadOptionsMethods) this.addLoadOptionsMethods(tempNode);
// Short renaming to avoid type issues
nodeVersion = Array.isArray(tempNode.description.version)
@ -244,6 +248,12 @@ export abstract class DirectoryLoader {
}
}
private addLoadOptionsMethods(node: INodeType) {
if (node?.methods?.loadOptions) {
node.description.__loadOptionsMethods = Object.keys(node.methods.loadOptions);
}
}
private fixIconPath(
obj: INodeTypeDescription | INodeTypeBaseDescription | ICredentialType,
filePath: string,
@ -296,7 +306,9 @@ export class PackageDirectoryLoader extends DirectoryLoader {
this.packageName = this.packageJson.name;
}
override async loadAll() {
override async loadAll(options = { withLoadOptionsMethods: false }) {
this.withLoadOptionsMethods = options.withLoadOptionsMethods;
await this.readPackageJson();
const { n8n } = this.packageJson;

View file

@ -508,9 +508,6 @@ export const contactFields: INodeProperties[] = [
name: 'lead_source_id',
type: 'options',
default: '',
typeOptions: {
loadOptionsMethod: 'getLeadSources',
},
description:
'ID of the source where contact came from. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
@ -580,9 +577,6 @@ export const contactFields: INodeProperties[] = [
name: 'subscription_status',
type: 'options',
default: '',
typeOptions: {
loadOptionsMethod: 'getSubscriptionStatuses',
},
description:
'Status of subscription that the contact is in. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
@ -591,9 +585,6 @@ export const contactFields: INodeProperties[] = [
name: 'subscription_types',
type: 'options',
default: '',
typeOptions: {
loadOptionsMethod: 'getSubscriptionTypes',
},
description:
'Type of subscription that the contact is in. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},

View file

@ -184,7 +184,7 @@ export const paymentFields: INodeProperties[] = [
name: 'paymentId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getpayment',
loadOptionsMethod: 'getPayments',
},
default: '',
required: true,

View file

@ -21,7 +21,7 @@
"build:translations": "gulp build:translations",
"build:metadata": "pnpm n8n-generate-known && pnpm n8n-generate-ui-types",
"format": "prettier --write . --ignore-path ../../.prettierignore",
"lint": "eslint --quiet nodes credentials",
"lint": "eslint --quiet nodes credentials; node ./scripts/validate-load-options-methods.js",
"lintfix": "eslint nodes credentials --fix",
"watch": "tsc-watch -p tsconfig.build.json --onSuccess \"pnpm n8n-generate-ui-types\"",
"test": "jest"

View file

@ -0,0 +1,43 @@
let referencedMethods;
let definedMethods;
try {
referencedMethods = require('../dist/methods/referenced.json');
definedMethods = require('../dist/methods/defined.json');
} catch (error) {
console.error(
'Failed to find methods to validate. Please run `npm run n8n-generate-ui-types` first.',
);
process.exit(1);
}
const compareMethods = (base, other) => {
const result = [];
for (const [nodeName, methods] of Object.entries(base)) {
if (nodeName in other) {
const found = methods.filter((item) => !other[nodeName].includes(item));
if (found.length > 0) result.push({ [nodeName]: found });
}
}
return result;
};
const referencedButUndefined = compareMethods(referencedMethods, definedMethods);
if (referencedButUndefined.length > 0) {
console.error('ERROR: The following load options methods are referenced but undefined.');
console.error('Please fix or remove the references or define the methods.');
console.error(referencedButUndefined);
process.exit(1);
}
const definedButUnused = compareMethods(definedMethods, referencedMethods);
if (definedButUnused.length > 0) {
console.warn('Warning: The following load options methods are defined but unused.');
console.warn('Please consider using or removing the methods.');
console.warn(definedButUnused);
}

View file

@ -1430,6 +1430,7 @@ export interface INodeTypeDescription extends INodeTypeBaseDescription {
};
};
actions?: INodeActionTypeDescription[];
__loadOptionsMethods?: string[]; // only for validation during build
}
export interface INodeHookDescription {