Skip to content

Commit 01473f6

Browse files
authored
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
1 parent 80b9422 commit 01473f6

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

internal/gapicgen/cmd/genbot/update.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func updateGocloudPR(ctx context.Context, githubClient *git.GithubClient, pr *gi
3030
}
3131

3232
// Checkout PR and update go.mod
33-
if err := githubClient.UpdateGocloudGoMod(pr); err != nil {
33+
if err := githubClient.UpdateGocloudGoMod(); err != nil {
3434
return err
3535
}
3636

internal/gapicgen/execv/command.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,26 @@ func Command(name string, arg ...string) *CmdWrapper {
4141

4242
// Run a command.
4343
func (c *CmdWrapper) Run() error {
44-
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
45-
return c.Cmd.Run()
44+
log.Printf("[%s] >>>> %v <<<<", c.Dir, strings.Join(c.Args, " "))
45+
err := c.Cmd.Run()
46+
if err != nil {
47+
if ee, ok := err.(*exec.ExitError); ok {
48+
log.Println(string(ee.Stderr))
49+
}
50+
}
51+
return err
52+
}
53+
54+
// Output a command.
55+
func (c *CmdWrapper) Output() ([]byte, error) {
56+
log.Printf("[%s] >>>> %v <<<<", c.Dir, strings.Join(c.Args, " "))
57+
b, err := c.Cmd.Output()
58+
if err != nil {
59+
if ee, ok := err.(*exec.ExitError); ok {
60+
log.Println(string(ee.Stderr))
61+
}
62+
}
63+
return b, err
4664
}
4765

4866
// ForEachMod runs the given function with the directory of

internal/gapicgen/git/github.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package git
1616

1717
import (
18-
"bytes"
1918
"context"
2019
"errors"
2120
"fmt"
@@ -336,7 +335,7 @@ func (gc *GithubClient) MarkPRReadyForReview(ctx context.Context, repo string, n
336335

337336
// UpdateGocloudGoMod updates the go.mod to include latest version of genproto
338337
// for the given gocloud ref.
339-
func (gc *GithubClient) UpdateGocloudGoMod(pr *PullRequest) error {
338+
func (gc *GithubClient) UpdateGocloudGoMod() error {
340339
tmpDir, err := ioutil.TempDir("", "finalize-github-pr")
341340
if err != nil {
342341
return err
@@ -374,26 +373,25 @@ git checkout $BRANCH_NAME
374373

375374
func updateDeps(tmpDir string) error {
376375
// Find directories that had code changes.
377-
out := bytes.NewBuffer(nil)
378376
c := execv.Command("git", "diff", "--name-only", "HEAD", "HEAD~1")
379-
c.Stdout = out
380377
c.Dir = tmpDir
381-
if err := c.Run(); err != nil {
378+
out, err := c.Output()
379+
if err != nil {
382380
return err
383381
}
384-
files := strings.Split(out.String(), "\n")
382+
files := strings.Split(string(out), "\n")
385383
dirs := map[string]bool{}
386384
for _, file := range files {
385+
if strings.HasPrefix(file, "internal") {
386+
continue
387+
}
387388
dir := filepath.Dir(file)
388389
dirs[filepath.Join(tmpDir, dir)] = true
389390
}
390391

391392
// Find which modules had code changes.
392393
updatedModDirs := map[string]bool{}
393-
errWriter := bytes.NewBuffer(nil)
394394
for dir := range dirs {
395-
out.Reset()
396-
errWriter.Reset()
397395
modDir, err := gocmd.ListModDirName(dir)
398396
if err != nil {
399397
if errors.Is(err, gocmd.ErrBuildConstraint) {

0 commit comments

Comments
 (0)