Skip to content

Commit de654eb

Browse files
authored
feat(internal): add force option for regen (#4310)
The forceAll option indicates that all proto files should be regenerated.
1 parent 8805c8a commit de654eb

6 files changed

Lines changed: 68 additions & 9 deletions

File tree

internal/gapicgen/cmd/genbot/bot.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ import (
2626
"cloud.google.com/go/internal/gapicgen/git"
2727
)
2828

29-
func genBot(ctx context.Context, githubAccessToken, githubUsername, githubName, githubEmail string) error {
29+
type botConfig struct {
30+
githubAccessToken string
31+
githubUsername string
32+
githubName string
33+
githubEmail string
34+
forceAll bool
35+
}
36+
37+
func genBot(ctx context.Context, c botConfig) error {
3038
for k, v := range map[string]string{
31-
"githubAccessToken": githubAccessToken,
32-
"githubUsername": githubUsername,
33-
"githubName": githubName,
34-
"githubEmail": githubEmail,
39+
"githubAccessToken": c.githubAccessToken,
40+
"githubUsername": c.githubUsername,
41+
"githubName": c.githubName,
42+
"githubEmail": c.githubEmail,
3543
} {
3644
if v == "" {
3745
log.Printf("missing or empty value for required flag --%s\n", k)
@@ -40,7 +48,7 @@ func genBot(ctx context.Context, githubAccessToken, githubUsername, githubName,
4048
}
4149

4250
// Setup the client and git environment.
43-
githubClient, err := git.NewGithubClient(ctx, githubUsername, githubName, githubEmail, githubAccessToken)
51+
githubClient, err := git.NewGithubClient(ctx, c.githubUsername, c.githubName, c.githubEmail, c.githubAccessToken)
4452
if err != nil {
4553
return err
4654
}
@@ -73,7 +81,7 @@ func genBot(ctx context.Context, githubAccessToken, githubUsername, githubName,
7381
return nil
7482
}
7583

76-
return generate(ctx, githubClient)
84+
return generate(ctx, githubClient, c.forceAll)
7785
}
7886

7987
// hasCreatedPRToday checks if the created time of a PR is from today.

internal/gapicgen/cmd/genbot/generate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131

3232
// generate downloads sources and generates pull requests for go-genproto and
3333
// google-cloud-go if needed.
34-
func generate(ctx context.Context, githubClient *git.GithubClient) error {
34+
func generate(ctx context.Context, githubClient *git.GithubClient, forceAll bool) error {
3535
log.Println("creating temp dir")
3636
tmpDir, err := ioutil.TempDir("", "update-genproto")
3737
if err != nil {
@@ -71,6 +71,7 @@ func generate(ctx context.Context, githubClient *git.GithubClient) error {
7171
GenprotoDir: genprotoDir,
7272
GapicDir: gocloudDir,
7373
ProtoDir: protoDir,
74+
ForceAll: forceAll,
7475
}
7576
changes, err := generator.Generate(ctx, conf)
7677
if err != nil {

internal/gapicgen/cmd/genbot/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func main() {
4141
githubName := flag.String("githubName", os.Getenv("GITHUB_NAME"), "The name of the author for git commits.")
4242
githubEmail := flag.String("githubEmail", os.Getenv("GITHUB_EMAIL"), "The email address of the author.")
4343
localMode := flag.Bool("local", strToBool(os.Getenv("GENBOT_LOCAL_MODE")), "Enables generating sources locally. This mode will not open any pull requests.")
44+
forceAll := flag.Bool("forceAll", strToBool(os.Getenv("GENBOT_FORCE_ALL")), "Enables regenerating everything regardless of changes in googleapis.")
4445

4546
// flags for local mode
4647
googleapisDir := flag.String("googleapis-dir", os.Getenv("GOOGLEAPIS_DIR"), "Directory where sources of googleapis/googleapis resides. If unset the sources will be cloned to a temporary directory that is not cleaned up.")
@@ -67,7 +68,13 @@ func main() {
6768
}
6869
return
6970
}
70-
if err := genBot(ctx, *githubAccessToken, *githubUsername, *githubName, *githubEmail); err != nil {
71+
if err := genBot(ctx, botConfig{
72+
githubAccessToken: *githubAccessToken,
73+
githubUsername: *githubUsername,
74+
githubName: *githubName,
75+
githubEmail: *githubEmail,
76+
forceAll: *forceAll,
77+
}); err != nil {
7178
log.Fatal(err)
7279
}
7380
}

internal/gapicgen/generator/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ var microgenGapicConfigs = []*microgenConfig{
10211021
gRPCServiceConfigPath: "google/cloud/recommendationengine/v1beta1/recommendationengine_grpc_service_config.json",
10221022
apiServiceConfigPath: "google/cloud/recommendationengine/v1beta1/recommendationengine_v1beta1.yaml",
10231023
releaseLevel: "beta",
1024+
stopGeneration: true,
10241025
},
10251026
{
10261027
inputDirectoryPath: "google/cloud/gkehub/v1beta1",

internal/gapicgen/generator/generator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Config struct {
3737
OnlyGenerateGapic bool
3838
LocalMode bool
3939
RegenOnly bool
40+
ForceAll bool
4041
}
4142

4243
// Generate generates genproto and gapics.

internal/gapicgen/generator/genproto.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io/ioutil"
2222
"log"
23+
"os"
2324
"path/filepath"
2425
"regexp"
2526
"strconv"
@@ -57,6 +58,7 @@ type GenprotoGenerator struct {
5758
genprotoDir string
5859
googleapisDir string
5960
protoSrcDir string
61+
forceAll bool
6062
}
6163

6264
// NewGenprotoGenerator creates a new GenprotoGenerator.
@@ -65,6 +67,7 @@ func NewGenprotoGenerator(c *Config) *GenprotoGenerator {
6567
genprotoDir: c.GenprotoDir,
6668
googleapisDir: c.GoogleapisDir,
6769
protoSrcDir: filepath.Join(c.ProtoDir, "/src"),
70+
forceAll: c.ForceAll,
6871
}
6972
}
7073

@@ -186,6 +189,9 @@ func (g *GenprotoGenerator) protoc(fileNames []string) error {
186189
// getUpdatedPackages parses all of the new commits to find what packages need
187190
// to be regenerated.
188191
func (g *GenprotoGenerator) getUpdatedPackages(googleapisHash string) (map[string][]string, error) {
192+
if g.forceAll {
193+
return g.getAllPackages()
194+
}
189195
files, err := git.UpdateFilesSinceHash(g.googleapisDir, googleapisHash)
190196
if err != nil {
191197
return nil, err
@@ -205,6 +211,41 @@ func (g *GenprotoGenerator) getUpdatedPackages(googleapisHash string) (map[strin
205211
return pkgFiles, nil
206212
}
207213

214+
func (g *GenprotoGenerator) getAllPackages() (map[string][]string, error) {
215+
seenFiles := make(map[string]bool)
216+
pkgFiles := make(map[string][]string)
217+
for _, root := range []string{g.googleapisDir} {
218+
walkFn := func(path string, info os.FileInfo, err error) error {
219+
if err != nil {
220+
return err
221+
}
222+
if !info.Mode().IsRegular() || !strings.HasSuffix(path, ".proto") {
223+
return nil
224+
}
225+
226+
switch rel, err := filepath.Rel(root, path); {
227+
case err != nil:
228+
return err
229+
case seenFiles[rel]:
230+
return nil
231+
default:
232+
seenFiles[rel] = true
233+
}
234+
235+
pkg, err := goPkg(path)
236+
if err != nil {
237+
return err
238+
}
239+
pkgFiles[pkg] = append(pkgFiles[pkg], path)
240+
return nil
241+
}
242+
if err := filepath.Walk(root, walkFn); err != nil {
243+
return nil, err
244+
}
245+
}
246+
return pkgFiles, nil
247+
}
248+
208249
// moveAndCleanupGeneratedSrc moves all generated src to their correct locations
209250
// in the repository, because protoc puts it in a folder called `generated/``.
210251
func (g *GenprotoGenerator) moveAndCleanupGeneratedSrc() error {

0 commit comments

Comments
 (0)