Skip to content

Commit

Permalink
feat(general): Support conflicted git modifiers custom tag (#354)
Browse files Browse the repository at this point in the history
* support external tags matching on conflicted git modifiers tag

* UT

* rename

* fallback to the default value in case of a conflict
  • Loading branch information
tronxd authored Mar 22, 2023
1 parent e8da739 commit 31f504a
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/common/tagging/external/tag_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func (t *TagGroup) CalculateTagValue(block structure.IBlock, tag Tag) (tags.ITag
retTag.Key = tag.GetKey()
retTag.Value = evaluateTemplateVariable(tag.defaultValue)
blockTags := append(block.GetExistingTags(), block.GetNewTags()...)
gitModifiersCounts := make(map[string]int)
if len(tag.matches) > 0 {
for _, matchEntry := range tag.matches {
for matchValue, matchObj := range matchEntry {
Expand All @@ -206,20 +207,26 @@ func (t *TagGroup) CalculateTagValue(block structure.IBlock, tag Tag) (tags.ITag
break
}
}
case []string:
for _, blockTag := range blockTags {
blockTagKey, blockTagValue := blockTag.GetKey(), blockTag.GetValue()
if blockTagKey == tagName {
if blockTagKey == tags.GitModifiersTagKey {
for _, val := range strings.Split(blockTagValue, "/") {
if utils.InSlice(tagMatchV, val) {
foundTag = true
break
case []string, []interface{}:
if tagMatchTypeSwitch, ok := tagMatchV.([]interface{}); ok {
tagMatchStrings := make([]string, len(tagMatchTypeSwitch))
for i := range tagMatchTypeSwitch {
tagMatchStrings[i] = tagMatchTypeSwitch[i].(string)
}

for _, blockTag := range blockTags {
blockTagKey, blockTagValue := blockTag.GetKey(), blockTag.GetValue()
if blockTagKey == tagName {
if blockTagKey == tags.GitModifiersTagKey {
for _, val := range strings.Split(blockTagValue, "/") {
if utils.InSlice(tagMatchStrings, val) {
gitModifiersCounts[matchValue] += 1
}
}
} else if utils.InSlice(tagMatchStrings, blockTagValue) {
foundTag = true
break
}
} else if utils.InSlice(tagMatchV, blockTagValue) {
foundTag = true
break
}
}
}
Expand All @@ -233,6 +240,15 @@ func (t *TagGroup) CalculateTagValue(block structure.IBlock, tag Tag) (tags.ITag
}
}
}
if len(gitModifiersCounts) == 1 {
for k, _ := range gitModifiersCounts {
retTag.Value = evaluateTemplateVariable(k)
break
}
} else if len(gitModifiersCounts) > 1 {
// TODO use the CODEOWNERS file to resolve the conflict
logger.Info(fmt.Sprintf("Git-modifiers conflict found, fallback to default value %s\n", retTag.Value))
}
return retTag, nil
} else if tag.defaultValue != "" {
return retTag, nil
Expand Down
83 changes: 83 additions & 0 deletions src/common/tagging/external/tag_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,89 @@ func TestExternalTagGroup(t *testing.T) {
}
}
})

t.Run("test tagGroup conflicted git_modifiers", func(t *testing.T) {
confPath, _ := filepath.Abs("../../../../tests/external_tags/external_tag_group.yml")
tagGroup := TagGroup{}
tagGroup.InitTagGroup("", nil, nil)
tagGroup.InitExternalTagGroups(confPath)
block := &MockTestBlock{
Block: structure.Block{
FilePath: "",
IsTaggable: true,
ExitingTags: []tags.ITag{
&tags.Tag{
Key: "git_modifiers",
Value: "tronxd/nofar/rotemavni",
},
&tags.Tag{
Key: "git_repo",
Value: "yor",
},
&tags.Tag{
Key: "git_commit",
Value: "00193660c248483862c06e2ae96111adfcb683af",
},
&tags.Tag{
Key: "yor_trace",
Value: "123",
},
},
},
}
err := tagGroup.CreateTagsForBlock(block)
if err != nil {
logger.Warning(err.Error())
t.Fail()
}
for _, newTag := range block.NewTags {
if newTag.GetKey() == "team" {
assert.Equal(t, "interfaces", newTag.GetValue())
}
}
})

t.Run("test tagGroup non conflicted git_modifiers", func(t *testing.T) {
confPath, _ := filepath.Abs("../../../../tests/external_tags/external_tag_group.yml")
tagGroup := TagGroup{}
tagGroup.InitTagGroup("", nil, nil)
tagGroup.InitExternalTagGroups(confPath)
block := &MockTestBlock{
Block: structure.Block{
FilePath: "",
IsTaggable: true,
ExitingTags: []tags.ITag{
&tags.Tag{
Key: "git_modifiers",
Value: "nofar",
},
&tags.Tag{
Key: "git_repo",
Value: "yor",
},
&tags.Tag{
Key: "git_commit",
Value: "00193660c248483862c06e2ae96111adfcb683af",
},
&tags.Tag{
Key: "yor_trace",
Value: "123",
},
},
},
}
err := tagGroup.CreateTagsForBlock(block)
if err != nil {
logger.Warning(err.Error())
t.Fail()
}
for _, newTag := range block.NewTags {
if newTag.GetKey() == "team" {
assert.Equal(t, "platform", newTag.GetValue())
}
}
})

}

type MockTestBlock struct {
Expand Down
12 changes: 12 additions & 0 deletions src/common/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,15 @@ func GetEnv(key, fallback string) string {
}
return fallback
}

func MaxMapCountKey(m map[string]int) string {
var maxKey string
var maxCount = -1
for key, count := range m {
if count > maxCount {
maxKey = key
maxCount = count
}
}
return maxKey
}

0 comments on commit 31f504a

Please sign in to comment.