Skip to content

Commit

Permalink
feat: filter type functions Added function to check if regex filter r…
Browse files Browse the repository at this point in the history
…adio button is enabled. If yes then modify the filter type to be regex.

Signed-off-by: Wilfred Almeida <60785452+WilfredAlmeida@users.noreply.github.com>
  • Loading branch information
WilfredAlmeida committed Aug 14, 2023
1 parent f8c14b3 commit 2d4f2e7
Showing 1 changed file with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
showChunkOption: boolean = false;
stringForLabelFilter: string = '';
copyStringForLabelFilter: string = '';
regexFilterModeEnabled: boolean;
constructor(
private fb: UntypedFormBuilder,
private repService: ReplicationService,
Expand Down Expand Up @@ -375,7 +376,7 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
this.stringForLabelFilter = '';
this.copyStringForLabelFilter = '';
rule.filters.forEach(item => {
if (item.type === FilterType.LABEL) {
if (item.type === FilterType.LABEL || item.type === FilterType.LABEL_REGEX) {
this.stringForLabelFilter = (item.value as string[]).join(
','
);
Expand Down Expand Up @@ -443,14 +444,32 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
value: filter.value,
});
}

if (filter.type === FilterType.LABEL_REGEX) {
let fbLabel = this.fb.group({
type: FilterType.LABEL_REGEX,
decoration: filter.decoration || Decoration.MATCHES,
});
let filterLabel = this.fb.array(filter.value);
fbLabel.setControl('value', filterLabel);
return fbLabel;
}
if (filter.type === FilterType.TAG_REGEX) {
return this.fb.group({
type: FilterType.TAG_REGEX,
decoration: filter.decoration || Decoration.MATCHES,
value: filter.value,
});
}

return this.fb.group(filter);
});
const filterFormArray = this.fb.array(filterFGs);
this.ruleForm.setControl('filters', filterFormArray);
}

initFilter(name: string) {
if (name === FilterType.LABEL) {
if (name === FilterType.LABEL || name === FilterType.LABEL_REGEX ) {
const labelArray = this.fb.array([]);
const labelControl = this.fb.group({
type: name,
Expand All @@ -459,7 +478,7 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
labelControl.setControl('value', labelArray);
return labelControl;
}
if (name === FilterType.TAG) {
if (name === FilterType.TAG || name === FilterType.TAG_REGEX) {
return this.fb.group({
type: name,
decoration: Decoration.MATCHES,
Expand Down Expand Up @@ -504,6 +523,28 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
return !isEmptyObject(this.hasChanges());
}


enableRegexFilterMode(): void{
this.regexFilterModeEnabled = true;
}

disableRegexFilterMode(): void{
this.regexFilterModeEnabled = false;
}

isValidRegexPattern(pattern: string): boolean {
if(pattern[0] === '/' || pattern[pattern.length-1] === '/'){
return false
}
try {
new RegExp(pattern);
return true;
} catch (error) {
return false;
}
}


onSubmit() {
if (this.ruleForm.value.trigger.type !== 'scheduled') {
this.ruleForm
Expand All @@ -530,12 +571,29 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
copyRuleForm.dest_registry = null;
}
let filters: any = copyRuleForm.filters;

// check if the filter mode is regex
// if it is, validate the pattern and set the filter type
if(this.regexFilterModeEnabled === true){
filters.forEach((filter)=>{
if(!this.isValidRegexPattern(filter.value)){
alert("Enter a valid regex pattern")
return
}
if(filter.type === "tag"){
filter.type = "tagRegex"
}
if(filter.type === "label"){
filter.type = "labelRegex"
}
})
}

// set label filter
if (this.stringForLabelFilter) {
// set stringForLabelFilter
copyRuleForm.filters.forEach(item => {
if (item.type === FilterType.LABEL) {
if (item.type === FilterType.LABEL || item.type === FilterType.LABEL_REGEX ) {
item.value = this.stringForLabelFilter
.split(',')
.filter(item => item);
Expand Down Expand Up @@ -708,7 +766,7 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
}

if (!findTag) {
if (this.supportedFilters[i].type === FilterType.LABEL) {
if (this.supportedFilters[i].type === FilterType.LABEL || this.supportedFilters[i].type === FilterType.LABEL_REGEX) {
filtersArray.push({
type: this.supportedFilters[i].type,
value: [],
Expand Down

0 comments on commit 2d4f2e7

Please sign in to comment.