Skip to content

Commit 33715e7

Browse files
authored
Improve promotion comment. (#31)
* 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()
1 parent a43d3d1 commit 33715e7

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

internal/pkg/githubapi/github.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -1109,14 +1109,40 @@ func prBody(keys []int, newPrMetadata prMetadata, newPrBody string) string {
11091109

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

11171117
return newPrBody
11181118
}
11191119

1120+
// identifyCommonPaths takes a slice of promotion paths and target paths and
1121+
// returns a slice containing paths in common.
1122+
func identifyCommonPaths(promotionPaths []string, targetPaths []string) []string {
1123+
if (len(promotionPaths) == 0) || (len(targetPaths) == 0) {
1124+
return nil
1125+
}
1126+
var commonPaths []string
1127+
for _, pp := range promotionPaths {
1128+
if pp == "" {
1129+
continue
1130+
}
1131+
for _, tp := range targetPaths {
1132+
if tp == "" {
1133+
continue
1134+
}
1135+
// strings.HasPrefix is used to check that the target path and promotion path match instead of
1136+
// using 'pp == tp' because the promotion path is targetPath + component.
1137+
if strings.HasPrefix(pp, tp) {
1138+
commonPaths = append(commonPaths, tp)
1139+
}
1140+
}
1141+
}
1142+
1143+
return commonPaths
1144+
}
1145+
11201146
func createPrObject(ghPrClientDetails GhPrClientDetails, newBranchRef string, newPrTitle string, newPrBody string, defaultBranch string, assignee string) (*github.PullRequest, error) {
11211147
newPrConfig := &github.NewPullRequest{
11221148
Body: github.String(newPrBody),

internal/pkg/githubapi/github_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ func TestPrBody(t *testing.T) {
235235
t.Parallel()
236236
keys := []int{1, 2, 3}
237237
newPrMetadata := prMetadata{
238+
// note: "targetPath3" is missing from the list of promoted paths, so it should not
239+
// be included in the new PR body.
240+
PromotedPaths: []string{"targetPath1", "targetPath2", "targetPath4", "targetPath5", "targetPath6"},
238241
PreviousPromotionMetadata: map[int]promotionInstanceMetaData{
239242
1: {
240243
SourcePath: "sourcePath1",
@@ -338,3 +341,72 @@ func TestCommitStatusTargetURL(t *testing.T) {
338341
})
339342
}
340343
}
344+
345+
func Test_identifyCommonPaths(t *testing.T) {
346+
t.Parallel()
347+
type args struct {
348+
promoPaths []string
349+
targetPaths []string
350+
}
351+
tests := []struct {
352+
name string
353+
args args
354+
want []string
355+
}{
356+
{
357+
name: "same paths",
358+
args: args{
359+
promoPaths: []string{"path1/component/path", "path2/component/path", "path3/component/path"},
360+
targetPaths: []string{"path1", "path2", "path3"},
361+
},
362+
want: []string{"path1", "path2", "path3"},
363+
},
364+
{
365+
name: "paths1 is empty",
366+
args: args{
367+
promoPaths: []string{},
368+
targetPaths: []string{"path1", "path2", "path3"},
369+
},
370+
want: nil,
371+
},
372+
{
373+
name: "paths2 is empty",
374+
args: args{
375+
promoPaths: []string{"path1/component/some", "path2/some/other", "path3"},
376+
targetPaths: []string{},
377+
},
378+
want: nil,
379+
},
380+
{
381+
name: "paths2 missing elements",
382+
args: args{
383+
promoPaths: []string{"path1", "path2", "path3"},
384+
targetPaths: []string{""},
385+
},
386+
want: nil,
387+
},
388+
{
389+
name: "path1 missing elements",
390+
args: args{
391+
promoPaths: []string{""},
392+
targetPaths: []string{"path1", "path2"},
393+
},
394+
want: nil,
395+
},
396+
{
397+
name: "path1 and path2 common elements",
398+
args: args{
399+
promoPaths: []string{"path1/component/path", "path3/component/also"},
400+
targetPaths: []string{"path1", "path2", "path3"},
401+
},
402+
want: []string{"path1", "path3"},
403+
},
404+
}
405+
for _, tt := range tests {
406+
t.Run(tt.name, func(t *testing.T) {
407+
t.Parallel()
408+
got := identifyCommonPaths(tt.args.promoPaths, tt.args.targetPaths)
409+
assert.Equal(t, got, tt.want)
410+
})
411+
}
412+
}

internal/pkg/githubapi/testdata/pr_body.golden.md

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
    `targetPath1`
33
    `targetPath2`
44
    ↘️ #2 `sourcePath2` ➡️
5-
        `targetPath3`
65
        `targetPath4`
76
        ↘️ #3 `sourcePath3` ➡️
87
            `targetPath5`

0 commit comments

Comments
 (0)