-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement policy-level tag regex filtering
Tag regex filtering allows the user to filter tags based on a regular expression pattern and enables tag version extraction through capture group replacement reference. Fixes #73 Signed-off-by: Aurel Canciu <[email protected]>
- Loading branch information
Showing
12 changed files
with
236 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
Copyright 2021 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package policy | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
// RegexFilter represents a regular expression filter | ||
type RegexFilter struct { | ||
filtered map[string]string | ||
|
||
Regexp *regexp.Regexp | ||
Replace string | ||
} | ||
|
||
// NewRegexFilter constructs new RegexFilter object | ||
func NewRegexFilter(pattern string, replace string) (*RegexFilter, error) { | ||
m, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid regular expression pattern '%s': %w", pattern, err) | ||
} | ||
return &RegexFilter{ | ||
Regexp: m, | ||
Replace: replace, | ||
}, nil | ||
} | ||
|
||
// Apply will construct the filtered list of tags based on the provided list of tags | ||
func (f *RegexFilter) Apply(list []string) { | ||
f.filtered = map[string]string{} | ||
for _, item := range list { | ||
if f.Regexp.MatchString(item) { | ||
tag := item | ||
if f.Replace != "" { | ||
tag = f.Regexp.ReplaceAllString(item, f.Replace) | ||
} | ||
f.filtered[tag] = item | ||
} | ||
} | ||
} | ||
|
||
// Items returns the list of filtered tags | ||
func (f *RegexFilter) Items() []string { | ||
var filtered []string | ||
for k := range f.filtered { | ||
filtered = append(filtered, k) | ||
} | ||
return filtered | ||
} | ||
|
||
// GetOriginalTag returns the original tag before replace extraction | ||
func (f *RegexFilter) GetOriginalTag(tag string) string { | ||
return f.filtered[tag] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2021 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package policy | ||
|
||
import ( | ||
"reflect" | ||
"sort" | ||
"testing" | ||
) | ||
|
||
func TestRegexFilter(t *testing.T) { | ||
cases := []struct { | ||
label string | ||
tags []string | ||
pattern string | ||
extract string | ||
expected []string | ||
}{ | ||
{ | ||
label: "none", | ||
tags: []string{"a"}, | ||
expected: []string{"a"}, | ||
}, | ||
{ | ||
label: "valid pattern", | ||
tags: []string{"ver1", "ver2", "ver3", "rel1"}, | ||
pattern: "^ver", | ||
expected: []string{"ver1", "ver2", "ver3"}, | ||
}, | ||
{ | ||
label: "valid pattern with capture group", | ||
tags: []string{"ver1", "ver2", "ver3", "rel1"}, | ||
pattern: `ver(\d+)`, | ||
extract: `$1`, | ||
expected: []string{"1", "2", "3"}, | ||
}, | ||
} | ||
for _, tt := range cases { | ||
t.Run(tt.label, func(t *testing.T) { | ||
filter := newRegexFilter(tt.pattern, tt.extract) | ||
filter.Apply(tt.tags) | ||
r := sort.StringSlice(filter.Items()) | ||
if reflect.DeepEqual(r, tt.expected) { | ||
t.Errorf("incorrect value returned, got '%s', expected '%s'", r, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func newRegexFilter(pattern string, extract string) *RegexFilter { | ||
f, _ := NewRegexFilter(pattern, extract) | ||
return f | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.