Skip to content

Commit

Permalink
Add getPromotionSkipPaths to find the component with fewest skip paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvarts committed Oct 29, 2024
1 parent 3b2b948 commit 75b6101
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 6 deletions.
46 changes: 40 additions & 6 deletions internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,12 +1126,7 @@ func generatePromotionPrBody(ghPrClientDetails GhPrClientDetails, components str

newPrMetadata.PromotedPaths = maps.Keys(promotion.ComputedSyncPaths)

promotionSkipPaths := make(map[string]bool)
for _, paths := range promotion.Metadata.PerComponentSkippedTargetPaths {
for _, p := range paths {
promotionSkipPaths[p] = true
}
}
promotionSkipPaths := getPromotionSkipPaths(promotion)

newPrBody = fmt.Sprintf("Promotion path(%s):\n\n", components)

Expand All @@ -1150,6 +1145,45 @@ func generatePromotionPrBody(ghPrClientDetails GhPrClientDetails, components str
return newPrBody
}

// getPromotionSkipPaths returns a map of paths that are marked as skipped for this promotion
// when we have multiple components, we are going to use the component that has the fewest skip paths
func getPromotionSkipPaths(promotion PromotionInstance) map[string]bool {
perComponentSkippedTargetPaths := promotion.Metadata.PerComponentSkippedTargetPaths
promotionSkipPaths := make(map[string]bool)
if perComponentSkippedTargetPaths == nil {
return promotionSkipPaths
}

if len(perComponentSkippedTargetPaths) == 0 {
return promotionSkipPaths
}

// if we have one or more components then we are just going to
// user the component that has the fewest skipPaths when
// generating the promotion prBody. This way the promotion
// body will error on the side of informing the user
// of more promotion paths, rather than leaving some out.
skipCounts := make(map[string]int)
for component, paths := range perComponentSkippedTargetPaths {
skipCounts[component] = len(paths)
}
// sort ['component','countOfSkipPaths'] by countOfSkipPaths
pathCountPairs := make([][2]interface{}, 0, len(skipCounts))
for k, v := range skipCounts {
pathCountPairs = append(pathCountPairs, [2]interface{}{k, v})
}
sort.Slice(pathCountPairs, func(i, j int) bool {
return pathCountPairs[i][1].(int) < pathCountPairs[j][1].(int)
})

componentWithFewestSkippedPaths := pathCountPairs[0][0].(string)
for _, p := range perComponentSkippedTargetPaths[componentWithFewestSkippedPaths] {
promotionSkipPaths[p] = true
}

return promotionSkipPaths
}

func prBody(keys []int, newPrMetadata prMetadata, newPrBody string, promotionSkipPaths map[string]bool) string {
const mkTab = "&nbsp;&nbsp;&nbsp;&nbsp;"
sp := ""
Expand Down
62 changes: 62 additions & 0 deletions internal/pkg/githubapi/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,65 @@ func TestCommitStatusTargetURL(t *testing.T) {
})
}
}

func Test_getPromotionSkipPaths(t *testing.T) {
type args struct {
promotion PromotionInstance
}
tests := []struct {
name string
args args
want map[string]bool
}{
{
name: "No skip paths",
args: args{
promotion: PromotionInstance{
Metadata: PromotionInstanceMetaData{
PerComponentSkippedTargetPaths: map[string][]string{},
},
},
},
want: map[string]bool{},
},
{
name: "one skip path",
args: args{
promotion: PromotionInstance{
Metadata: PromotionInstanceMetaData{
PerComponentSkippedTargetPaths: map[string][]string{
"component1": {"targetPath1", "targetPath2"},
},
},
},
},
want: map[string]bool{
"targetPath1": true,
"targetPath2": true,
},
},
{
name: "multiple skip path",
args: args{
promotion: PromotionInstance{
Metadata: PromotionInstanceMetaData{
PerComponentSkippedTargetPaths: map[string][]string{
"component1": {"targetPath1", "targetPath2", "targetPath3"},
"component2": {"targetPath3"},
"component3": {"targetPath1", "targetPath2"},
},
},
},
},
want: map[string]bool{
"targetPath3": true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getPromotionSkipPaths(tt.args.promotion)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 75b6101

Please sign in to comment.