Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(internal/actions): add touch flag to changefinder #9325

Merged
merged 3 commits into from Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/actions/cmd/changefinder/README.md
Expand Up @@ -20,6 +20,8 @@ The available flags are as follows:
* `-content-pattern=[regex]`: A regex to match on diff contents.
* `-commit-message=[commit message]`: Message to use in the nested commit block
* `-commit-scope=[conventional commit scope]`: Scope to use for the commit e.g. `fix`
* `-touch`: touches the `CHANGES.md` file to elicit a submodule change - only
works when used with `-format=commit`

Example usages from this repo root:

Expand All @@ -32,4 +34,11 @@ 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

# bumping all modules that changed e.g., via generator version update
go run ./internal/actions/cmd/changefinder -q \
-format=commit \
-commit-scope=fix \
-commit-message="describe the change" \
-touch
```
30 changes: 30 additions & 0 deletions internal/actions/cmd/changefinder/main.go
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"os/exec"
"path"
"strings"

"cloud.google.com/go/internal/actions/logg"
Expand All @@ -37,6 +38,7 @@ var (
contentPattern = flag.String("content-regex", "", "regular expression to execute against contents of diff")
commitMessage = flag.String("commit-message", "", "message to use with the module in nested commit format")
commitScope = flag.String("commit-scope", "", "scope to use in commit message - only for format=commit")
touch = flag.Bool("touch", false, "touches the CHANGES.md file to elicit a submodule change - only works when used with -format=commit")
)

func main() {
Expand All @@ -45,6 +47,9 @@ func main() {
if *format == "commit" && (*commitMessage == "" || *commitScope == "") {
logg.Fatalf("requested format=commit and missing commit-message or commit-scope")
}
if *touch && *format != "commit" {
logg.Fatalf("requested modules be touched without using format=commit")
}
rootDir, err := os.Getwd()
if err != nil {
logg.Fatal(err)
Expand Down Expand Up @@ -79,6 +84,11 @@ func main() {
logg.Printf("changes in submodule: %s", submodDir)
updatedSubmoduleDirs = append(updatedSubmoduleDirs, submodDir)
modulesSeen[submodDir] = true
if *touch {
if err := touchModule(rootDir, submodDir); err != nil {
logg.Printf("error touching module %q: %v", submodDir, err)
}
}
}
}

Expand Down Expand Up @@ -168,3 +178,23 @@ func gitFilesChanges(dir string) ([]string, error) {
logg.Printf("Files changed:\n%s", b)
return strings.Split(string(b), "\n"), nil
}

func touchModule(root, mod string) error {
c := exec.Command("echo")
logg.Printf(c.String())

f, err := os.OpenFile(path.Join(root, mod, "CHANGES.md"), os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
c.Stdout = f

err = c.Run()
if err != nil {
return err
}

logg.Printf("Module touched: %s", mod)
return nil
}