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

Improve promotion comment. #31

Merged
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
28 changes: 27 additions & 1 deletion internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,14 +1109,40 @@ func prBody(keys []int, newPrMetadata prMetadata, newPrBody string) string {

for i, k := range keys {
sp = newPrMetadata.PreviousPromotionMetadata[k].SourcePath
x := newPrMetadata.PreviousPromotionMetadata[k].TargetPaths
x := identifyCommonPaths(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
// returns a slice containing paths in common.
func identifyCommonPaths(promotionPaths []string, targetPaths []string) []string {
if (len(promotionPaths) == 0) || (len(targetPaths) == 0) {
return nil
}
var commonPaths []string
for _, pp := range promotionPaths {
if pp == "" {
continue
}
for _, tp := range targetPaths {
if tp == "" {
ashvarts marked this conversation as resolved.
Show resolved Hide resolved
continue
}
// 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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this always true? I'm missing some context about how the paths are built/where they come from so it isn't fully clear to me that this covers all cases.

If this does the job and solves the current issue I'm happy to get it in for now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if strings.HasPrefix(pp, tp) {
commonPaths = append(commonPaths, tp)
}
}
}

return commonPaths
}

func createPrObject(ghPrClientDetails GhPrClientDetails, newBranchRef string, newPrTitle string, newPrBody string, defaultBranch string, assignee string) (*github.PullRequest, error) {
newPrConfig := &github.NewPullRequest{
Body: github.String(newPrBody),
Expand Down
72 changes: 72 additions & 0 deletions internal/pkg/githubapi/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ func TestPrBody(t *testing.T) {
t.Parallel()
keys := []int{1, 2, 3}
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", "targetPath2", "targetPath4", "targetPath5", "targetPath6"},
PreviousPromotionMetadata: map[int]promotionInstanceMetaData{
1: {
SourcePath: "sourcePath1",
Expand Down Expand Up @@ -338,3 +341,72 @@ func TestCommitStatusTargetURL(t *testing.T) {
})
}
}

func Test_identifyCommonPaths(t *testing.T) {
t.Parallel()
type args struct {
promoPaths []string
targetPaths []string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "same paths",
args: args{
promoPaths: []string{"path1/component/path", "path2/component/path", "path3/component/path"},
targetPaths: []string{"path1", "path2", "path3"},
},
want: []string{"path1", "path2", "path3"},
},
{
name: "paths1 is empty",
args: args{
promoPaths: []string{},
targetPaths: []string{"path1", "path2", "path3"},
},
want: nil,
},
{
name: "paths2 is empty",
args: args{
promoPaths: []string{"path1/component/some", "path2/some/other", "path3"},
targetPaths: []string{},
},
want: nil,
},
{
name: "paths2 missing elements",
args: args{
promoPaths: []string{"path1", "path2", "path3"},
targetPaths: []string{""},
},
want: nil,
},
{
name: "path1 missing elements",
args: args{
promoPaths: []string{""},
targetPaths: []string{"path1", "path2"},
},
want: nil,
},
{
name: "path1 and path2 common elements",
args: args{
promoPaths: []string{"path1/component/path", "path3/component/also"},
targetPaths: []string{"path1", "path2", "path3"},
},
want: []string{"path1", "path3"},
},
ashvarts marked this conversation as resolved.
Show resolved Hide resolved
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := identifyCommonPaths(tt.args.promoPaths, tt.args.targetPaths)
assert.Equal(t, got, tt.want)
})
}
}
1 change: 0 additions & 1 deletion internal/pkg/githubapi/testdata/pr_body.golden.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
    `targetPath1`
    `targetPath2`
    ↘️ #2 `sourcePath2` ➡️
        `targetPath3`
        `targetPath4`
        ↘️ #3 `sourcePath3` ➡️
            `targetPath5`
Expand Down
Loading