From 77409871e8cddf1892572026c220f403efc667b6 Mon Sep 17 00:00:00 2001 From: AllForNothing Date: Wed, 3 Mar 2021 13:25:19 +0800 Subject: [PATCH] Improve double star validator on UI Signed-off-by: AllForNothing --- .../add-p2p-policy.component.ts | 12 ++++++--- .../p2p-provider/policy/policy.component.ts | 27 ++++++++++++++++--- .../add-rule/add-rule.component.ts | 18 ++++++++++--- .../immutable-tag/immutable-tag.component.ts | 6 ++++- .../add-rule/add-rule.component.ts | 18 ++++++++++--- .../tag-retention/tag-retention.component.ts | 6 ++++- 6 files changed, 70 insertions(+), 17 deletions(-) diff --git a/src/portal/src/app/project/p2p-provider/add-p2p-policy/add-p2p-policy.component.ts b/src/portal/src/app/project/p2p-provider/add-p2p-policy/add-p2p-policy.component.ts index 83ad9de9645..85215ec8243 100644 --- a/src/portal/src/app/project/p2p-provider/add-p2p-policy/add-p2p-policy.component.ts +++ b/src/portal/src/app/project/p2p-provider/add-p2p-policy/add-p2p-policy.component.ts @@ -208,21 +208,27 @@ export class AddP2pPolicyComponent implements OnInit, OnDestroy { policy.provider_id = +policy.provider_id; const filters: any[] = []; if (this.repos) { - if (this.repos.indexOf(",") !== -1) { + if (this.repos.indexOf(",") !== -1 + && this.repos.indexOf('{') === -1 + && this.repos.indexOf('}') === -1) { filters.push({type: FILTER_TYPE.REPOS, value: `{${this.repos}}`}); } else { filters.push({type: FILTER_TYPE.REPOS, value: this.repos}); } } if (this.tags) { - if (this.tags.indexOf(",") !== -1) { + if (this.tags.indexOf(",") !== -1 + && this.tags.indexOf('{') === -1 + && this.tags.indexOf('}') === -1) { filters.push({type: FILTER_TYPE.TAG, value: `{${this.tags}}`}); } else { filters.push({type: FILTER_TYPE.TAG, value: this.tags}); } } if (this.labels) { - if (this.labels.indexOf(",") !== -1) { + if (this.labels.indexOf(",") !== -1 + && this.labels.indexOf('{') === -1 + && this.labels.indexOf('}') === -1) { filters.push({type: FILTER_TYPE.LABEL, value: `{${this.labels}}`}); } else { filters.push({type: FILTER_TYPE.LABEL, value: this.labels}); diff --git a/src/portal/src/app/project/p2p-provider/policy/policy.component.ts b/src/portal/src/app/project/p2p-provider/policy/policy.component.ts index cd14b84be99..2670bdbb22b 100644 --- a/src/portal/src/app/project/p2p-provider/policy/policy.component.ts +++ b/src/portal/src/app/project/p2p-provider/policy/policy.component.ts @@ -258,6 +258,9 @@ export class PolicyComponent implements OnInit, OnDestroy { } editPolicy() { if (this.selectedRow) { + this.addP2pPolicyComponent.repos = null; + this.addP2pPolicyComponent.tags = null; + this.addP2pPolicyComponent.labels = null; this.addP2pPolicyComponent.isOpen = true; this.addP2pPolicyComponent.isEdit = true; this.addP2pPolicyComponent.inlineAlert.close(); @@ -266,13 +269,25 @@ export class PolicyComponent implements OnInit, OnDestroy { if (filter && filter.length) { filter.forEach(item => { if (item.type === FILTER_TYPE.REPOS && item.value) { - this.addP2pPolicyComponent.repos = item.value.replace(/[{}]/g, ""); + let str: string = item.value; + if (/^{\S+}$/.test(str)) { + return str.slice(1, str.length - 1); + } + this.addP2pPolicyComponent.repos = str; } if (item.type === FILTER_TYPE.TAG && item.value) { - this.addP2pPolicyComponent.tags = item.value.replace(/[{}]/g, ""); + let str: string = item.value; + if (/^{\S+}$/.test(str)) { + return str.slice(1, str.length - 1); + } + this.addP2pPolicyComponent.tags = str; } if (item.type === FILTER_TYPE.LABEL && item.value) { - this.addP2pPolicyComponent.labels = item.value.replace(/[{}]/g, ""); + let str: string = item.value; + if (/^{\S+}$/.test(str)) { + return str.slice(1, str.length - 1); + } + this.addP2pPolicyComponent.labels = str; } }); } @@ -433,7 +448,11 @@ export class PolicyComponent implements OnInit, OnDestroy { if (arr && arr.length) { for (let i = 0; i < arr.length; i++) { if (arr[i].type === type && arr[i].value) { - return (arr[i].value + "").replace(/[{}]/g, ""); + let str: string = arr[i].value; + if (/^{\S+}$/.test(str)) { + return str.slice(1, str.length - 1); + } + return str; } } } diff --git a/src/portal/src/app/project/tag-feature-integration/immutable-tag/add-rule/add-rule.component.ts b/src/portal/src/app/project/tag-feature-integration/immutable-tag/add-rule/add-rule.component.ts index 03353aa8f7c..0f1a939fec0 100644 --- a/src/portal/src/app/project/tag-feature-integration/immutable-tag/add-rule/add-rule.component.ts +++ b/src/portal/src/app/project/tag-feature-integration/immutable-tag/add-rule/add-rule.component.ts @@ -54,7 +54,8 @@ export class AddRuleComponent implements OnInit, OnDestroy { set repositories(repositories) { if (this.rule && this.rule.scope_selectors && this.rule.scope_selectors.repository && this.rule.scope_selectors.repository[0]) { - if (repositories.indexOf(",") !== -1) { + if (repositories.indexOf(",") !== -1 + && repositories.indexOf("{") === -1 && repositories.indexOf("}") === -1) { this.rule.scope_selectors.repository[0].pattern = "{" + repositories + "}"; } else { this.rule.scope_selectors.repository[0].pattern = repositories; @@ -65,7 +66,11 @@ export class AddRuleComponent implements OnInit, OnDestroy { get repositories() { if (this.rule && this.rule.scope_selectors && this.rule.scope_selectors.repository && this.rule.scope_selectors.repository[0] && this.rule.scope_selectors.repository[0].pattern) { - return this.rule.scope_selectors.repository[0].pattern.replace(/[{}]/g, ""); + let str: string = this.rule.scope_selectors.repository[0].pattern; + if (/^{\S+}$/.test(str)) { + return str.slice(1, str.length - 1); + } + return str; } return ""; } @@ -85,7 +90,8 @@ export class AddRuleComponent implements OnInit, OnDestroy { set tagsInput(tagsInput) { if (this.rule && this.rule.tag_selectors && this.rule.tag_selectors[0]) { - if (tagsInput.indexOf(",") !== -1) { + if (tagsInput.indexOf(",") !== -1 + && tagsInput.indexOf("{") === -1 && tagsInput.indexOf("}") === -1) { this.rule.tag_selectors[0].pattern = "{" + tagsInput + "}"; } else { this.rule.tag_selectors[0].pattern = tagsInput; @@ -95,7 +101,11 @@ export class AddRuleComponent implements OnInit, OnDestroy { get tagsInput() { if (this.rule && this.rule.tag_selectors && this.rule.tag_selectors[0] && this.rule.tag_selectors[0].pattern) { - return this.rule.tag_selectors[0].pattern.replace(/[{}]/g, ""); + let str: string = this.rule.tag_selectors[0].pattern; + if (/^{\S+}$/.test(str)) { + return str.slice(1, str.length - 1); + } + return str; } return ""; } diff --git a/src/portal/src/app/project/tag-feature-integration/immutable-tag/immutable-tag.component.ts b/src/portal/src/app/project/tag-feature-integration/immutable-tag/immutable-tag.component.ts index 35b4f5daa8f..35352109c85 100644 --- a/src/portal/src/app/project/tag-feature-integration/immutable-tag/immutable-tag.component.ts +++ b/src/portal/src/app/project/tag-feature-integration/immutable-tag/immutable-tag.component.ts @@ -161,7 +161,11 @@ export class ImmutableTagComponent implements OnInit { } formatPattern(pattern: string): string { - return pattern.replace(/[{}]/g, ""); + let str: string = pattern; + if (/^{\S+}$/.test(str)) { + return str.slice(1, str.length - 1); + } + return str; } getI18nKey(str: string) { diff --git a/src/portal/src/app/project/tag-feature-integration/tag-retention/add-rule/add-rule.component.ts b/src/portal/src/app/project/tag-feature-integration/tag-retention/add-rule/add-rule.component.ts index 53c60070808..c4c246855c6 100644 --- a/src/portal/src/app/project/tag-feature-integration/tag-retention/add-rule/add-rule.component.ts +++ b/src/portal/src/app/project/tag-feature-integration/tag-retention/add-rule/add-rule.component.ts @@ -93,7 +93,8 @@ export class AddRuleComponent implements OnInit, OnDestroy { } set repositories(repositories) { - if (repositories.indexOf(",") !== -1) { + if (repositories.indexOf(",") !== -1 + && repositories.indexOf("{") === -1 && repositories.indexOf("}") === -1) { this.rule.scope_selectors.repository[0].pattern = "{" + repositories + "}"; } else { this.rule.scope_selectors.repository[0].pattern = repositories; @@ -101,7 +102,11 @@ export class AddRuleComponent implements OnInit, OnDestroy { } get repositories() { - return this.rule.scope_selectors.repository[0].pattern.replace(/[{}]/g, ""); + let str: string = this.rule.scope_selectors.repository[0].pattern; + if (/^{\S+}$/.test(str)) { + return str.slice(1, str.length - 1); + } + return str; } get tagsSelect() { @@ -113,7 +118,8 @@ export class AddRuleComponent implements OnInit, OnDestroy { } set tagsInput(tagsInput) { - if (tagsInput.indexOf(",") !== -1) { + if (tagsInput.indexOf(",") !== -1 + && tagsInput.indexOf("{") === -1 && tagsInput.indexOf("}") === -1) { this.rule.tag_selectors[0].pattern = "{" + tagsInput + "}"; } else { this.rule.tag_selectors[0].pattern = tagsInput; @@ -121,7 +127,11 @@ export class AddRuleComponent implements OnInit, OnDestroy { } get tagsInput() { - return this.rule.tag_selectors[0].pattern.replace(/[{}]/g, ""); + let str: string = this.rule.tag_selectors[0].pattern; + if (/^{\S+}$/.test(str)) { + return str.slice(1, str.length - 1); + } + return str; } set untagged(untagged) { let extras = JSON.parse(this.rule.tag_selectors[0].extras); diff --git a/src/portal/src/app/project/tag-feature-integration/tag-retention/tag-retention.component.ts b/src/portal/src/app/project/tag-feature-integration/tag-retention/tag-retention.component.ts index 05ab254f94e..d2be867007a 100644 --- a/src/portal/src/app/project/tag-feature-integration/tag-retention/tag-retention.component.ts +++ b/src/portal/src/app/project/tag-feature-integration/tag-retention/tag-retention.component.ts @@ -467,7 +467,11 @@ export class TagRetentionComponent implements OnInit { } formatPattern(pattern: string): string { - return pattern.replace(/[{}]/g, ""); + let str: string = pattern; + if (/^{\S+}$/.test(str)) { + return str.slice(1, str.length - 1); + } + return str; } getI18nKey(str: string) {