From 01473f6d8db26c6e18969ace7f9e87c66e94ad9e Mon Sep 17 00:00:00 2001 From: Cody Oss <6331106+codyoss@users.noreply.github.com> Date: Wed, 12 May 2021 12:42:14 -0600 Subject: [PATCH] fix(internal/gapicgen): filter out internal directory changes (#4085) - Filter out internal dir changes since they do not need a module update. - Remove unneeded param from method - Log extra details when a excuting a command yields an error --- internal/gapicgen/cmd/genbot/update.go | 2 +- internal/gapicgen/execv/command.go | 22 ++++++++++++++++++++-- internal/gapicgen/git/github.go | 16 +++++++--------- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/internal/gapicgen/cmd/genbot/update.go b/internal/gapicgen/cmd/genbot/update.go index d2eaeac0179..1487c1cae15 100644 --- a/internal/gapicgen/cmd/genbot/update.go +++ b/internal/gapicgen/cmd/genbot/update.go @@ -30,7 +30,7 @@ func updateGocloudPR(ctx context.Context, githubClient *git.GithubClient, pr *gi } // Checkout PR and update go.mod - if err := githubClient.UpdateGocloudGoMod(pr); err != nil { + if err := githubClient.UpdateGocloudGoMod(); err != nil { return err } diff --git a/internal/gapicgen/execv/command.go b/internal/gapicgen/execv/command.go index 9794bc8a34c..7a81fa8bf7e 100644 --- a/internal/gapicgen/execv/command.go +++ b/internal/gapicgen/execv/command.go @@ -41,8 +41,26 @@ func Command(name string, arg ...string) *CmdWrapper { // Run a command. func (c *CmdWrapper) Run() error { - log.Printf("[%s] >>>> %v <<<<", c.Dir, strings.Join(c.Args, " ")) // NOTE: we have some multi-line commands, make it clear where the command starts and ends - return c.Cmd.Run() + log.Printf("[%s] >>>> %v <<<<", c.Dir, strings.Join(c.Args, " ")) + err := c.Cmd.Run() + if err != nil { + if ee, ok := err.(*exec.ExitError); ok { + log.Println(string(ee.Stderr)) + } + } + return err +} + +// Output a command. +func (c *CmdWrapper) Output() ([]byte, error) { + log.Printf("[%s] >>>> %v <<<<", c.Dir, strings.Join(c.Args, " ")) + b, err := c.Cmd.Output() + if err != nil { + if ee, ok := err.(*exec.ExitError); ok { + log.Println(string(ee.Stderr)) + } + } + return b, err } // ForEachMod runs the given function with the directory of diff --git a/internal/gapicgen/git/github.go b/internal/gapicgen/git/github.go index fe89097a4a2..a4b5bc15d3d 100644 --- a/internal/gapicgen/git/github.go +++ b/internal/gapicgen/git/github.go @@ -15,7 +15,6 @@ package git import ( - "bytes" "context" "errors" "fmt" @@ -336,7 +335,7 @@ func (gc *GithubClient) MarkPRReadyForReview(ctx context.Context, repo string, n // UpdateGocloudGoMod updates the go.mod to include latest version of genproto // for the given gocloud ref. -func (gc *GithubClient) UpdateGocloudGoMod(pr *PullRequest) error { +func (gc *GithubClient) UpdateGocloudGoMod() error { tmpDir, err := ioutil.TempDir("", "finalize-github-pr") if err != nil { return err @@ -374,26 +373,25 @@ git checkout $BRANCH_NAME func updateDeps(tmpDir string) error { // Find directories that had code changes. - out := bytes.NewBuffer(nil) c := execv.Command("git", "diff", "--name-only", "HEAD", "HEAD~1") - c.Stdout = out c.Dir = tmpDir - if err := c.Run(); err != nil { + out, err := c.Output() + if err != nil { return err } - files := strings.Split(out.String(), "\n") + files := strings.Split(string(out), "\n") dirs := map[string]bool{} for _, file := range files { + if strings.HasPrefix(file, "internal") { + continue + } dir := filepath.Dir(file) dirs[filepath.Join(tmpDir, dir)] = true } // Find which modules had code changes. updatedModDirs := map[string]bool{} - errWriter := bytes.NewBuffer(nil) for dir := range dirs { - out.Reset() - errWriter.Reset() modDir, err := gocmd.ListModDirName(dir) if err != nil { if errors.Is(err, gocmd.ErrBuildConstraint) {