Skip to content

Commit

Permalink
chore(internal/gapicgen): have genbot fallback to env vars (#2762)
Browse files Browse the repository at this point in the history
Moved all direct use of env vars out of docker and into the Go
code.
  • Loading branch information
codyoss authored Aug 25, 2020
1 parent bc9dce9 commit b5a5582
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
5 changes: 0 additions & 5 deletions internal/gapicgen/cmd/genbot/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 6 additions & 9 deletions internal/gapicgen/cmd/genbot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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
```

Expand Down
15 changes: 13 additions & 2 deletions internal/gapicgen/cmd/genbot/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path"
"strings"
"time"

Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions internal/gapicgen/cmd/genbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b5a5582

Please sign in to comment.