-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor genlocal into genbot (#3674)
Previously if you wanted to run genlocal you needed to have a bunch of dependencies installed. Some of which can be a little tricky to install correctly. These dependencies happen to be the same as genbot. For this reason this commit refactors genlocal into genbot so that the genbot in local mode can be run from a docker container that has the exact same deps installed as our nightly regen.
- Loading branch information
Showing
10 changed files
with
312 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build !windows | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"time" | ||
) | ||
|
||
func genBot(ctx context.Context, githubAccessToken, githubUsername, githubName, githubEmail string) error { | ||
for k, v := range map[string]string{ | ||
"githubAccessToken": githubAccessToken, | ||
"githubUsername": githubUsername, | ||
"githubName": githubName, | ||
"githubEmail": githubEmail, | ||
} { | ||
if v == "" { | ||
log.Printf("missing or empty value for required flag --%s\n", k) | ||
flag.PrintDefaults() | ||
} | ||
} | ||
|
||
// Setup the client and git environment. | ||
githubClient, err := NewGithubClient(ctx, githubUsername, githubName, githubEmail, githubAccessToken) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Check current regen status. | ||
if pr, err := githubClient.GetRegenPR(ctx, "go-genproto", "open"); err != nil { | ||
return err | ||
} else if pr != nil { | ||
return fmt.Errorf("There is already a re-generation in progress") | ||
} | ||
if pr, err := githubClient.GetRegenPR(ctx, "google-cloud-go", "open"); err != nil { | ||
return err | ||
} else if pr != nil { | ||
if err := updateGocloudPR(ctx, githubClient, pr); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
log.Println("checking if a pull request was already opened and merged today") | ||
if pr, err := githubClient.GetRegenPR(ctx, "go-genproto", "closed"); err != nil { | ||
return err | ||
} else if pr != nil && hasCreatedPRToday(pr.Created) { | ||
log.Println("skipping generation, already created and merged a go-genproto PR today") | ||
return nil | ||
} | ||
if pr, err := githubClient.GetRegenPR(ctx, "google-cloud-go", "closed"); err != nil { | ||
return err | ||
} else if pr != nil && hasCreatedPRToday(pr.Created) { | ||
log.Println("skipping generation, already created and merged a google-cloud-go PR today") | ||
return nil | ||
} | ||
|
||
return generate(ctx, githubClient) | ||
} | ||
|
||
// hasCreatedPRToday checks if the created time of a PR is from today. | ||
func hasCreatedPRToday(created time.Time) bool { | ||
now := time.Now() | ||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) | ||
log.Printf("Times -- Now: %v\tToday: %v\tPR Created: %v", now, today, created) | ||
return created.After(today) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build !windows | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"cloud.google.com/go/internal/gapicgen/generator" | ||
"golang.org/x/sync/errgroup" | ||
"gopkg.in/src-d/go-git.v4" | ||
) | ||
|
||
type localConfig struct { | ||
googleapisDir string | ||
gocloudDir string | ||
genprotoDir string | ||
protoDir string | ||
gapicToGenerate string | ||
onlyGapics bool | ||
} | ||
|
||
func genLocal(ctx context.Context, c localConfig) error { | ||
log.Println("creating temp dir") | ||
tmpDir, err := ioutil.TempDir("", "update-genproto") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Printf("temp dir created at %s\n", tmpDir) | ||
tmpGoogleapisDir := filepath.Join(tmpDir, "googleapis") | ||
tmpGenprotoDir := filepath.Join(tmpDir, "genproto") | ||
tmpGocloudDir := filepath.Join(tmpDir, "gocloud") | ||
tmpProtoDir := filepath.Join(tmpDir, "proto") | ||
|
||
// Clone repositories if needed. | ||
grp, _ := errgroup.WithContext(ctx) | ||
gitShallowClone(grp, "https://github.com/googleapis/googleapis.git", c.googleapisDir, tmpGoogleapisDir) | ||
gitShallowClone(grp, "https://github.com/googleapis/go-genproto", c.genprotoDir, tmpGenprotoDir) | ||
gitShallowClone(grp, "https://github.com/googleapis/google-cloud-go", c.gocloudDir, tmpGocloudDir) | ||
gitShallowClone(grp, "https://github.com/protocolbuffers/protobuf", c.protoDir, tmpProtoDir) | ||
if err := grp.Wait(); err != nil { | ||
log.Println(err) | ||
} | ||
|
||
// Regen. | ||
conf := &generator.Config{ | ||
GoogleapisDir: deafultDir(tmpGoogleapisDir, c.googleapisDir), | ||
GenprotoDir: deafultDir(tmpGenprotoDir, c.genprotoDir), | ||
GapicDir: deafultDir(tmpGocloudDir, c.gocloudDir), | ||
ProtoDir: deafultDir(tmpProtoDir, c.protoDir), | ||
GapicToGenerate: c.gapicToGenerate, | ||
OnlyGenerateGapic: c.onlyGapics, | ||
LocalMode: true, | ||
} | ||
if _, err := generator.Generate(ctx, conf); err != nil { | ||
log.Printf("Generator ran (and failed) in %s\n", tmpDir) | ||
log.Fatal(err) | ||
} | ||
return nil | ||
} | ||
|
||
// gitShallowClone clones a repository into the provided tmpDir if a dir has not | ||
// been specified. | ||
func gitShallowClone(eg *errgroup.Group, repo, dir, tmpDir string) { | ||
if dir != "" { | ||
return | ||
} | ||
eg.Go(func() error { | ||
log.Printf("cloning %s\n", repo) | ||
|
||
_, err := git.PlainClone(tmpDir, false, &git.CloneOptions{ | ||
URL: repo, | ||
Progress: os.Stdout, | ||
Depth: 1, | ||
Tags: git.NoTags, | ||
}) | ||
return err | ||
}) | ||
} | ||
|
||
// deafultDir returns the default option if dir is not set. | ||
func deafultDir(def, dir string) string { | ||
if dir == "" { | ||
return def | ||
} | ||
return dir | ||
} |
Oops, something went wrong.