Skip to content

Commit

Permalink
chore(internal/actions): refactor changefinder (#7884)
Browse files Browse the repository at this point in the history
Refactors `changefinder` to support nested submodules i.e. `vision/v2`.

Changes the default compare the current `HEAD` against `origin/main`.

Add a `-q` quiet mode, all logs will be buffered and dumped in the event of an error.

Add a `-dir` flag to specify a different root dir than the default of `cwd`.

Add a `-format` flag with values `plain` (stdout, new line delimited), and `github` (prints github action output). Defaults to `plain`.

Add a `-gh-var` flag that accepts the name of the GitHub variable the output should set,  defaults to `submodules`.

This will be able to replace the git-fu used in the `apidiff` workflow and it can power a "change description" action, basically the inverse of the `apidiff` workflow that reports the _added_ surfaces.

Try it out by running the following:
```
cd $GOOGLE_CLOUD_GO
git checkout origin owl-bot-copy
go run ./internal/actions/cmd/changefinder -q
```
  • Loading branch information
noahdietz committed May 5, 2023
1 parent 9fc48a9 commit 482ff7b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 86 deletions.
29 changes: 29 additions & 0 deletions internal/actions/cmd/changefinder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# changefinder

`changefinder` will compare the current branch to the `origin/master` branch
then determine which submodules, excluding `internal` submodules, had changes,
and list them on `stdout`. The default is emit them in a simple newline
delimited list.

The available flags are as follows:
* `-dir=[absolute path]`: The directory to diff, defaults to current working
directory.
* `-q`: Enables quiet mode with no logging. In the event of an error while in
quiet mode, all logs that were surpressed are dumped with the error. Defaults
to `false` (i.e. "verbose").
* `-format=[plain|github]`: The `stdout` output format. Default is `plain`.
* `-gh-var=[variable name]`: The variabe name to set output for in `github`
format mode. Defaults to `submodules`.

Example usages from this repo root:

```sh
# targeting a git repository other than current working directory
go run ./internal/actions/cmd/changefinder -dir /path/to/your/go/repo

# quiet mode, github format
go run ./internal/actions/cmd/changefinder -q -format=github

# quiet mode, github format, github var name "foo"
go run ./internal/actions/cmd/changefinder -q -format=github -gh-var=foo
```
182 changes: 96 additions & 86 deletions internal/actions/cmd/changefinder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,126 +17,113 @@ package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

var (
dir = flag.String("dir", "", "the root directory to evaluate")
format = flag.String("format", "plain", "output format, one of [plain|github], defaults to 'plain'")
ghVarName = flag.String("gh-var", "submodules", "github format's variable name to set output for, defaults to 'submodules'.")
quiet = flag.Bool("q", false, "quiet mode, minimal logging")
// Only used in quiet mode, printed in the event of an error.
logBuffer []string
)

func main() {
flag.Parse()
rootDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
fatalE(err)
}
if len(os.Args) > 1 {
rootDir = os.Args[1]
if *dir != "" {
rootDir = *dir
}
log.Printf("Root dir: %q", rootDir)
var modDirs []string
// Find all external modules
filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.Name() == "internal" {
return filepath.SkipDir
}
if d.Name() == "go.mod" {
modDirs = append(modDirs, filepath.Dir(path))
}
return nil
})
logg("Root dir: %q", rootDir)

// Find relative sub-module
submodules := map[string]bool{}
for _, dir := range modDirs {
name, err := modName(dir)
if err != nil {
log.Fatalf("unable to lookup mod dir")
}
// Skip non-submodule
if name == "cloud.google.com/go" {
continue
}
name = strings.TrimPrefix(name, "cloud.google.com/go/")
submodules[name] = true
}

c := exec.Command("git", "pull", "--tags")
c.Dir = rootDir
if err := c.Run(); err != nil {
log.Fatalf("unable to pull tags: %v", err)
}

tag, err := latestTag(rootDir)
submodules, err := mods(rootDir)
if err != nil {
log.Fatalf("unable to find tag: %v", err)
}
log.Printf("Latest release: %s", tag)

c = exec.Command("git", "reset", "--hard", tag)
c.Dir = rootDir
if err := c.Run(); err != nil {
log.Fatalf("unable to reset to tag: %v", err)
fatalE(err)
}

changes, err := gitFilesChanges(rootDir)
if err != nil {
log.Fatalf("unable to get files changed: %v", err)
fatal("unable to get files changed: %v", err)
}

updatedSubmodulesSet := map[string]bool{}
modulesSeen := map[string]bool{}
updatedSubmodules := []string{}
for _, change := range changes {
//TODO(codyoss): This will not work with nested sub-modules. If we add
// those this needs to be updated.
pkg := strings.Split(change, "/")[0]
log.Printf("update to path: %s", pkg)
if submodules[pkg] {
updatedSubmodulesSet[pkg] = true
if strings.HasPrefix(change, "internal") {
continue
}
submod, ok := owner(change, submodules)
if !ok {
logg("no module for: %s", change)
continue
}
if _, seen := modulesSeen[submod]; !seen {
logg("changes in submodule: %s", submod)
updatedSubmodules = append(updatedSubmodules, submod)
modulesSeen[submod] = true
}
}

updatedSubmodule := []string{}
for mod := range updatedSubmodulesSet {
updatedSubmodule = append(updatedSubmodule, mod)
}
b, err := json.Marshal(updatedSubmodule)
if err != nil {
log.Fatalf("unable to marshal submodules: %v", err)
output(updatedSubmodules)
}

func output(s []string) error {
switch *format {
case "github":
b, err := json.Marshal(s)
if err != nil {
fatal("unable to marshal submodules: %v", err)
}
fmt.Printf("::set-output name=%s::%s", *ghVarName, b)
case "plain":
fallthrough
default:
fmt.Println(strings.Join(s, "\n"))
}
fmt.Printf("::set-output name=submodules::%s", b)
return nil
}

func modName(dir string) (string, error) {
c := exec.Command("go", "list", "-m")
c.Dir = dir
b, err := c.Output()
if err != nil {
return "", err
func owner(file string, submodules []string) (string, bool) {
submod := ""
for _, mod := range submodules {
if strings.HasPrefix(file, mod) && len(mod) > len(submod) {
submod = mod
}
}
b = bytes.TrimSpace(b)
return string(b), nil

return submod, submod != ""
}

func latestTag(dir string) (string, error) {
c := exec.Command("git", "rev-list", "--tags", "--max-count=1")
func mods(dir string) (submodules []string, err error) {
c := exec.Command("go", "list", "-m")
c.Dir = dir
b, err := c.Output()
if err != nil {
return "", err
return submodules, err
}
commit := string(bytes.TrimSpace(b))
c = exec.Command("git", "describe", "--tags", commit)
c.Dir = dir
b, err = c.Output()
if err != nil {
return "", err
list := strings.Split(strings.TrimSpace(string(b)), "\n")

submodules = []string{}
for _, mod := range list {
// Skip non-submodule or internal submodules.
if mod == "cloud.google.com/go" || strings.Contains(mod, "internal") {
continue
}
logg("found module: %s", mod)
mod = strings.TrimPrefix(mod, "cloud.google.com/go/")
submodules = append(submodules, mod)
}
b = bytes.TrimSpace(b)
return string(b), nil

return submodules, nil
}

func gitFilesChanges(dir string) ([]string, error) {
Expand All @@ -147,6 +134,29 @@ func gitFilesChanges(dir string) ([]string, error) {
return nil, err
}
b = bytes.TrimSpace(b)
log.Printf("Files changed:\n%s", b)
logg("Files changed:\n%s", b)
return strings.Split(string(b), "\n"), nil
}

// logg is a potentially quiet log.Printf.
func logg(format string, values ...interface{}) {
if *quiet {
logBuffer = append(logBuffer, fmt.Sprintf(format, values...))
return
}
log.Printf(format, values...)
}

func fatalE(err error) {
if *quiet {
log.Print(strings.Join(logBuffer, "\n"))
}
log.Fatal(err)
}

func fatal(format string, values ...interface{}) {
if *quiet {
log.Print(strings.Join(logBuffer, "\n"))
}
log.Fatalf(format, values...)
}

0 comments on commit 482ff7b

Please sign in to comment.