Skip to content

Commit

Permalink
Improve promotion comment. (#31)
Browse files Browse the repository at this point in the history
* Improve promotion comment.

Promotion comment used to list all the values in the 'targetPaths' key,
ignoring the promotionAllow and promotionBlock lists. This PR limits
the comment to only paths that are being promoted.

* Use testify library for assert

* return nil, use assert

* Add t.Parallel()
  • Loading branch information
ashvarts authored Oct 23, 2024
1 parent a43d3d1 commit 33715e7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
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 == "" {
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.
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"},
},
}
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

0 comments on commit 33715e7

Please sign in to comment.