Skip to content

Commit

Permalink
tag-convention when not specified defaulted to incremental to address…
Browse files Browse the repository at this point in the history
… a change request from upstream team (#373)

Co-authored-by: Ruchi Maheshwari <[email protected]>
  • Loading branch information
Ruchii-27 and Ruchi Maheshwari authored Oct 29, 2024
1 parent 16a623b commit 619fbff
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
14 changes: 7 additions & 7 deletions internal/cssc/cssc.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ func ApplyFilterAndGetFilteredList(ctx context.Context, acrClient api.AcrCLIClie
var filteredRepos []FilteredRepository
var artifactsNotFound []FilteredRepository

// Default is floating tag regex, only if tag convention is incremental, then use incremental tag regex
patchTagRegex := floatingTagRegex
if filter.TagConvention == Incremental {
patchTagRegex = incrementalTagRegex
// Default is incremental tag regex, only if tag convention is specified as floating, use floating tag regex
patchTagRegex := incrementalTagRegex
if filter.TagConvention == Floating {
patchTagRegex = floatingTagRegex
}

for _, filterRepo := range filter.Repositories {
Expand Down Expand Up @@ -239,10 +239,10 @@ func ApplyFilterAndGetFilteredList(ctx context.Context, acrClient api.AcrCLIClie
}
// This is needed to evaluate all versions of patch tags when original tag is specified in the filter
var re *regexp.Regexp
if filter.TagConvention == Incremental {
re = regexp.MustCompile(`^` + ftag + `(-[1-9]\d{0,2})?$`)
} else {
if filter.TagConvention == Floating {
re = regexp.MustCompile(`^` + ftag + `(-patched\d*)?$`)
} else {
re = regexp.MustCompile(`^` + ftag + `(-[1-9]\d{0,2})?$`)
}
for _, tag := range tagList {
if re.MatchString(*tag.Name) {
Expand Down
56 changes: 53 additions & 3 deletions internal/cssc/cssc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ func TestApplyFilterAndGetFilteredList(t *testing.T) {
assert.Equal(t, common.RepoName1, artifactsNotFound[0].Repository)
assert.Equal(t, common.TagName1, artifactsNotFound[0].Tag)
})
// 5. Success scenario with all the combination of filters
t.Run("AllFilterCombinationTest", func(t *testing.T) {
// 5. Success scenario with all the combination of filters when tag-convention is floating
t.Run("AllFilterCombinationTestWithFloatingTagConvention", func(t *testing.T) {
filter := Filter{
Version: "v1",
Version: "v1",
TagConvention: "floating",
Repositories: []Repository{
{
Repository: common.RepoName1,
Expand Down Expand Up @@ -151,6 +152,55 @@ func TestApplyFilterAndGetFilteredList(t *testing.T) {
assert.True(t, isInFilteredList(filteredRepositories, common.RepoName3, common.TagName1, common.TagName1FloatingTag))
assert.True(t, isInFilteredList(filteredRepositories, common.RepoName3, common.TagName2, common.TagName2FloatingTag))
})
// 6. Success scenario with all the combination of filters when tag-convention is empty (defaults to incremental)
t.Run("AllFilterCombinationTestWithIncrementalTagConvention", func(t *testing.T) {
filter := Filter{
Version: "v1",
Repositories: []Repository{
{
Repository: common.RepoName1,
Tags: []string{common.TagName1, common.TagName2}, // tags specified
Enabled: boolPtr(true),
},
{
Repository: common.RepoName2,
Tags: []string{"*"}, // * all tags
Enabled: boolPtr(true),
},
{
Repository: common.RepoName3,
Tags: []string{common.TagName1, common.TagName2},
Enabled: nil, // nil means enabled
},
{
Repository: common.RepoName4,
Tags: []string{common.TagName1, common.TagName2},
Enabled: boolPtr(false), // disabled repository for all tags
},
},
}
mockAcrClient.On("GetAcrTags", common.TestCtx, common.RepoName1, "", "").Return(common.FourTagsResultWithPatchTags, nil).Once()
mockAcrClient.On("GetAcrTags", common.TestCtx, common.RepoName1, "", common.TagName4FloatingTag).Return(common.EmptyListTagsResult, nil).Once()
mockAcrClient.On("GetAcrTags", common.TestCtx, common.RepoName2, "", "").Return(common.FourTagsResultWithPatchTags, nil).Once()
mockAcrClient.On("GetAcrTags", common.TestCtx, common.RepoName2, "", common.TagName4FloatingTag).Return(common.EmptyListTagsResult, nil).Once()
mockAcrClient.On("GetAcrTags", common.TestCtx, common.RepoName3, "", "").Return(common.FourTagsResultWithPatchTags, nil).Once()
mockAcrClient.On("GetAcrTags", common.TestCtx, common.RepoName3, "", common.TagName4FloatingTag).Return(common.EmptyListTagsResult, nil).Once()
mockAcrClient.On("GetAcrTags", common.TestCtx, common.RepoName4, "", "").Return(common.FourTagsResultWithPatchTags, nil).Once()
mockAcrClient.On("GetAcrTags", common.TestCtx, common.RepoName4, "", common.TagName4FloatingTag).Return(common.EmptyListTagsResult, nil).Once()
filteredRepositories, artifactsNotFound, err := ApplyFilterAndGetFilteredList(context.Background(), mockAcrClient, filter)
assert.NoError(t, err)
assert.Nil(t, artifactsNotFound)
assert.Len(t, artifactsNotFound, 0)
assert.Len(t, filteredRepositories, 8)
assert.True(t, isInFilteredList(filteredRepositories, common.RepoName1, common.TagName1, common.TagName1Incremental2))
assert.True(t, isInFilteredList(filteredRepositories, common.RepoName1, common.TagName2, common.TagName2Incremental2))
assert.True(t, isInFilteredList(filteredRepositories, common.RepoName2, common.TagName1, common.TagName1Incremental2))
assert.True(t, isInFilteredList(filteredRepositories, common.RepoName2, common.TagName2, common.TagName2Incremental2))
assert.True(t, isInFilteredList(filteredRepositories, common.RepoName2, common.TagName3, common.TagName3Incremental2))
assert.True(t, isInFilteredList(filteredRepositories, common.RepoName2, common.TagName4, common.TagName4Incremental2))
assert.True(t, isInFilteredList(filteredRepositories, common.RepoName3, common.TagName1, common.TagName1Incremental2))
assert.True(t, isInFilteredList(filteredRepositories, common.RepoName3, common.TagName2, common.TagName2Incremental2))
})
}

func TestGetFilterFromFilterPolicy(t *testing.T) {
Expand Down

0 comments on commit 619fbff

Please sign in to comment.