diff --git a/internal/pkg/githubapi/github.go b/internal/pkg/githubapi/github.go index 745c6fb6..9ad6e8b7 100644 --- a/internal/pkg/githubapi/github.go +++ b/internal/pkg/githubapi/github.go @@ -1104,6 +1104,13 @@ 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) @@ -1111,7 +1118,8 @@ func generatePromotionPrBody(ghPrClientDetails GhPrClientDetails, components str keys = append(keys, k) } sort.Ints(keys) - newPrBody = prBody(keys, newPrMetadata, newPrBody) + + newPrBody = prBody(keys, newPrMetadata, newPrBody, promotionSkipPaths) prMetadataString, _ := newPrMetadata.serialize() @@ -1120,14 +1128,14 @@ 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) } @@ -1135,38 +1143,26 @@ func prBody(keys []int, newPrMetadata prMetadata, newPrBody string) string { 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) { diff --git a/internal/pkg/githubapi/github_test.go b/internal/pkg/githubapi/github_test.go index c9dd026a..68c9a041 100644 --- a/internal/pkg/githubapi/github_test.go +++ b/internal/pkg/githubapi/github_test.go @@ -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. @@ -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) @@ -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. @@ -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)