From a6fd53fed8a38f81ad08bc23ded49716920d80d2 Mon Sep 17 00:00:00 2001 From: amanda Date: Tue, 20 Jul 2021 12:01:44 +0100 Subject: [PATCH 1/7] If array field is just a yaql expression, do not try and split as yaql expression might refer to array --- modules/st2-auto-form/fields/array.js | 11 ++++++++++- modules/st2-auto-form/fields/base.js | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/modules/st2-auto-form/fields/array.js b/modules/st2-auto-form/fields/array.js index 35e0e80b5..ce59476f5 100644 --- a/modules/st2-auto-form/fields/array.js +++ b/modules/st2-auto-form/fields/array.js @@ -15,7 +15,7 @@ import _ from 'lodash'; import validator from 'validator'; -import { BaseTextField, isJinja } from './base'; +import { BaseTextField, isJinja, isYaql } from './base'; const jsonCheck = (value) => { try { @@ -78,6 +78,10 @@ export default class ArrayField extends BaseTextField { return v; } + if (isYaql(v)) { + return v; + } + const { items } = this.props.spec || {}; return split(v) .map((v) => typeConversions(items && items.type, v)) @@ -93,7 +97,12 @@ export default class ArrayField extends BaseTextField { return v; } + if (isYaql(v)) { + return v; + } + const { secret } = this.props.spec || {}; + if (secret && v && !Array.isArray(v)) { return v; } diff --git a/modules/st2-auto-form/fields/base.js b/modules/st2-auto-form/fields/base.js index 5bcd15e87..0c387787b 100644 --- a/modules/st2-auto-form/fields/base.js +++ b/modules/st2-auto-form/fields/base.js @@ -34,6 +34,10 @@ export function isJinja(v) { return _.isString(v) && v.startsWith('{{') && v.endsWith('}}'); } +export function isYaql(v) { + return _.isString(v) && v.startsWith('<%') && v.endsWith('%>'); +} + // TODO: make controlled export class BaseTextField extends React.Component { static propTypes = { From 4df5b481a41af74545436b40c13d14c496782c7c Mon Sep 17 00:00:00 2001 From: amanda Date: Thu, 22 Jul 2021 14:34:56 +0100 Subject: [PATCH 2/7] Amend so keep [] around arrays that use YAQL so can distinguish between using an expression that returns an array and one that returns string --- .circleci/config.yml | 2 +- modules/st2-auto-form/fields/array.js | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e3671038a..a590ab470 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,7 +12,7 @@ jobs: DEPLOY_PACKAGES: 1 DEB: bionic focal RPM: el7 el8 - ST2_VERSION: "3.5dev" + ST2_VERSION: "3.6dev" ST2_HOST: localhost ST2_PROTOCOL: http ST2_USERNAME: st2admin diff --git a/modules/st2-auto-form/fields/array.js b/modules/st2-auto-form/fields/array.js index ce59476f5..610018f80 100644 --- a/modules/st2-auto-form/fields/array.js +++ b/modules/st2-auto-form/fields/array.js @@ -78,11 +78,18 @@ export default class ArrayField extends BaseTextField { return v; } - if (isYaql(v)) { + /* YAQL parameter without , assume is array parameter */ + if (isYaql(v) && !v.includes(",")) { return v; } const { items } = this.props.spec || {}; + + /* Trim [], required for when kept [] around YAQL parameter */ + if (v && v.startsWith("[") && v.endsWith("]")) { + v = v.substring(1, v.length-1).trim() + } + return split(v) .map((v) => typeConversions(items && items.type, v)) ; @@ -97,6 +104,7 @@ export default class ArrayField extends BaseTextField { return v; } + /* YAQL parameter without , assume is array parameter */ if (isYaql(v)) { return v; } @@ -107,6 +115,15 @@ export default class ArrayField extends BaseTextField { return v; } + /* + * Keep [] if after converting to comma separated string would be treated + * as YAQL, as need to distingish between when pass an array parameter or + * an array of string parameters. + */ + if (v && isYaql(v.join(', '))) { + return '['.concat(v.join(', '),']'); + } + return v ? v.join(', ') : ''; } From fa31986591573d3845abd5e644f8b491594a614e Mon Sep 17 00:00:00 2001 From: amanda Date: Thu, 22 Jul 2021 15:11:33 +0100 Subject: [PATCH 3/7] Fix lint errors --- modules/st2-auto-form/fields/array.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/st2-auto-form/fields/array.js b/modules/st2-auto-form/fields/array.js index 610018f80..835152c43 100644 --- a/modules/st2-auto-form/fields/array.js +++ b/modules/st2-auto-form/fields/array.js @@ -79,15 +79,15 @@ export default class ArrayField extends BaseTextField { } /* YAQL parameter without , assume is array parameter */ - if (isYaql(v) && !v.includes(",")) { + if (isYaql(v) && !v.includes(',')) { return v; } const { items } = this.props.spec || {}; /* Trim [], required for when kept [] around YAQL parameter */ - if (v && v.startsWith("[") && v.endsWith("]")) { - v = v.substring(1, v.length-1).trim() + if (v && v.startsWith('[') && v.endsWith(']')) { + v = v.substring(1, v.length-1).trim(); } return split(v) From 0ae66c43c90942a6a759225a3a81538420b769f9 Mon Sep 17 00:00:00 2001 From: amanda Date: Mon, 26 Jul 2021 11:56:17 +0100 Subject: [PATCH 4/7] See if keeping space helps - as does when do manually --- modules/st2-auto-form/fields/array.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/st2-auto-form/fields/array.js b/modules/st2-auto-form/fields/array.js index 835152c43..dd59214ca 100644 --- a/modules/st2-auto-form/fields/array.js +++ b/modules/st2-auto-form/fields/array.js @@ -104,7 +104,7 @@ export default class ArrayField extends BaseTextField { return v; } - /* YAQL parameter without , assume is array parameter */ + /* string which is YAQL */ if (isYaql(v)) { return v; } @@ -121,7 +121,7 @@ export default class ArrayField extends BaseTextField { * an array of string parameters. */ if (v && isYaql(v.join(', '))) { - return '['.concat(v.join(', '),']'); + return '[ '.concat(v.join(', '),' ]'); } return v ? v.join(', ') : ''; From 038496ab4269a954929bd3921808e12d65f6fb16 Mon Sep 17 00:00:00 2001 From: amanda Date: Thu, 29 Jul 2021 13:37:58 +0100 Subject: [PATCH 5/7] Resolve problem when after split got [ in array --- modules/st2-auto-form/fields/array.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/st2-auto-form/fields/array.js b/modules/st2-auto-form/fields/array.js index dd59214ca..3810ed756 100644 --- a/modules/st2-auto-form/fields/array.js +++ b/modules/st2-auto-form/fields/array.js @@ -78,24 +78,26 @@ export default class ArrayField extends BaseTextField { return v; } - /* YAQL parameter without , assume is array parameter */ + /* YAQL parameter that came input as single yaql */ if (isYaql(v) && !v.includes(',')) { return v; } const { items } = this.props.spec || {}; + let t = v; /* Trim [], required for when kept [] around YAQL parameter */ if (v && v.startsWith('[') && v.endsWith(']')) { - v = v.substring(1, v.length-1).trim(); + t = v.substring(1, v.length-1).trim(); } - return split(v) - .map((v) => typeConversions(items && items.type, v)) + return split(t) + .map((t) => typeConversions(items && items.type, t)) ; } toStateValue(v) { + if (jsonCheck(v)) { return JSON.stringify(v); } @@ -120,10 +122,11 @@ export default class ArrayField extends BaseTextField { * as YAQL, as need to distingish between when pass an array parameter or * an array of string parameters. */ - if (v && isYaql(v.join(', '))) { + if (v && Array.isArray(v) && isYaql(v.join(', ')) && v.length === 1) { return '[ '.concat(v.join(', '),' ]'); } + return v ? v.join(', ') : ''; } From ed2322dbd17b01dca4ecd38b0e4614e97173ef8d Mon Sep 17 00:00:00 2001 From: amanda Date: Thu, 29 Jul 2021 14:15:58 +0100 Subject: [PATCH 6/7] Add Yaql check on integer, as cannot save if put yaql for an int in composer --- modules/st2-auto-form/fields/integer.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/modules/st2-auto-form/fields/integer.js b/modules/st2-auto-form/fields/integer.js index 0345365ab..63a1c02f1 100644 --- a/modules/st2-auto-form/fields/integer.js +++ b/modules/st2-auto-form/fields/integer.js @@ -14,7 +14,7 @@ import validator from 'validator'; -import { BaseTextField, isJinja } from './base'; +import { BaseTextField, isJinja, isYaql } from './base'; export default class IntegerField extends BaseTextField { static icon = '12' @@ -24,6 +24,10 @@ export default class IntegerField extends BaseTextField { return v; } + if (isYaql(v)) { + return v; + } + if (this.props.name === 'timeout' || this.props.name === 'limit') { return v ; } @@ -37,6 +41,10 @@ export default class IntegerField extends BaseTextField { return v; } + if (isYaql(v)) { + return v; + } + return v ? v.toString(10) : ''; } From 1cc4c9f8d4e7fedd25ae661df9c2d669bb22e724 Mon Sep 17 00:00:00 2001 From: amanda Date: Thu, 29 Jul 2021 14:33:55 +0100 Subject: [PATCH 7/7] More changes needed for integer, so will handle separately --- modules/st2-auto-form/fields/integer.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/modules/st2-auto-form/fields/integer.js b/modules/st2-auto-form/fields/integer.js index 63a1c02f1..0345365ab 100644 --- a/modules/st2-auto-form/fields/integer.js +++ b/modules/st2-auto-form/fields/integer.js @@ -14,7 +14,7 @@ import validator from 'validator'; -import { BaseTextField, isJinja, isYaql } from './base'; +import { BaseTextField, isJinja } from './base'; export default class IntegerField extends BaseTextField { static icon = '12' @@ -24,10 +24,6 @@ export default class IntegerField extends BaseTextField { return v; } - if (isYaql(v)) { - return v; - } - if (this.props.name === 'timeout' || this.props.name === 'limit') { return v ; } @@ -41,10 +37,6 @@ export default class IntegerField extends BaseTextField { return v; } - if (isYaql(v)) { - return v; - } - return v ? v.toString(10) : ''; }