Skip to content

Commit

Permalink
Improve double star validator on UI (#14357)
Browse files Browse the repository at this point in the history
Signed-off-by: AllForNothing <[email protected]>
  • Loading branch information
AllForNothing authored Mar 4, 2021
1 parent eb8b0b1 commit 340921d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
27 changes: 23 additions & 4 deletions src/portal/src/app/project/p2p-provider/policy/policy.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
});
}
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 "";
}
Expand All @@ -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;
Expand All @@ -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 "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,20 @@ 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;
}
}

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() {
Expand All @@ -113,15 +118,20 @@ 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;
}
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 340921d

Please sign in to comment.