feat(sync): add tag excludeRegex filter (#2906)

Fix #2902

Signed-off-by: Vladimir Ermakov <vooon341@gmail.com>
This commit is contained in:
Vladimir Ermakov
2025-01-26 19:29:02 +01:00
committed by GitHub
parent d0de12d2d3
commit 22864a95c8
5 changed files with 80 additions and 2 deletions
+3 -2
View File
@@ -43,6 +43,7 @@ type Content struct {
}
type Tags struct {
Regex *string
Semver *bool
Regex *string
ExcludeRegex *string
Semver *bool
}
+33
View File
@@ -63,6 +63,13 @@ func (cm ContentManager) FilterTags(repo string, tags []string) ([]string, error
}
}
if content.Tags.ExcludeRegex != nil {
tags, err = excludeTagsByRegex(tags, *content.Tags.ExcludeRegex, cm.log)
if err != nil {
return []string{}, err
}
}
if content.Tags.Semver != nil && *content.Tags.Semver {
tags = filterTagsBySemver(tags, cm.log)
}
@@ -240,6 +247,32 @@ func filterTagsByRegex(tags []string, regex string, log log.Logger) ([]string, e
return filteredTags, nil
}
// excludeTagsByRegex filter-out images by tag regex given in the config.
func excludeTagsByRegex(tags []string, regex string, log log.Logger) ([]string, error) {
if len(tags) == 0 || regex == "" {
return tags, nil
}
filteredTags := make([]string, 0, len(tags))
log.Info().Str("excludeRegex", regex).Msg("filtering out tags using regex")
tagReg, err := regexp.Compile(regex)
if err != nil {
log.Error().Err(err).Str("excludeRegex", regex).Msg("failed to compile regex")
return filteredTags, err
}
for _, tag := range tags {
if !tagReg.MatchString(tag) {
filteredTags = append(filteredTags, tag)
}
}
return filteredTags, nil
}
// filterTagsBySemver filters tags by checking if they are semver compliant.
func filterTagsBySemver(tags []string, log log.Logger) []string {
filteredTags := []string{}
@@ -170,6 +170,7 @@ func TestGetContentByLocalRepo(t *testing.T) {
func TestFilterTags(t *testing.T) {
allTagsRegex := ".*"
badRegex := "[*"
excludeArchRegex := ".*(x86_64|aarch64|amd64|arm64)$"
semverFalse := false
semverTrue := true
testCases := []struct {
@@ -234,6 +235,33 @@ func TestFilterTags(t *testing.T) {
filteredTags: []string{},
err: false,
},
{
repo: "alpine",
content: []syncconf.Content{
{Prefix: "**", Tags: &syncconf.Tags{ExcludeRegex: &allTagsRegex}},
},
tags: []string{"v1", "v2", "v3"},
filteredTags: []string{},
err: false,
},
{
repo: "alpine",
content: []syncconf.Content{
{Prefix: "**", Tags: &syncconf.Tags{ExcludeRegex: &excludeArchRegex}},
},
tags: []string{"v1", "v2-x86_64", "v3-aarch64"},
filteredTags: []string{"v1"},
err: false,
},
{
repo: "repo",
content: []syncconf.Content{
{Prefix: "repo*", Tags: &syncconf.Tags{ExcludeRegex: &badRegex}},
},
tags: []string{"latest", "v2.0.1"},
filteredTags: []string{},
err: true,
},
}
Convey("Test FilterTags()", t, func() {