Skip to content

Commit

Permalink
Unique targetPaths in PR Body comment.
Browse files Browse the repository at this point in the history
We were returning a slice of targetPaths that matches the promotionPaths,
this was creating duplicates in the pr body, like in this PR:
commercetools/k8s-gitops#1004

This PR return a unique list of targetPaths.
  • Loading branch information
ashvarts committed Oct 28, 2024
1 parent 89c7839 commit 49ccc00
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
18 changes: 13 additions & 5 deletions internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1127,21 +1127,22 @@ func prBody(keys []int, newPrMetadata prMetadata, newPrBody string) string {

for i, k := range keys {
sp = newPrMetadata.PreviousPromotionMetadata[k].SourcePath
x := identifyCommonPaths(newPrMetadata.PromotedPaths, newPrMetadata.PreviousPromotionMetadata[k].TargetPaths)
x := uniqueCommonPaths(newPrMetadata.PromotedPaths, newPrMetadata.PreviousPromotionMetadata[k].TargetPaths)
tp = strings.Join(x, fmt.Sprintf("` \n%s`", strings.Repeat(mkTab, i+1)))
newPrBody = newPrBody + fmt.Sprintf("%s↘️ #%d `%s` ➡️ \n%s`%s` \n", strings.Repeat(mkTab, i), k, sp, strings.Repeat(mkTab, i+1), tp)
}

return newPrBody
}

// identifyCommonPaths takes a slice of promotion paths and target paths and
// uniqueCommonPaths takes a slice of promotion paths and target paths and
// returns a slice containing paths in common.
func identifyCommonPaths(promotionPaths []string, targetPaths []string) []string {
func uniqueCommonPaths(promotionPaths []string, targetPaths []string) []string {
if (len(promotionPaths) == 0) || (len(targetPaths) == 0) {
return nil
}
var commonPaths []string

uniqueCommonPaths := make(map[string]bool)
for _, pp := range promotionPaths {
if pp == "" {
continue
Expand All @@ -1153,11 +1154,18 @@ func identifyCommonPaths(promotionPaths []string, targetPaths []string) []string
// strings.HasPrefix is used to check that the target path and promotion path match instead of
// using 'pp == tp' because the promotion path is targetPath + component.
if strings.HasPrefix(pp, tp) {
commonPaths = append(commonPaths, tp)
if _, ok := uniqueCommonPaths[tp]; !ok {
uniqueCommonPaths[tp] = true
}
}
}
}

var commonPaths []string
for path := range uniqueCommonPaths {
commonPaths = append(commonPaths, path)
}

return commonPaths
}

Expand Down
28 changes: 27 additions & 1 deletion internal/pkg/githubapi/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,32 @@ func TestPrBody(t *testing.T) {
assert.Equal(t, string(expectedPrBody), newPrBody)
}

func TestPrBodyMultiComponent(t *testing.T) {
t.Parallel()
keys := []int{1, 2}
newPrMetadata := prMetadata{
// note: "targetPath3" is missing from the list of promoted paths, so it should not
// be included in the new PR body.
PromotedPaths: []string{"targetPath1/component1", "targetPath1/component2", "targetPath2/component1"},
PreviousPromotionMetadata: map[int]promotionInstanceMetaData{
1: {
SourcePath: "sourcePath1",
TargetPaths: []string{"targetPath1"},
},
2: {
SourcePath: "sourcePath2",
TargetPaths: []string{"targetPath2"},
},
},
}
newPrBody := prBody(keys, newPrMetadata, "")
expectedPrBody, err := os.ReadFile("testdata/pr_body_multi_component.golden.md")
if err != nil {
t.Fatalf("Error loading golden file: %s", err)
}
assert.Equal(t, string(expectedPrBody), newPrBody)
}

func TestGhPrClientDetailsGetBlameURLPrefix(t *testing.T) {
t.Parallel()
tests := []struct {
Expand Down Expand Up @@ -474,7 +500,7 @@ func Test_identifyCommonPaths(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := identifyCommonPaths(tt.args.promoPaths, tt.args.targetPaths)
got := uniqueCommonPaths(tt.args.promoPaths, tt.args.targetPaths)
assert.Equal(t, got, tt.want)
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
↘️ #1 `sourcePath1` ➡️
    `targetPath1`
    ↘️ #2 `sourcePath2` ➡️
        `targetPath2`

0 comments on commit 49ccc00

Please sign in to comment.