Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique targetPaths in PR Body comment. #36

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
Loading