diff --git a/internal/gapicgen/generator/generator.go b/internal/gapicgen/generator/generator.go index 7b13ff9a67cb..c3e129b84b17 100644 --- a/internal/gapicgen/generator/generator.go +++ b/internal/gapicgen/generator/generator.go @@ -23,6 +23,8 @@ import ( "fmt" "os" "path/filepath" + "sort" + "strings" "cloud.google.com/go/internal/gapicgen/git" ) @@ -69,7 +71,43 @@ func gatherChanges(googleapisDir, genprotoDir string) ([]*git.ChangeInfo, error) if err != nil { return nil, err } - changes, err := git.ParseChangeInfo(googleapisDir, commits) + affectedProtos := make(map[string][]string) + var relevantCommits []string + for _, commit := range commits { + files, err := git.FilesChanged(googleapisDir, commit) + if err != nil { + return nil, err + } + protos := make(map[string]struct{}) + for _, file := range files { + if !strings.HasSuffix(file, ".proto") { + continue + } + pkg, err := goPkg(filepath.Join(googleapisDir, file)) + if err != nil { + return nil, err + } + var onWatchlist bool + for _, watchedPkg := range generateList { + if pkg == watchedPkg { + onWatchlist = true + break + } + } + if onWatchlist { + if _, ok := affectedProtos[commit]; !ok { + relevantCommits = append(relevantCommits, commit) + } + protos[file] = struct{}{} + } + } + for proto := range protos { + affectedProtos[commit] = append(affectedProtos[commit], proto) + } + sort.Strings(affectedProtos[commit]) + } + + changes, err := git.ParseChangeInfo(googleapisDir, relevantCommits, affectedProtos) if err != nil { return nil, err } diff --git a/internal/gapicgen/git/git.go b/internal/gapicgen/git/git.go index 114a233e6e30..56d7e020deff 100644 --- a/internal/gapicgen/git/git.go +++ b/internal/gapicgen/git/git.go @@ -33,6 +33,7 @@ type ChangeInfo struct { Body string Title string GoogleapisHash string + AffectedProtos []string } // FormatChanges turns a slice of changes into formatted string that will match @@ -73,11 +74,18 @@ func truncateAndFormatChanges(changes []*ChangeInfo, onlyGapicChanges, truncate if truncate { startBody := strings.Index(body, "PiperOrigin-RevId") if startBody != -1 { - body = fmt.Sprintf(" %s", body[startBody:]) + body = " " + body[startBody:] } } - sb.WriteString(fmt.Sprintf("%s\n\n", body)) + sb.WriteString(fmt.Sprintf("%s\n", body)) + if len(c.AffectedProtos) > 0 { + sb.WriteString(" Affected protos:\n") + for _, proto := range c.AffectedProtos { + sb.WriteString(fmt.Sprintf(" - %s\n", proto)) + } + } + sb.WriteString("\n") } // If the buffer is empty except for the "Changes:" text return an empty // string. @@ -88,7 +96,7 @@ func truncateAndFormatChanges(changes []*ChangeInfo, onlyGapicChanges, truncate } // ParseChangeInfo gets the ChangeInfo for a given googleapis hash. -func ParseChangeInfo(googleapisDir string, hashes []string) ([]*ChangeInfo, error) { +func ParseChangeInfo(googleapisDir string, hashes []string, affectedProtos map[string][]string) ([]*ChangeInfo, error) { var changes []*ChangeInfo for _, hash := range hashes { // Get commit title and body @@ -112,6 +120,7 @@ func ParseChangeInfo(googleapisDir string, hashes []string) ([]*ChangeInfo, erro Title: title, Body: body, GoogleapisHash: hash, + AffectedProtos: affectedProtos[hash], }) } return changes, nil @@ -182,9 +191,9 @@ func DeepClone(repo, dir string) error { return err } -// filesChanged returns a list of files changed in a commit for the provdied +// FilesChanged returns a list of files changed in a commit for the provided // hash in the given gitDir. -func filesChanged(gitDir, hash string) ([]string, error) { +func FilesChanged(gitDir, hash string) ([]string, error) { c := execv.Command("git", "show", "--pretty=format:", "--name-only", hash) c.Dir = gitDir b, err := c.Output() diff --git a/internal/gapicgen/git/git_test.go b/internal/gapicgen/git/git_test.go index 35f4cf214893..f69248aa38b4 100644 --- a/internal/gapicgen/git/git_test.go +++ b/internal/gapicgen/git/git_test.go @@ -68,6 +68,12 @@ func TestFormatChanges(t *testing.T) { onlyGapics: true, want: "", }, + { + name: "affected protos", + changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar", AffectedProtos: []string{"foo.proto", "bar.proto"}}}, + onlyGapics: false, + want: "\nChanges:\n\nfix: foo\n bar\n Affected protos:\n - foo.proto\n - bar.proto\n\n", + }, } for _, tc := range tests {