Skip to content

Commit

Permalink
fix(internal/gapicgen): filter out internal directory changes (#4085)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
codyoss committed May 12, 2021
1 parent 80b9422 commit 01473f6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/gapicgen/cmd/genbot/update.go
Expand Up @@ -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
}

Expand Down
22 changes: 20 additions & 2 deletions internal/gapicgen/execv/command.go
Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions internal/gapicgen/git/github.go
Expand Up @@ -15,7 +15,6 @@
package git

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 01473f6

Please sign in to comment.