From b5a5582a2318ea9971d0972116f6379a41bf7d91 Mon Sep 17 00:00:00 2001 From: Cody Oss <6331106+codyoss@users.noreply.github.com> Date: Tue, 25 Aug 2020 12:44:43 -0600 Subject: [PATCH] chore(internal/gapicgen): have genbot fallback to env vars (#2762) Moved all direct use of env vars out of docker and into the Go code. --- internal/gapicgen/cmd/genbot/Dockerfile | 5 ----- internal/gapicgen/cmd/genbot/README.md | 15 ++++++--------- internal/gapicgen/cmd/genbot/github.go | 15 +++++++++++++-- internal/gapicgen/cmd/genbot/main.go | 12 ++++++++++++ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/internal/gapicgen/cmd/genbot/Dockerfile b/internal/gapicgen/cmd/genbot/Dockerfile index 6b5353e3887b..f016c213ad93 100644 --- a/internal/gapicgen/cmd/genbot/Dockerfile +++ b/internal/gapicgen/cmd/genbot/Dockerfile @@ -41,13 +41,8 @@ RUN mkdir /root/.ssh && echo -e "Host github.com\n\tStrictHostKeyChecking no\n" RUN echo -e '#!/bin/bash\n\ set -ex\n\ - echo "https://$GITHUB_USERNAME:$GITHUB_ACCESS_TOKEN@github.com" > ~/.git-credentials \n\ go mod download \n\ go run cloud.google.com/go/internal/gapicgen/cmd/genbot \ - --githubAccessToken=$GITHUB_ACCESS_TOKEN \ - --githubUsername=$GITHUB_USERNAME \ - --githubName="$GITHUB_NAME" \ - --githubEmail=$GITHUB_EMAIL \ ' >> /run.sh RUN chmod a+x /run.sh diff --git a/internal/gapicgen/cmd/genbot/README.md b/internal/gapicgen/cmd/genbot/README.md index 0365e26bcc4e..1fa64ea63bfc 100644 --- a/internal/gapicgen/cmd/genbot/README.md +++ b/internal/gapicgen/cmd/genbot/README.md @@ -18,10 +18,7 @@ non-trivial amounts of space on your computer. 1. Make sure you have all the tools installed listed in genlocal's README.md 2. Run: -``` -export GITHUB_USERNAME=jadekler -export GITHUB_ACCESS_TOKEN=11223344556677889900aabbccddeeff11223344 -echo "https://$GITHUB_USERNAME:$GITHUB_ACCESS_TOKEN@github.com" > ~/.git-credentials +```shell cd /path/to/internal/gapicgen go run cloud.google.com/go/internal/gapicgen/cmd/genbot \ --githubAccessToken=$GITHUB_ACCESS_TOKEN \ @@ -37,15 +34,15 @@ Note: this can be quite slow (~10m). Note: this may leave a lot of docker resources laying around. Use `docker system prune` to clean up after runs. -``` +```shell cd /path/to/internal/gapicgen/cmd/genbot docker build . -t genbot docker run -t --rm --privileged \ -v `pwd`/../..:/gapicgen \ - -e "GITHUB_ACCESS_TOKEN=11223344556677889900aabbccddeeff11223344" \ - -e "GITHUB_USERNAME=jadekler" \ - -e "GITHUB_NAME=\"Jean de Klerk\"" \ - -e "GITHUB_EMAIL=deklerk@google.com" \ + -e GITHUB_ACCESS_TOKEN \ + -e GITHUB_USERNAME \ + -e GITHUB_NAME \ + -e GITHUB_EMAIL \ genbot ``` diff --git a/internal/gapicgen/cmd/genbot/github.go b/internal/gapicgen/cmd/genbot/github.go index 8be0eae8854c..1c944716216e 100644 --- a/internal/gapicgen/cmd/genbot/github.go +++ b/internal/gapicgen/cmd/genbot/github.go @@ -17,9 +17,12 @@ package main import ( "context" "fmt" + "io/ioutil" "log" "os" "os/exec" + "os/user" + "path" "strings" "time" @@ -91,7 +94,7 @@ type GithubClient struct { // NewGithubClient creates a new GithubClient. func NewGithubClient(ctx context.Context, username, name, email, accessToken string) (*GithubClient, error) { - if err := setGitCreds(name, email); err != nil { + if err := setGitCreds(name, email, username, accessToken); err != nil { return nil, err } @@ -103,7 +106,15 @@ func NewGithubClient(ctx context.Context, username, name, email, accessToken str } // SetGitCreds sets credentials for gerrit. -func setGitCreds(githubName, githubEmail string) error { +func setGitCreds(githubName, githubEmail, githubUsername, accessToken string) error { + u, err := user.Current() + if err != nil { + return err + } + gitCredentials := []byte(fmt.Sprintf("https://%s:%s@github.com", githubUsername, accessToken)) + if err := ioutil.WriteFile(path.Join(u.HomeDir, ".git-credentials"), gitCredentials, 0644); err != nil { + return err + } c := exec.Command("git", "config", "--global", "user.name", githubName) c.Stdout = os.Stdout c.Stderr = os.Stderr diff --git a/internal/gapicgen/cmd/genbot/main.go b/internal/gapicgen/cmd/genbot/main.go index 1853b6174900..028d223f349a 100644 --- a/internal/gapicgen/cmd/genbot/main.go +++ b/internal/gapicgen/cmd/genbot/main.go @@ -72,6 +72,18 @@ func main() { flag.Usage = usage flag.Parse() + if *githubAccessToken == "" { + *githubAccessToken = os.Getenv("GITHUB_ACCESS_TOKEN") + } + if *githubUsername == "" { + *githubUsername = os.Getenv("GITHUB_USERNAME") + } + if *githubName == "" { + *githubName = os.Getenv("GITHUB_NAME") + } + if *githubEmail == "" { + *githubEmail = os.Getenv("GITHUB_EMAIL") + } for k, v := range map[string]string{ "githubAccessToken": *githubAccessToken,