Skip to content

Commit

Permalink
Add skipPaths filter for generating prBody.
Browse files Browse the repository at this point in the history
Added a filterSkipPaths function when generating a pr comment.
This will take the targetPaths and filter out any skipped paths from
the final comment.
  • Loading branch information
ashvarts committed Oct 28, 2024
1 parent 49ccc00 commit 50e4e8d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
56 changes: 26 additions & 30 deletions internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,14 +1104,22 @@ func generatePromotionPrBody(ghPrClientDetails GhPrClientDetails, components str

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

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

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

keys := make([]int, 0)
for k := range newPrMetadata.PreviousPromotionMetadata {
keys = append(keys, k)
}
sort.Ints(keys)
newPrBody = prBody(keys, newPrMetadata, newPrBody)

newPrBody = prBody(keys, newPrMetadata, newPrBody, promotionSkipPaths)

prMetadataString, _ := newPrMetadata.serialize()

Expand All @@ -1120,53 +1128,41 @@ func generatePromotionPrBody(ghPrClientDetails GhPrClientDetails, components str
return newPrBody
}

func prBody(keys []int, newPrMetadata prMetadata, newPrBody string) string {
func prBody(keys []int, newPrMetadata prMetadata, newPrBody string, promotionSkipPaths map[string]bool) string {
const mkTab = "    "
sp := ""
tp := ""

for i, k := range keys {
sp = newPrMetadata.PreviousPromotionMetadata[k].SourcePath
x := uniqueCommonPaths(newPrMetadata.PromotedPaths, newPrMetadata.PreviousPromotionMetadata[k].TargetPaths)
x := filterSkipPaths(newPrMetadata.PreviousPromotionMetadata[k].TargetPaths, promotionSkipPaths)
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
}

// uniqueCommonPaths takes a slice of promotion paths and target paths and
// returns a slice containing paths in common.
func uniqueCommonPaths(promotionPaths []string, targetPaths []string) []string {
if (len(promotionPaths) == 0) || (len(targetPaths) == 0) {
return nil
}

uniqueCommonPaths := make(map[string]bool)
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) {
if _, ok := uniqueCommonPaths[tp]; !ok {
uniqueCommonPaths[tp] = true
}
}
// filterSkipPaths filters out the paths that are marked as skipped
func filterSkipPaths(targetPaths []string, promotionSkipPaths map[string]bool) []string {
var pathSkip = make(map[string]bool)
for _, targetPath := range targetPaths {
if _, ok := promotionSkipPaths[targetPath]; ok {
pathSkip[targetPath] = true
} else {
pathSkip[targetPath] = false
}
}

var commonPaths []string
for path := range uniqueCommonPaths {
commonPaths = append(commonPaths, path)
var paths []string

for path, skip := range pathSkip {
if !skip {
paths = append(paths, path)
}
}

return commonPaths
return paths
}

func createPrObject(ghPrClientDetails GhPrClientDetails, newBranchRef string, newPrTitle string, newPrBody string, defaultBranch string, assignee string) (*github.PullRequest, error) {
Expand Down
6 changes: 4 additions & 2 deletions internal/pkg/githubapi/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func readJSONFromFile(t *testing.T, filename string, data interface{}) {
func TestPrBody(t *testing.T) {
t.Parallel()
keys := []int{1, 2, 3}
promotionSkipPaths := map[string]bool{"targetPath3": true}
newPrMetadata := prMetadata{
// note: "targetPath3" is missing from the list of promoted paths, so it should not
// be included in the new PR body.
Expand All @@ -254,7 +255,7 @@ func TestPrBody(t *testing.T) {
},
},
}
newPrBody := prBody(keys, newPrMetadata, "")
newPrBody := prBody(keys, newPrMetadata, "", promotionSkipPaths)
expectedPrBody, err := os.ReadFile("testdata/pr_body.golden.md")
if err != nil {
t.Fatalf("Error loading golden file: %s", err)
Expand All @@ -265,6 +266,7 @@ func TestPrBody(t *testing.T) {
func TestPrBodyMultiComponent(t *testing.T) {
t.Parallel()
keys := []int{1, 2}
promotionSkipPaths := map[string]bool{}
newPrMetadata := prMetadata{
// note: "targetPath3" is missing from the list of promoted paths, so it should not
// be included in the new PR body.
Expand All @@ -280,7 +282,7 @@ func TestPrBodyMultiComponent(t *testing.T) {
},
},
}
newPrBody := prBody(keys, newPrMetadata, "")
newPrBody := prBody(keys, newPrMetadata, "", promotionSkipPaths)
expectedPrBody, err := os.ReadFile("testdata/pr_body_multi_component.golden.md")
if err != nil {
t.Fatalf("Error loading golden file: %s", err)
Expand Down

0 comments on commit 50e4e8d

Please sign in to comment.