Skip to content

Commit

Permalink
feat(core): Introduce simplified node versioning (#3205)
Browse files Browse the repository at this point in the history
* ✨ Introduce simple node versioning

* ⚡ Add example how to read version in node-code for custom logic

* 🐛 Fix setting of parameters

* 🐛 Fix another instance where it sets the wrong parameter

* ⚡ Remove unnecessary TOODs

* ⚡ Revert Set Node example changes

* ;rotating_light: Add test
  • Loading branch information
janober authored Apr 28, 2022
1 parent 5e2589e commit d5b9b0c
Show file tree
Hide file tree
Showing 19 changed files with 277 additions and 31 deletions.
7 changes: 4 additions & 3 deletions packages/cli/src/CredentialsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export class CredentialsHelper extends ICredentialsHelper {
decryptedDataOriginal as INodeParameters,
true,
false,
null,
) as ICredentialDataDecryptedObject;

if (decryptedDataOriginal.oauthTokenData !== undefined) {
Expand Down Expand Up @@ -436,8 +437,6 @@ export class CredentialsHelper extends ICredentialsHelper {
// Add special database related data
newCredentialsData.updatedAt = new Date();

// TODO: also add user automatically depending on who is logged in, if anybody is logged in

// Save the credentials in DB
const findQuery = {
id: credentials.id,
Expand Down Expand Up @@ -562,7 +561,9 @@ export class CredentialsHelper extends ICredentialsHelper {
parameters: {},
name: 'Temp-Node',
type: nodeType.description.name,
typeVersion: nodeType.description.version,
typeVersion: Array.isArray(nodeType.description.version)
? nodeType.description.version.slice(-1)[0]
: nodeType.description.version,
position: [0, 0],
};

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,7 @@ export async function getCredentials(
!NodeHelpers.displayParameter(
additionalData.currentNodeParameters || node.parameters,
nodeCredentialDescription,
node,
node.parameters,
)
) {
Expand Down
59 changes: 59 additions & 0 deletions packages/core/test/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,65 @@ class NodeTypesClass implements INodeTypes {
},
},
},
'n8n-nodes-base.versionTest': {
sourcePath: '',
type: {
description: {
displayName: 'Version Test',
name: 'versionTest',
group: ['input'],
version: 1,
description: 'Tests if versioning works',
defaults: {
name: 'Version Test',
color: '#0000FF',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Display V1',
name: 'versionTest',
type: 'number',
displayOptions: {
show: {
'@version': [1],
},
},
default: 1,
},
{
displayName: 'Display V2',
name: 'versionTest',
type: 'number',
displayOptions: {
show: {
'@version': [2],
},
},
default: 2,
},
],
},
execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];

for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
const newItem: INodeExecutionData = {
json: {
versionFromParameter: this.getNodeParameter('versionTest', itemIndex),
versionFromNode: this.getNode().typeVersion,
},
};

returnData.push(newItem);
}

return this.prepareOutputData(returnData);
},
},
},
'n8n-nodes-base.set': {
sourcePath: '',
type: {
Expand Down
139 changes: 139 additions & 0 deletions packages/core/test/WorkflowExecute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,145 @@ describe('WorkflowExecute', () => {
},
},
},

{
description:
'should display the correct parameters and so correct data when simplified node-versioning is used',
input: {
workflowData: {
nodes: [
{
parameters: {},
name: 'Start',
type: 'n8n-nodes-base.start',
typeVersion: 1,
position: [240, 300],
},
{
parameters: {},
name: 'VersionTest1a',
type: 'n8n-nodes-base.versionTest',
typeVersion: 1,
position: [460, 300],
},
{
parameters: {
versionTest: 11,
},
name: 'VersionTest1b',
type: 'n8n-nodes-base.versionTest',
typeVersion: 1,
position: [680, 300],
},
{
parameters: {},
name: 'VersionTest2a',
type: 'n8n-nodes-base.versionTest',
typeVersion: 2,
position: [880, 300],
},
{
parameters: {
versionTest: 22,
},
name: 'VersionTest2b',
type: 'n8n-nodes-base.versionTest',
typeVersion: 2,
position: [1080, 300],
},
],
connections: {
Start: {
main: [
[
{
node: 'VersionTest1a',
type: 'main',
index: 0,
},
],
],
},
VersionTest1a: {
main: [
[
{
node: 'VersionTest1b',
type: 'main',
index: 0,
},
],
],
},
VersionTest1b: {
main: [
[
{
node: 'VersionTest2a',
type: 'main',
index: 0,
},
],
],
},
VersionTest2a: {
main: [
[
{
node: 'VersionTest2b',
type: 'main',
index: 0,
},
],
],
},
},
},
},
output: {
nodeExecutionOrder: [
'Start',
'VersionTest1a',
'VersionTest1b',
'VersionTest2a',
'VersionTest2b',
],
nodeData: {
VersionTest1a: [
[
{
versionFromNode: 1,
versionFromParameter: 1,
},
],
],
VersionTest1b: [
[
{
versionFromNode: 1,
versionFromParameter: 11,
},
],
],
VersionTest2a: [
[
{
versionFromNode: 2,
versionFromParameter: 2,
},
],
],
VersionTest2b: [
[
{
versionFromNode: 2,
versionFromParameter: 22,
},
],
],
},
},
},
];

const fakeLogger = {
Expand Down
6 changes: 5 additions & 1 deletion packages/editor-ui/src/components/CollectionParameter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<script lang="ts">
import {
INodeUi,
IUpdateInformation,
} from '@/Interface';
Expand Down Expand Up @@ -87,6 +88,9 @@ export default mixins(
return this.displayNodeParameter(option as INodeProperties);
});
},
node (): INodeUi {
return this.$store.getters.activeNode;
},
// Returns all the options which did not get added already
parameterOptions (): Array<INodePropertyOptions | INodeProperties> {
return (this.filteredOptions as Array<INodePropertyOptions | INodeProperties>).filter((option) => {
Expand Down Expand Up @@ -127,7 +131,7 @@ export default mixins(
// If it is not defined no need to do a proper check
return true;
}
return this.displayParameter(this.nodeValues, parameter, this.path);
return this.displayParameter(this.nodeValues, parameter, this.path, this.node);
},
optionSelected (optionName: string) {
const options = this.getOptionProperties(optionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ export default mixins(showMessage, nodeHelpers).extend({
this.credentialData as INodeParameters,
parameter,
'',
null,
);
},
getCredentialProperties(name: string): INodeProperties[] {
Expand Down Expand Up @@ -598,6 +599,7 @@ export default mixins(showMessage, nodeHelpers).extend({
this.credentialData as INodeParameters,
false,
false,
null,
);
const credentialDetails: ICredentialsDecrypted = {
Expand Down
2 changes: 1 addition & 1 deletion packages/editor-ui/src/components/NodeCredentials.vue
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export default mixins(
// If it is not defined no need to do a proper check
return true;
}
return this.displayParameter(this.node.parameters, credentialTypeDescription, '');
return this.displayParameter(this.node.parameters, credentialTypeDescription, '', this.node);
},
getIssues (credentialTypeName: string): string[] {
Expand Down
4 changes: 2 additions & 2 deletions packages/editor-ui/src/components/NodeSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export default mixins(
}
// Get only the parameters which are different to the defaults
let nodeParameters = NodeHelpers.getNodeParameters(nodeType.properties, node.parameters, false, false);
let nodeParameters = NodeHelpers.getNodeParameters(nodeType.properties, node.parameters, false, false, node);
const oldNodeParameters = Object.assign({}, nodeParameters);
// Copy the data because it is the data of vuex so make sure that
Expand Down Expand Up @@ -415,7 +415,7 @@ export default mixins(
// Get the parameters with the now new defaults according to the
// from the user actually defined parameters
nodeParameters = NodeHelpers.getNodeParameters(nodeType.properties, nodeParameters as INodeParameters, true, false);
nodeParameters = NodeHelpers.getNodeParameters(nodeType.properties, nodeParameters as INodeParameters, true, false, node);
for (const key of Object.keys(nodeParameters as object)) {
if (nodeParameters && nodeParameters[key] !== null && nodeParameters[key] !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion packages/editor-ui/src/components/ParameterInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ export default mixins(
const newPath = this.shortPath.split('.');
newPath.pop();
const issues = NodeHelpers.getParameterIssues(this.parameter, this.node.parameters, newPath.join('.'));
const issues = NodeHelpers.getParameterIssues(this.parameter, this.node.parameters, newPath.join('.'), this.node);
if (['options', 'multiOptions'].includes(this.parameter.type) && this.remoteParameterOptionsLoading === false && this.remoteParameterOptionsLoadingIssues === null) {
// Check if the value resolves to a valid option
Expand Down
11 changes: 7 additions & 4 deletions packages/editor-ui/src/components/ParameterInputList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import {
NodeParameterValue,
} from 'n8n-workflow';
import { IUpdateInformation } from '@/Interface';
import { INodeUi, IUpdateInformation } from '@/Interface';
import MultipleParameter from '@/components/MultipleParameter.vue';
import { genericHelpers } from '@/components/mixins/genericHelpers';
Expand Down Expand Up @@ -124,6 +124,9 @@ export default mixins(
filteredParameterNames (): string[] {
return this.filteredParameters.map(parameter => parameter.name);
},
node (): INodeUi {
return this.$store.getters.activeNode;
},
},
methods: {
multipleValues (parameter: INodeProperties): boolean {
Expand Down Expand Up @@ -213,13 +216,13 @@ export default mixins(
if (this.path) {
rawValues = JSON.parse(JSON.stringify(this.nodeValues));
set(rawValues, this.path, nodeValues);
return this.displayParameter(rawValues, parameter, this.path);
return this.displayParameter(rawValues, parameter, this.path, this.node);
} else {
return this.displayParameter(nodeValues, parameter, '');
return this.displayParameter(nodeValues, parameter, '', this.node);
}
}
return this.displayParameter(this.nodeValues, parameter, this.path);
return this.displayParameter(this.nodeValues, parameter, this.path, this.node);
},
valueChanged (parameterData: IUpdateInformation): void {
this.$emit('valueChanged', parameterData);
Expand Down
6 changes: 3 additions & 3 deletions packages/editor-ui/src/components/mixins/nodeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export const nodeHelpers = mixins(
},

// Returns if the given parameter should be displayed or not
displayParameter (nodeValues: INodeParameters, parameter: INodeProperties | INodeCredentialDescription, path: string) {
return NodeHelpers.displayParameterPath(nodeValues, parameter, path);
displayParameter (nodeValues: INodeParameters, parameter: INodeProperties | INodeCredentialDescription, path: string, node: INodeUi | null) {
return NodeHelpers.displayParameterPath(nodeValues, parameter, path, node);
},

// Returns all the issues of the node
Expand Down Expand Up @@ -200,7 +200,7 @@ export const nodeHelpers = mixins(
let selectedCredentials: INodeCredentialsDetails;
for (const credentialTypeDescription of nodeType!.credentials!) {
// Check if credentials should be displayed else ignore
if (this.displayParameter(node.parameters, credentialTypeDescription, '') !== true) {
if (this.displayParameter(node.parameters, credentialTypeDescription, '', node) !== true) {
continue;
}

Expand Down
Loading

0 comments on commit d5b9b0c

Please sign in to comment.